0
  1. I have a sharepoint form where the user fill some textboxes.

  2. The user click on the save button. The script run from here.

  3. Check if all texbox is filled or not.

  4. If good, get some fields from an outer list based on the inputs and update some other list based on that.

  5. Save the inputs as a new item.

The problem: I use a function named PreSaveAction(). This called when the user click on the "Save" button and only if this return with true the form ll be saved. So before returning with true I should do the field check and list updates, I don't know where to add this return true. If I add this directly inside the PreSaveAction() it's running asyncron so with 30-40% chance returns before the update is done. I am new to javascript so I need a little help.

My simplified code looks like this:

function PreSaveAction() //This start if the user click on the save button, and create a new item if return with true
{
  var promresult;
  if(textbox!="")
  {
    //do my staff based on promise like: promresult=promise.then(input textbox, get list field).then(update).then(resolve true, fail false)
    return promresult;  //this run before the update is completed and promresult get value
  }
  else
  {
    return false;  //nothing happens if the textboxes are not filled yet.
  }

}

I read about promises and I think I understanded how they work, but at my case the PreSaveAction() have multiple functions (detect onclick, do my stuffs, return with true/false). I tryed several approach, but I coudn't find the solution where and how to place the return from presave to wait my promise to finish? Or should I use completly different approach? Thanks you very much for your kind help!

Nefri
  • 197
  • 2
  • 13
  • People will not be able help you without seeing the actual asynchronous code in your function. It appears you are trying to return an asynchronously retrieved value from a function. You cannot do that. The value is not available at the time the function returns. The async callbacks will be called much later. – jfriend00 Oct 19 '17 at 11:06
  • I simplyfied the code and tryed to focus on the problam because I didn't want to annoy ppl with long and hard to understand code. But looks like you got the idea of my problem. So is this impossible? What other type of solution would u use to archive something similar? My problem is that this "Presaveaction" is an integrated function and I don't know how to split it to components. For example: if I would use simple onlcick event, do the check, update list in the next function, and force the save in a 3. function, would it work? – Nefri Oct 19 '17 at 11:42
  • Stackoverflow does not work well with theoretical questions. It works a lot better with actual code and a very specific problem. The operative part of your question is all in your asynchronous code, none of which you've shared so your question is pretty much entirely theoretical. Given that, about the best answer I can offer is this [How do I return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323). – jfriend00 Oct 19 '17 at 11:55

1 Answers1

0
function PreSaveAction() //This start if the user click on the save button, and create a new item if return with true
{
  var promresult;
  if(textbox!="")
  {
    //do my staff based on promise like: promresult=promise.then(input textbox, get list field).then(update).then(resolve true, fail false)
    return promresult;  //this run before the update is completed and promresult get value
  }
  else
  {
    return Promise.resolve(false);  //nothing happens if the textboxes are not filled yet.
  }

}

PreSaveAction()
  .then(function(result){
    if (result===false) {
      // this means the textboxes are not filled yet
    } else {
      // result is whatever is returned from promresult
    }
  })
TKoL
  • 13,158
  • 3
  • 39
  • 73
  • Hi! I tested it and looks like Presaveaction().then() structure is not works. Are u sure is this compatible with promises like this? Or maybe I am the one doing something wrong but I get, "SyntaxError: Unexpected token ." from this row :( – Nefri Oct 19 '17 at 12:08
  • perhaps 'promresult' isn't a promise. You didn't post your actual promresult code so I don't really know what it is. Anythign could be wrong there – TKoL Oct 19 '17 at 12:44