1

My goal:

  1. I have a new form where the users can fill textboxes.

  2. The user click on the save button if finished.

  3. I check if the fields are valid or not.

  4. If valid, based on the textboxs I get records from an outer list and based on that I update the outerlist.

  5. 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!

1

Your number of parameters for $SP().list().update() is incorrect. after must be in the same {} than where.

I rewrote your code:

function beforeSaving() {
  alert("appear 3.");
  var deferred=jQuery.Deferred();
  var toplace=Number(document.getElementById("targettextbox").value);
  $SP().list("targetList").get({where: 'Targetcolumn = '+toplace}, function getData(data) {
    var tothis=(Number(data[0].getAttribute("updatecolumn")) + 1);
    alert("appear 5.");
    $SP().list("targetList", "http://.../sites/").update({
      updatecolumn:tothis
    },{
      where: 'Targetcolumn = '+toplace,
      after:function() { // "after" is in the same {} than "where"
        alert("not appear");
        deferred.resolve();
      }
    });
  });
  alert("appear 4.");
  return deferred;
}
  • Ahh so that was it. I couldn't fine example for "where + after" combination and that was in your original code from the prev post so I didn't think that is the problem. Thanks you very much again, u saved my ass :) – Nefri Oct 24 '17 at 16:03
1

Your problem is WPQ2FormCtx because it's not the correct variable name to use in your code. Please use my previous answer and read again this part:

// force PostBackRequired to true in the context, otherwise it won't work if you open the into a modal
// Please note that "WPQ2FormCtx" may change depending of your server...
// Look at the source code of your page to find "FormCtx" and verify this variable is correct

So you have to use the code I have provided, but just replace WPQ2FormCtx with the one from your page.

Once you have the correct variable name, you have to modify it everywhere, including in this part (that's causing your issue):

    beforeSaving().done(function() {
      // 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() {
      // if it fails we just unblock the save button
      $get(WPQ2FormCtx.SubmitButtonID).disabled=false;
    })
  • Thanks you very much again! I belive I checked as u mentioned and "WPQ2FormCtx" is exist in my source. I am keep trying :) – Nefri Oct 24 '17 at 7:26
  • Before I added it to the loading code it didn't work and I was not sure when to execute it. But looks like if I set this parameter to true (by default in the source it was false) in the onload code it contans no error and looks good. Just the result is not what I want, so I suspect different kind of problem. – Nefri Oct 24 '17 at 7:46
  • I updated the code, if u have a free moment pls take a look on it. And thanks you very much for keep helping me! – Nefri Oct 24 '17 at 9:49
  • After several more test looks like the problem is that the process is not entering the "after:" part of the code. I also tryed with simple function or "success: " but the result is the same, not entering into it, but I don't get the reason yet. – Nefri Oct 24 '17 at 11:34
  • Okey I got it. Looks like functions after updates with "where" are not working for me. But I used .then() after the update and that way worked well! – Nefri Oct 24 '17 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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