My goal:
I have a new form where the users can fill textboxes.
The user click on the save button if finished.
I check if the fields are valid or not.
If valid, based on the textboxs I get records from an outer list and based on that I update the outerlist.
Create a new item based on the textboxes.
I got detailed instructions in a previous post, but I started both SP and JS recently so I am not sure how to implement to the current situation.
What happens: If the user hit the "Save" button, the outer list is updated well, but new item is not created (default view is not loaded) and the Save button became disabled.
Here is my code (Except the first comment, all comments are from the instructor):
UPDATE: I added some alerts to see the sequence of the process. Looks like something is wrong cause it not even enters to the after:
part of the code even if the update is always successful.
// WPQ2FormCtx.PostBackRequired=true; //This executed when the page is loaded (not sure that is the good way to use this)
function beforeSaving()
{
// force PostBackRequired to true in the context, otherwise it won't work if you open the into a modal
alert("appear 3.");
var deferred=jQuery.Deferred();
var toplace=Number(document.getElementById("targettextbox").value);
$SP().list("targetList","http://.../sites/").get({where: 'Targetcolumn = '+toplace}, function getData(data)
{
var tothis;
tothis=(Number(data[0].getAttribute("updatecolumn")) + Number("1"));
alert("appear 5.");
$SP().list("targetList", "http://.../sites/").update(
{
updatecolumn: tothis
},{where: 'Targetcolumn = '+toplace },
{
after:function()
{
// you now need to indicate the operation has been completed:
alert("not appear");
deferred.resolve();
}
});
});
alert("appear 4.");
return deferred;
}
// we override SPClientForms.ClientFormManager.SubmitClientForm
SPClientForms.ClientFormManager.SubmitClientForm=function(b){
alert("appear first");
var a=SPClientForms.ClientFormManager.GetClientForm(b);
var res = (a!=null&&a.SubmitClientForm());
// if the form is not valid, then res===true
if (res === true) return true;
else {
alert("appear 2.");
// if the form is valid we now want to do our asynchronous check
beforeSaving().done(function() {
alert("not appear");
// our test is valid too so we can send the form to the server
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions($get(WPQ2FormCtx.SubmitButtonID).name, "", true, "", "", false, true))
}).fail(function() {
alert("not appear");
// if it fails we just unblock the save button
$get(WPQ2FormCtx.SubmitButtonID).disabled=false;
})
}
// to make sure the form won't be submitted yet
return true;
}
Result: The update is successful, but Save button is disabled permanently and new item is not created.
I am using jquery and sharepointplus. (I have problem with the jquery part) I tryed a lot of kind of approach, but none of them was perfect. Basicly I can force it to save the items, but in that case in the 30% the update is not done before the item creation (aync run), or I can do the updates well (like now), but the new item is not created. I am suspecting I should connect the 2 part of the code maybe with a promise or something, but I am not sure. I am new to both JS and SP, and I should find a solution fast, that is why I am asking for your assistance. Thank you very much for any suggestion!