0

I'm uploading files in vf page form (site force.com) using remote action.

I want to wait for the remote action will finish its method, and only then proceed - means - do it synchronously, because I need all the files will be loaded before submitting the form.

 Visualforce.remoting.Manager.invokeAction(
                    '{!$RemoteAction.PaymentFormController.dfqwerFile}',
                    var1,var2,var3,var4,var5,
                    function(result, event){
                        if (event.status) {
                               console.log('status',event.status);
                        } else if (event.type === 'exception') {
                               console.log('failed');

                        } else {
                            console.log('Succes');

                        }
                    },
                    {escape: true}
                );

Any way to achieve it?

Thanks!

1 Answer 1

1

Remote functions are asynchronous by nature. There's no such thing as synchronous calls in JavaScript these days, because they cause the page to become unresponsive while they're processed. However, you can simulate this in your code using async await:

async function remoteAction(var1, var2, var3, var4, var5) {
  return new Promise((resolve, reject) => {
    Visualforce.remoting.Manager.invokeAction(
      "{!$RemoteAction.PaymentFormController.dfqwerFile}",
      var1,
      var2,
      var3,
      var4,
      var5,
      (result, event) => {
        event.status ? resolve(result) : reject(event.message);
      },
      { escape: true }
    );
  });
}
async function onclick(event) {
  try {
    // Get the values for the variables, then...
    await remoteAction(var1,var2,var3,var4,var5);
    // Next line will execute only when remote action finishes.
    doMoreStuffHere();
  } catch(e) {
    // handle the error here
  }
}

Note that Internet Explorer does not support this syntax, so if you use it, you'll get a script error instead of the desired behavior.

1
  • Thanks for your response.. It helped me, but since Internet Explorer is one of the common explorers in my environment, I couldn't give up on this. I used eventually transient variable.
    – Salvation
    Commented Jun 27, 2020 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .