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.
- 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.
- 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.