I have some queries on Apex and JavaScript execution. I want to insert large data to Salesforce after reading the file in JavaScript function and then I want to call the Apex method to insert the data chunk to the database. What I want to know is that how do I stop the execution of JavaScript further until I get the response from Salesforce.

1 Answer 1


Use asynchronous calls. Without using a library, that looks like this:

function sendData(data) {
    var x = new XMLHttpRequest();
    x.onreadystatechange = function() {
        if(this.readyState === 4) {
            // Process data, then...
            if(data.length) {
    x.send(data.shift()); // send the next record

The exact nature of what you you need to do will depend on the framework. If you're using @RemoteAction methods, for example, you can do your next callback in the success handler.

  • Thank you for your answer. As a newbie, I am learning the Web development and also I forgot to mention that the data file is in the format of XLSX. Nov 14, 2015 at 6:11
  • You're going to have a very hard time reading xlsx files into JavaScript. I suggest you sick with something easier, like csv files.
    – sfdcfox
    Nov 14, 2015 at 7:05

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.