Unsaved data and custom tasks
Is there a way to commit a page prior to running a task? I have implemented a custom tasks to add Manual Activity to a Service Request and Incidents. After the tasks runs it refreshed the page so that the user can now see and edit the manual activity. However, if there is unsaved data on form when the task runs the user is prompted to either stay or leave. If they choose Stay on this page, they will not loose any edits, but they will need to click Apply to have the page refresh so they can see and edit the manual activity. If they click Leave this page, it will erase all edits made, but they will be able to see the manual activity.
I've pasted the code used to create the MA below. I ran the debugger on this and saw that the strData variable is populated with the updated values, but it seems like they are ignored. I've tested this in version 6 and 5.0.9 of the portal.
for (var i = newpos; i < length; i++) {<br> finalsr.Activity[i].SequenceId = i + 1;<br> }<br> }<br> else {<br> var newpos = parseInt(sequenceid) + 1;<br> finalsr.Activity.push({<br> "ClassTypeId": "7ac62bd4-8fce-a150-3b40-16a39a61383d",<br> "SequenceId": newpos,<br> "Title": title,<br> "Description": desc,<br> "Id": "MA{0}",<br> });<br><br>
for (var i = newpos; i < length; i++) {<br> finalsr.Activity[i].SequenceId = i + 1;<br> }<br> }<br> var strData = { "formJson":{"original": ogsr, "current": finalsr}}<br> //console.log (strData);<br> $.ajax({<br> url: "/api/V3/Projection/Commit",<br> type: "POST",<br> contentType: "application/json; charset=utf-8",<br> dataType: "json",<br> data: JSON.stringify(strData) ,<br> success: function (data2) {<br> window.location.href = window.location.protocol + "//" + window.location.hostname + "/" + type + "/Edit/" + srid<br> }<br> });<br><br>
}
function addMAtoWI (workitem, title, desc, sequenceid, type, inpa){<br> //console.log("Sequence Id: " + sequenceid);<br> //console.log(desc);<br> var ogsr = workitem;<br> var finalsr = JSON.parse(JSON.stringify(ogsr));<br> var srid = ogsr.Id;<br> var length = ogsr.Activity.length;<br> //if there are no activities currently just add the Activity<br> if (length == 0){<br> finalsr.Activity.push({<br> "ClassTypeId": "7ac62bd4-8fce-a150-3b40-16a39a61383d",<br> "SequenceId": 0,<br> "Title": title,<br> "Description": desc,<br> "Id": "MA{0}",<br> "Status": {"Id": "11fc3cef-15e5-bca4-dee0-9c1155ec8d83"}<br> });<br> }<br> //They are adding the Activity within a Parallel Activity<br> else if (inpa == true) {<br> var palength = ogsr.Activity[sequenceid].Activity.length;<br> //console.log (palength);<br> var newseqid = palength + 1;<br> //console.log (newseqid);<br> //If the Parallel Activity is In Progress make the Manual Activity In Progress<br> if (ogsr.Activity[sequenceid].Status.Name == "In Progress") {<br> finalsr.Activity[sequenceid].Activity.push({<br> "ClassTypeId": "7ac62bd4-8fce-a150-3b40-16a39a61383d",<br> "SequenceId": newseqid,<br> "Title": title,<br> "Description": desc,<br> "Id": "MA{0}",<br> "Status": {"Id": "11fc3cef-15e5-bca4-dee0-9c1155ec8d83"}<br> });<br> }<br> //If the Parallel Activity is not In Progress make the Manual Activity Pending<br> else {<br> finalsr.Activity[sequenceid].Activity.push({<br> "ClassTypeId": "7ac62bd4-8fce-a150-3b40-16a39a61383d",<br> "SequenceId": newseqid,<br> "Title": title,<br> "Description": desc,<br> "Id": "MA{0}"<br> });<br> } <br> }<br> //They are adding an Activity and it isn't within a Parallel Activity<br> else if (type == "Incident"){<br> var newpos = parseInt(sequenceid) + 1;<br> finalsr.Activity.push({<br> "ClassTypeId": "7ac62bd4-8fce-a150-3b40-16a39a61383d",<br> "SequenceId": newpos,<br> "Title": title,<br> "Description": desc,<br> "Id": "MA{0}",<br> "Status": {"Id": "11fc3cef-15e5-bca4-dee0-9c1155ec8d83"}<br> });<br><br>
Best Answer
-
james_kleinschnitz Cireson Dev, Product Owner Advanced IT Monkey ✭✭✭Matthew
I did some quick testing here but you should be able to use the pageForm.save function to accomplish what you want. The save function takes two callback functions as arguments:var save = function (success, failure) {....
so basically it looks like you will want to wrap your ajax call as the success callback and add a failure callback to handle as needed (not tested):<div> <pre class="CodeBlock"><code>pageForm.save(function() { /*save is complete*/ $.ajax({ url: "/api/V3/Projection/Commit", type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", data: JSON.stringify(strData) , success: function (data2) { window.location.href = window.location.protocol + "//" + window.location.hostname + "/" + type + "/Edit/" + srid } }); },function(error){ /*save failed*/ alert(error); /*remove the loading mask*/ app.lib.mask.remove(); } );
Since you your success function is refreshing the page there is no need to remove the loading mask, but I added the code in the failure function to remove the loading mask for reference.
Let me know if this answers your question.
Thanks
--
James5
Answers
I did some quick testing here but you should be able to use the pageForm.save function to accomplish what you want. The save function takes two callback functions as arguments:
so basically it looks like you will want to wrap your ajax call as the success callback and add a failure callback to handle as needed (not tested):
Since you your success function is refreshing the page there is no need to remove the loading mask, but I added the code in the failure function to remove the loading mask for reference.
Let me know if this answers your question.
Thanks
--
James