The following trigger creates child records on insert of a parent record. It checks for number of years field on parent and creates child records based on the number of years. It also does check for the grantor field and if it is null. If the grantor field is null it creates an account (inserts an account) and then updates the name of account with an auto number on account. Then it creates child records based on number of years on the parent. Finally, the grantor field on parent is assigned with the newly created account and then parent is updated.
The trigger fails on insert of child records(doesn't fails on all child records. I see failure in chunks of 200 records twice when I insert 900 records). It only fails if I do a bulk insert of more than 19 records.
It doesn't fail if I create a parent record via the UI.
I am not getting any failures in data loader. I can only see this error in debug logs.
The trigger is failing on line: insert newfevenlist.
System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []
I found this salesforce knowledge article. Do I need to update the concurrency mode in the dataloader to Serial Mode?
trigger CreateChildren on Parent__c (after insert) {
public List<Id> fecIdList = new List<Id>();
public child__c tempfevent = new child__c();
public child__c tempfevent2 = new child__c();
public List<child__c> newfevenList = new List<child__c>();
public List<child__c> newfevenList2 = new List<child__c>();
Map<Id,Account> newAcc = new Map<Id,Account>();
public List<Account> accList;
public List<Account> newAccList;
public List<Id> accIdList = new List<Id>();
for(Parent__c fec :trigger.new){
if(fec.From_Import__c == true)
fecIdList.add(fec.Id);
}
List<Parent__c> fecList = [Select f.Status__c, f.Data__c,f.Number_of_Years__c, f.Name, f.Id, f.Fiscal_Year__c,
f.Amount__c, f.Grantor__c, f.Grantee__c, f.Budget_Expenses__c, f.Staff_Count__c, f.From_Import__c,
f.Series__c From Parent__c f where f.Id in: fecIdList];
List<RecordType>rectype= [Select r.SobjectType, r.Name, r.Id From RecordType r where r.Name ='External' and r.SobjectType = 'child__c'];
try {
for(Parent__c f:fecList){
system.debug('####f.Grantor__c####'+f.Grantor__c);
if((f.Number_of_Years__c > 0)&&(f.Grantor__c!=null)){
Integer totalYears = (f.Number_of_Years__c).intValue();
for(integer i=0;i<totalYears;i++){
tempfevent.RecordTypeId = rectype[0].Id;
tempfevent.Grantee__c = f.Grantee__c;
tempfevent.Grantor__c = f.Grantor__c;
tempfevent.Status__c = f.Status__c;
tempfevent.Series__c = f.Series__c;
tempfevent.Data__c = f.Data__c;
system.debug('.f.Amount__c..'+f.Amount__c);
if(f.Amount__c!=NULL)
tempfevent.Amount__c = f.Amount__c/totalYears ;
if(f.Fiscal_Year__c!= NULL)
tempfevent.Fiscal_Year__c = String.valueof(Integer.valueof(f.Fiscal_Year__c)+i);
tempfevent.Number_of_years_of_the_child__c = f.Number_of_years__c;
tempfevent.Budget_Expenses__c = f.Budget_Expenses__c;
tempfevent.Staff_Count__c = f.Staff_Count__c;
tempfevent.Parent__c = f.Id;
newfevenList.add(tempfevent);
tempfevent = new child__c();
}
}
else{
if(f.Grantor__c == null){
newAcc.put(f.Id,new Account(Name='Anon',Anonymous__c = true));
}
}
}
insert newfevenList;
insert newAcc.values();
List<Parent__c> fecList2 = [Select f.Status__c, f.Data__c, f.Number_of_Years__c, f.Name, f.Id, f.Fiscal_Year__c,
f.Amount__c, f.Grantor__c, f.Grantee__c, f.Staff_Count__c, f.From_Import__c, f.Series__c
From Parent__c f where f.Id in: newAcc.keyset()];
for(Parent__c f:fecList2){
accIdList.add(newAcc.get(f.Id).Id);
}
accList = [select Id, auto_number__c, name from account where id in:accIdList];
for(Account acc:accList){
acc.Name = acc.Name + '(' + acc.Auto_Number__c + ')';
}
update accList;
for(Parent__c f:fecList2){
if((f.Number_of_Years__c > 0) && (f.Grantor__c == null)){
Integer totalYears = (f.Number_of_Years__c).intValue();
for(integer i = 0; i < totalYears; i++){
tempfevent2.RecordTypeId = rectype[0].Id;
tempfevent2.Grantee__c = f.Grantee__c;
tempfevent2.Grantor__c = newAcc.get(f.Id).Id;
tempfevent2.Status__c = f.Status__c;
tempfevent2.Series__c = f.Series__c;
tempfevent2.Data__c = f.Data__c;
if(f.Amount__c!=NULL)
tempfevent2.Amount__c = f.Amount__c/totalYears ;
if(f.Fiscal_Year__c!= NULL)
tempfevent2.Fiscal_Year__c = String.valueof(Integer.valueof(f.Fiscal_Year__c)+i);
tempfevent2.Number_of_years_of_the_child__c = f.Number_of_years__c;
tempfevent2.Budget_Expenses__c = f.Budget_Expenses__c;
tempfevent2.Staff_Count__c = f.Staff_Count__c;
tempfevent2.Parent__c = f.Id;
newfevenList2.add(tempfevent2);
tempfevent2 = new child__c();
f.Grantor__c = newAcc.get(f.Id).Id;
}
}
}
insert newfevenList2;
update fecList2;
}
catch (Exception e) {
System.Debug('ERROR: ' + e);
}
}