+ Start a Discussion
kmorf_entransformkmorf_entransform 

Error while inserting a record.

Hi i dont know why but when i try to insert more than one record I get an error saying Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ET.AutoCreateSchedule: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id a06A000000FrtPEIAZ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Trigger.ET.AutoCreateSchedule: line 26, column 5: []

If any one can help with this it would be great here is the code for the trigger

 

trigger AutoCreateSchedules on Request__c (after insert) {
List<Schedule__c> schedules = new List<Schedule__c>();
date nextScheduleStart;
//Integer sprintDuration;
    
// For each request processed by the trigger, add a set of 
// schedules to cover the start and end dates of the request.
// New is a list of all the new requests that are being
// created.
 
for (Request__c newRequest: Trigger.New) {
   if (newRequest.Autocreate_Schedule__c == 'Auto'){
   //sprintDuration = math.round(newRelease.Phase_duration__c);
        nextScheduleStart = newRequest.Start_Date__c;
        while (nextScheduleStart < newRequest.End_Date__c){
        schedules.add(new Schedule__c(
             Name = (nextScheduleStart.toStartOfMonth()).format(),
             Request__c = newRequest.Id,
             Month__c = nextScheduleStart.toStartOfMonth(),
             Demand_Hrs__c = ( newRequest.Monthly_Work_Hrs__c * newRequest.Request_Allocation__c)
             ));         
             
        nextScheduleStart = nextScheduleStart.addDays(31);     
        }
   
    insert schedules;
    }      
   }          
}

 



 

Rahul SharmaRahul Sharma

Hello,

 

You are performing dml statements inside a for loop, 

For the first record it will insert properly, then for the second time second record plus the first would be inserted.

I think that is causing the exception,

 

Try with inserting it outside the for loop:

trigger AutoCreateSchedules on Request__c (after insert) {
List<Schedule__c> schedules = new List<Schedule__c>();
date nextScheduleStart;
//Integer sprintDuration;
    
// For each request processed by the trigger, add a set of 
// schedules to cover the start and end dates of the request.
// New is a list of all the new requests that are being
// created.
 
for (Request__c newRequest: Trigger.New) {
   if (newRequest.Autocreate_Schedule__c == 'Auto'){
   //sprintDuration = math.round(newRelease.Phase_duration__c);
        nextScheduleStart = newRequest.Start_Date__c;
        while (nextScheduleStart < newRequest.End_Date__c){
        schedules.add(new Schedule__c(
             Name = (nextScheduleStart.toStartOfMonth()).format(),
             Request__c = newRequest.Id,
             Month__c = nextScheduleStart.toStartOfMonth(),
             Demand_Hrs__c = ( newRequest.Monthly_Work_Hrs__c * newRequest.Request_Allocation__c)
             ));         
             
        nextScheduleStart = nextScheduleStart.addDays(31);     
        }
    }      
   }       
 if(!schedules.isEmpty()) insert schedules;
}

 Hope it helps.