on.submit code doesn't update SharePoint list

rss

Posted by Jdubs - May 3 ’16 at 11:20

Scenarios:

  1. Fire the following code when the form opens -->  The code works and the SharePoint list is updatedwith the new value.
  2. Fire the following code in a fd.onsubmit(function() { }) wrapper.  The code works but the SharePoint list is not updated with the new value.

//Get Time Stamp of when form was Opened

var formOpenedTime = new Date();

 

//Compare Current User to Assigned To

SP.SOD.executeOrDelayUntilScriptLoaded((function() {

var ctx = new SP.ClientContext.get_current();

var web = ctx.get_web();

ctx.load(web);

user = web.get_currentUser();

user.retrieve();

ctx.executeQueryAsync(

function() {

currentuser = user.get_loginName();

assignedTo = fd.field('AssignedTo').value().dictionaryEntries[0].AccountName;

if ( currentuser == assignedTo ) {

//CALCULATE TIME WORKED ON ITEM

//Get Time Stamp of when form was Closed

var formClosedTime = new Date();

//Calculate total time form opened

var timeElapsed = (formClosedTime - formOpenedTime);

var seconds = timeElapsed / 1000

fd.field('calcWorkingTime').value(seconds);

//Update Time

var currentTime = parseFloat(fd.field('calcWorkingTime').value());

var oldTime = parseFloat(fd.field('TotalTime').value());

var newTime = currentTime + oldTime;

fd.field('TotalTime').value(newTime);

fd.field('EndTime').value(newTime);

}

});

}), "SP.js");

 

I've commented everything out and verified that the on.submit wrapper is solely responsbile for the changed value not hitting the list.

 

I cannot figure out why :(

  • Jdubs
    Member
    Member for: 5 years 2 months
    #1 by Jdubs May 4 ’16 at 03:01

    Just in case someone is able to get to this, I'll provide some clarification below because I really need this to work.

     

    When the code below runs in the on.submit wrapper, the code works and I can see the values change in the Edit Form (as intended), but after the item is saved, when I go to the SharePoint list the values on the list dont change (even though they changed in the form before it was submitted).

     

    Conversely, if I run the code outside of the on.submit wrapper, everything still works, but the values are actually updated in the SharePoint list upon submit.

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 6 years 4 months 29 days
    #2 by Dmitry Kozlov May 5 ’16 at 01:00

    [#1]: The problem is that you're using asinchronous operation (ctx.executeQueryAsync) in the synchronous handler (fd.onsubmit). You need to rewrite your code following way:

    1. Retrieve the current user's login when the form is loaded and save it in a local variable

    2. Use the variable in the onsubmit handler.

  • Jdubs
    Member
    Member for: 5 years 2 months
    #3 by Jdubs May 18 ’16 at 01:56

    [#2]: Thanks Dmitry.  You rock.

     

    Was beating my head against the wall on that one.

Displaying 1 to 3 of 3 messages