2

Is there a way to trigger a JavaScript function to execute when a new item is added to a list?

I have a task list with a Priority column. The Priority column is storing a unique number for each item in the list (so each task has an assigned priority number).

When a new task is added with a Priority number that is already in use, I want to increment the priority number of each item with a higher priority number then the new task. If I can grab the ID and priority number of the new item, then I believe I can accomplish the rest with SPServices.

But I don't know how to trigger the JavaScript function when a new item is added (and grab the necessary data from the newly added item).

2

This shouldn't be done through JavaScript. In order to react to a server side event (creation of a list item) you should use a SharePoint 2013 Workflow that is triggered on item creation.

You can then query the list for all items with a priority >= to the current item and update them individually.

This approach would work in 2013, 2016, and O365. You could also use flow in O365. I would recommend against event receivers (including remote event receivers) in all 3 of those environments.

A few reasons why this shouldn't be done in JavaScript:

  • Relies on the users permissions (runs as the user so if they don't have access to an item, there would be an issue)
  • Relies on the user keeping their browser open and on the page
  • Won't be able to respond to save errors thrown by the server
  • 1
    Ok that sounds reasonable, I'm not as familiar with workflows as I am with JavaScript. How do I query this list for the priorities I'm looking for in a workflow? And how am I able to store the IDs for those items? I don't see any sort of array as an option when I try to create a workflow variable? I'm using 2010 workflows by the way. – Jason Jan 3 '18 at 19:18
0

You can put a JavaScript function on the page named PreSaveAction() which executes after the Save button is clicked.

If you return false, the item won't be saved, and if you return true, it will be saved.

You could run your JavaScript code there, but keep in mind that your new item won't be saved yet. If you return true, and some error occurs submitting it to the server, your code will have run, but the new item won't exist.

If you can accept that risk, then this would potentially work for you.

The other way is to use an event receiver, but there you are talking C# code (and potentially more complicated if you are using SharePoint Online).

  • How would I pull the ID and Priority for the new item? Pull them from the form itself? – Jason Jan 3 '18 at 17:41
  • Why would you need the new ID? (You can’t get it because it hasn’t been created yet.) – mannaggia Jan 3 '18 at 20:09
  • Right, so I guess I don't need the ID, but I do need the Priority. I'm guessing the answer is to pull it from the Priority input field on the form? – Jason Jan 3 '18 at 21:24
  • Right, that’s what I would do. – mannaggia Jan 4 '18 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.