on.submit code doesn't update SharePoint list

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
Jdubs
Posts: 73
Joined: Fri Dec 19, 2014

03 May 2016

Scenarios:
  • Fire the following code when the form opens --> The code works and the SharePoint list is updatedwith the new value.
  • 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
Posts: 73
Joined: Fri Dec 19, 2014

04 May 2016

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.

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

05 May 2016

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
Posts: 73
Joined: Fri Dec 19, 2014

18 May 2016

Thanks Dmitry. You rock.

Was beating my head against the wall on that one.

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 16 guests