0

On my new form, I wish to execute further code that involves processing the ID of the item just created. Obviously the ID is not available before the item is saved, but I was hopeful I could get the ID once the window closed.

Alas, it does not appear to be the case. I don't want to get the ID of the previously added item and then add one to it. Any suggestions? It seems that the commit occurs even after the window closes. Is there a commit event that I can listen for?

In my example, there could be a disjoint in the processing of thisID, as it is populated in a detached async process, but that aside, no value of the latest ID is ever displayed in the console log either.

My code looks like this

 var call = '$'; 
    var thisID  = '';
    function MyPreSaveAction (){    

    // Set key field to be $ to capture it in the GetItemID function

            window.addEventListener("unload", getID(), false);
            return true;
      }

     function getID(){
        var n = 0;

    // Loop several times to see if the added item appears.. alas no, it doesn't

        while (n < 20) {
             n++;
            SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getItemID());
           }

            alert('Thank you for your request. Your receipt number is: '+thisID);
        }

    function getItemID(){
       var context = new SP.ClientContext();
       var list = context.get_web().get_lists().getByTitle('My List');
       if (call == "$") { call = "#"} else { call = "$"}  // Vary it in case it is a caching issue
       var caml = new SP.CamlQuery();
       caml.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name="Keyfield" /><Value Type="Text">'+call+'</Value></Eq><Eq><FieldRef Name="Author" LookupId="True"/><Value Type="Lookup">' + _spPageContextInfo.userId + '</Value></Eq></And></Where></Query><RowLimit>1</RowLimit></View>');

       var listItems = list.getItems(caml);
       context.load(listItems , 'Include(Title,ID)');

       context.executeQueryAsync(function(){
       var listEnumerator = listItems.getEnumerator();  
       while (listEnumerator.moveNext()) {
             // get the current list item.
             var listItem = listEnumerator.get_current();
             // get the field value.
             thisID = listItem.get_item('ID');
             console.log('Created ID is '+ thisID);
                 };
             },
              function(){  
             });
      }
2
  • Can you let me know the context/problem statement that this implementation caters to? Commented Jul 12, 2017 at 10:44
  • I am trying to set up a post commit event so that I can do further processing of the item that was just created.
    – southskies
    Commented Jul 13, 2017 at 0:29

1 Answer 1

0

There are two solutions that I have found.

  1. Change the URL so that the task will go to another page that can then process the item subsequently, as per the following link Customize newform save button - Save and redirect to another form

  2. Make all list fields optional, and put all validation into the presaveaction, so by the time the processing gets to the end of that function you know that the commit will proceed. Then do what I didn't want to do, look for the last ID and then add 1 to it. Use the following query to get his ID. I will also add code to the Editform to make sure that any save action updates that relied upon the ID value were correctly updated.

    <View><Query><OrderBy><FieldRef Name="ID" Ascending="False" /></OrderBy></Query><RowLimit>1</RowLimit></View>

I ended up going to option 2 as there was less coding involved, and the flow was more obvious.

I hope that MS will eventually change SP so that there is a postsaveaction function available.

If you have any other better ideas, still keen to find out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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