0

I created a upload Visual Force page which selects a CSV file and read in the contents. With the file data I call a Apex remote action. The remote action method does a upsert into a custom object. If the file is larger then 1400 records I receive an error that the memory size to greater then 1 Meg. To fix this I broke up the data which was read in from the file into 1000 record chunks. After each 1000 records is read I call the remote action. This causes another problem Upsert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: [] The first fix I tried was to delay between remote calls. This is not a 100% fix. I am still getting errors on IE 11. What can I do to handle larger amounts of record?

Thank you, Dan Glaser

I would like to give a better explanation of the problem. I am using Javascript to select and read a CSV files data.

var files = evt.target.files; // FileList object which is called by an eventhandler.

I process the data by reading in each record and putting the data into an javascript array. the data comes from var data = $.csv.toArrays(csv); where CSV is the data from the file var csv = event.target.result . Once a 1000 records are put into the array the array is passed to the remoteaction call via:

Visualforce.remoting.Manager.invokeAction(
    '{!$RemoteAction.ProductMasterUploadController.upsertProdMaster}',
    masterItems,
    function(result, event) {
        isExecuting=false;
        if ( event.status ) {
            numbercomplete++;
            if ( numbercomplete >= partialuploads ) {
                document.getElementById("WaitStatus").innerHTML = " Completed! ";
                $("#loading").hide();
                $("#uploadmsg").hide();
            } else {
                document.getElementById("WaitStatus").innerHTML = " Processed "+numbercomplete+" of "+partialuploads;
            } 
        } else if ( event.type === 'exception' ) {
            document.getElementById("responseErrors").innerHTML =  event.message + "<br/>\n<pre>" + event.where + "</pre><br>";
            remoteresults = 0;
            uploadError = true;    
        } else {
            document.getElementById("responseErrors").innerHTML = event.message;
            remoteresults = 0;
            uploadError = true;
        }
    },
    { buffer: false, escape: true, timeout: 60000}
);

The apex code is very basic:

@RemoteAction 
public static void upsertProdMaster(List<Customer_Product_Master__c> masterItems){
    Customer_Product_Master__c [] prodmstr = [SELECT Id FROM Customer_Product_Master__c LIMIT 2 FOR UPDATE];
    upsert masterItems External_ID__c;
}
  • Hi Dan, welcome to SFSE! Please take the time to visit the Help center and read How do I ask a good question. The more details you provide, particularly code you've written, the more likely it is that someone will respond to your question with an answer you'll find helpful. – crmprogdev Sep 17 '15 at 18:23
1

See Configuring a JavaScript Remoting Request in the documentation. Specifically, the system will batch calls close together and run them at once. This is a performance boost, but can cause locking errors. Instead, explicitly disable buffering and chain your callback such that the second won't run until the first completes. Your code should look like this:

controller.method(param1, param2, ..., callback, { buffer: false });

You may also reduce the odds of locking problems by querying the record beforehand using "FOR UPDATE" to gain an explicit lock. This gives you about a five second window for the first request to complete before the second request fails, which should be plenty of time in normal circumstances.

| improve this answer | |
  • I added the SELECT Id FROM sobject LIMIT 2 FOR UPDATE and my remote call uses { buffer: false, escape: true, timeout: 60000}. Now I am getting the Unable to connect to the server (transaction aborted: timeout) and also the UNABLE_TO_LOCK_ROW. – Dan Glaser Sep 17 '15 at 18:39
  • Without your entire code,I much doubt we can help you further. Honestly, you might be better off with someone who can log in to your org and debug this. – sfdcfox Sep 18 '15 at 16:02

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.