3

I have a batch job running asynchronously and I believe I am getting this error when the record run by batch job is being updated by a user at the same time. I am getting above error in test class and I have a feeling I might get the same error in production because of the batch job.

  1. Cases are inserted into SFDC from ETL in loads of 1000. After cases are inserted, "After Trigger" is fired and these cases are sent to batch class after checking if the case status is not closed.
  2. Batch job then asynchronously posts the field value on these cases to web service.

My test class gives the error mentioned in subject when all tests are run in parallel.

  • If I select "Disable parallel apex testing" under options in Apex test execution, the test class passes but with the option unchecked, I am getting this error.

  • I referred to Mohith's answer for question # 20921and considering to add 'for update' but I am confused on where to add that in my batch class.

  • trigger sends all list of cases to class which checks if status is closed.

  • collect all cases that match above criteria and store them in list
  • Pass this list to batch class

Here is my batch class:

public class CaseTriggerBatch implements Database.Batchable<caseVars>, Database.AllowsCallouts {

    public List<CaseVars> cM;
    public Integer count;       

    public CaseTriggerBatch(List<CaseVars> caseWrap){
        cM = caseWrap;
        count = caseWrap.size();
    }    

    public Iterable<caseVars> start(Database.BatchableContext bc) {
        return cM;
    }

    public void execute(Database.BatchableContext bc, List<CaseVars> scope) {

.
.
.
}
}

Test Class:

  public static testmethod void testBatch(){       
        list<CaseVars> cVList = new list<CaseVars>();
        for(integer i=0;i<10;i++){
            CaseVars cV = new CaseVars();
            cV.caseOwnerName = 'Test user'+i;
            cV.accountName = 'Test'+i;
            cV.parentAccountName = 'Parent'+i;
            cVList.add(cV);
        }       
        test.startTest();
        Test.setMock(HttpCalloutMock.class, new SFDCCasePostToCM_Mock());
        Database.executeBatch(new CaseTriggerBatch(cVList));
        test.stopTest();
    }

Update: Added test class.

5
  • 2
    Showing the Unit Test(s) involved would also be beneficial.
    – sfdcfox
    Aug 26, 2015 at 16:25
  • Assuming you're using @isTest or @isTest(SeeAllData=false) on your unit class, I definitely don't see anything here that should cause concurrency issues. We're going to need to find where the problem lies. Are you using a custom setting to store data somehow? That's about the only situation that could cause an issue if you're not using live data.
    – sfdcfox
    Aug 26, 2015 at 17:16
  • I am using @isTest and sealldata=false. I am not using any custom setting. There are couple of other functions being done by after trigger like creating tasks, sending emails etc. I am assuming when batch job is running and at the same time record is used for other functions I am getting this error. Will adding a delay timer to start the batch resolve ? I haven't tried this yet
    – sf_user
    Aug 26, 2015 at 17:22
  • Also planning to implement mohith's solution as posted in question but I am not sure where to add "for update"
    – sf_user
    Aug 26, 2015 at 17:25
  • 6

0

Browse other questions tagged .