+ Start a Discussion
Irish@acc.comIrish@acc.com 

INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call

Can somebody help me with the code m constanly getting this error

 


global class CreateQoDelivery implements Database.Batchable<sObject>,
Schedulable {

public String Query;
global CreateQoDelivery(){

String ProjectQuery = 'Select Id,Name,Dev_Lead_User__c,Project_Start_Date_original_baseline__c,Project_End_Date_original_baseline__c,Project_Start_Date_current_baseline__c,Project_End_Date_current_baseline__c,Project_Start_Date_actual__c,Project_End_Date_actual__c,Project_TQL_Phase__c,Project_State__c from Project__c where Overall_Project_Status__c NOT IN (\'Not Started\',\'On hold\') AND Project_TQL_Phase__c !=\'1. Kick-off\' AND Project_State__c IN(\'Active\')';

        
        
        this.Query=ProjectQuery;         
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(Query);
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope){
        List<Project__c> ProjectList =(Project__c[]) scope; 
                           
                        
                        
          List< Quality_of_Delivery__c> detailList = new List< Quality_of_Delivery__c>(); // to insert the record
          List< Quality_of_Delivery__c> detailListUpdate = new List< Quality_of_Delivery__c>(); // to upsert the record
      

        for(Project__c prj : ProjectList){
                      Quality_of_Delivery__c qodDel = new Quality_of_Delivery__c ();
           List< Quality_of_Delivery__c> LstRecord = new List< Quality_of_Delivery__c>();// new line
           lstRecord=[Select CDN__c,Working_Days_to_get_ROM__c,Slow_score__c,PageSpeed_score__c,Number_of_Change_Requests__c,Planned_Cost_current_baseline__c,Planned_Cost_original_baseline__c,Actual_Cost__c,Planned_Scope_originale_baseline__c,Planned_Scope_current_baseline__c,Actual_Scope__c,SIT_defects__c,Minor_UAT_Defects_TA__c,Minor_UAT_Defects_All__c,Major_UAT_Defects_TA__c,Major_UAT_Defects_All__c,Hypercare_Defects_All__c,Hypercare_Defects_TA__c,P_S_Compliance_after_Hypercare__c,P_S_Compliance_as_Launch__c from Quality_of_Delivery__C where Project__c=:Prj.id order by createdDate desc limit 1]; // checking the record in project…
               
                if (lstrecord!=null){             // if there a record in the list then copy the field values and creates a new record
                    for(Quality_of_Delivery__c q:lstRecord){
                                                 
                         qodDel.Project__c=prj.Id;
                         date currentDate = Date.today();
                         
                         Integer CurrentMonth = currentDate.month(); 
                         Integer  Currentyear=currentDate.year();
                         qodDel.Date__c = System.Today()-1;
                         qodDel.CDN__c = q.CDN__c;     //copying values for all the below fields from the latest created Record..
                         qodDel.Working_Days_to_get_ROM__c = q.Working_Days_to_get_ROM__c;                        
                         qodDel.Slow_score__c = q.Slow_score__c;
                         qodDel.PageSpeed_score__c = q.PageSpeed_score__c;
                         qodDel.Number_of_Change_Requests__c = q.Number_of_Change_Requests__c;
                         qodDel.Planned_Cost_current_baseline__c = q.Planned_Cost_current_baseline__c;
                         qodDel.Planned_Cost_original_baseline__c = q.Planned_Cost_original_baseline__c;
                         qodDel.Actual_Cost__c = q.Actual_Cost__c;
                         qodDel.Planned_Scope_originale_baseline__c = q.Planned_Scope_originale_baseline__c;
                         qodDel.Planned_Scope_current_baseline__c = q.Planned_Scope_current_baseline__c;
                         qodDel.Actual_Scope__c = q.Actual_Scope__c;
                         qodDel.SIT_defects__c = q.SIT_defects__c;
                         qodDel.Minor_UAT_Defects_TA__c = q.Minor_UAT_Defects_TA__c;
                         qodDel.Minor_UAT_Defects_All__c = q.Minor_UAT_Defects_All__c;
                         qodDel.Major_UAT_Defects_TA__c = q.Major_UAT_Defects_TA__c;
                         qodDel.Major_UAT_Defects_All__c = q.Major_UAT_Defects_All__c;
                         qodDel.Hypercare_Defects_All__c = q.Hypercare_Defects_All__c;
                         qodDel.Hypercare_Defects_TA__c = q.Hypercare_Defects_TA__c;
                         qodDel.P_S_Compliance_after_Hypercare__c = q.P_S_Compliance_after_Hypercare__c;
                         qodDel.P_S_Compliance_as_Launch__c = q.P_S_Compliance_as_Launch__c;
                         
                         qodDel.Apex_Context__c=true; // used to bypass validation rule on the record
                         
                        if(CurrentMonth==1){ // used for naming the record
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear-1)+'-'+String.valueof(CurrentMonth+11);
                        }
                        
                        else if(CurrentMonth==11 || CurrentMonth==12) {                        
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+String.valueof(CurrentMonth-1);
                        }
                        
                        else{
                         qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+'0'+String.valueof(CurrentMonth-1);
                         
                        }                        
                         qodDel.Dev_Lead__c=prj.Dev_Lead_User__c;  // lookup to user
                              
                            qodDel.Approved_Status__c ='Pending input from Dev Specialist';
                         
                         if(prj.Project_Start_Date_original_baseline__c!=null && prj.Project_End_Date_original_baseline__c!=null)
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c= prj.Project_Start_Date_original_baseline__c.daysBetween(prj.Project_End_Date_original_baseline__c);
                         }
                         else
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c=0;
                         }
                         if(prj.Project_Start_Date_current_baseline__c!=null && prj.Project_End_Date_current_baseline__c!=null)
                         {
                            qodDel.Planned_Working_Days_current_baseline__c=prj.Project_Start_Date_current_baseline__c.daysBetween(prj.Project_End_Date_current_baseline__c); 
                         }  
                        else
                        {
                            qodDel.Planned_Working_Days_current_baseline__c=0;
                        }
                        if(prj.Project_Start_Date_actual__c!=null && prj.Project_End_Date_actual__c !=null)
                        {
                        qodDel.Actual_Working_Days__c = prj.Project_Start_Date_actual__c.daysBetween(prj.Project_End_Date_actual__c);
                        }
                        else
                        {
                       qodDel.Actual_Working_Days__c=0;
                        }
    
                         
                    detailListUpdate.add(qodDel);
                    insert detailListUpdate;     
                   
           
                    }
                    }
               

                else   // if there is no record in the list of Qod then creates a new record in Project
                      {                                   
                         date currentDate = Date.today();
                         Integer CurrentMonth = currentDate.month(); 
                         Integer  Currentyear=currentDate.year();
                         qodDel.Date__c = System.Today()-1;   
                         
                         qodDel.Project__c=prj.Id;               // Id of the parent Record
                         
                        if(CurrentMonth==1){
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear-1)+'-'+String.valueof(CurrentMonth+11);
                        }
                        
                        else if(CurrentMonth==11 || CurrentMonth==12) {                        
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+String.valueof(CurrentMonth-1);
                        }
                        
                        else{
                         qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+'0'+String.valueof(CurrentMonth-1);
                         
                        }                        
                         qodDel.Dev_Lead__c=prj.Dev_Lead_User__c;
                              
                         qodDel.Number_of_Change_Requests__c =0;
                         qodDel.Approved_Status__c ='Pending input from Dev Specialist';
                         
                         if(prj.Project_Start_Date_original_baseline__c!=null && prj.Project_End_Date_original_baseline__c!=null)
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c= prj.Project_Start_Date_original_baseline__c.daysBetween(prj.Project_End_Date_original_baseline__c);
                         }
                         else
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c=0;
                         }
                         if(prj.Project_Start_Date_current_baseline__c!=null && prj.Project_End_Date_current_baseline__c!=null)
                         {
                            qodDel.Planned_Working_Days_current_baseline__c=prj.Project_Start_Date_current_baseline__c.daysBetween(prj.Project_End_Date_current_baseline__c); 
                         }  
                        else
                        {
                            qodDel.Planned_Working_Days_current_baseline__c=0;
                        }
                        if(prj.Project_Start_Date_actual__c!=null && prj.Project_End_Date_actual__c !=null)
                        {
                        qodDel.Actual_Working_Days__c = prj.Project_Start_Date_actual__c.daysBetween(prj.Project_End_Date_actual__c);
                        }
                        else
                        {
                       qodDel.Actual_Working_Days__c=0;
                        }
                         detailList.add(qodDel); 
                         insert detailList;
                        }                                                        
                                        
                     
        
    
    
}
}
global void finish(Database.BatchableContext BC){
}
  

                //Execute batch class with default constructor from schedular
    global void execute(SchedulableContext sc) {
        
        
       try {
    
         database.executeBatch(new CreateQoDelivery());

        }
        catch (Exception e) {
            System.debug('There are no jobs currently scheduled. ' + e.getMessage()); 
        } 
   }   
    
}

 thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma
global class CreateQoDelivery implements Database.Batchable<sObject>,
Schedulable {

public String Query;
global CreateQoDelivery(){

String ProjectQuery = 'Select Id,Name,Dev_Lead_User__c,Project_Start_Date_original_baseline__c,Project_End_Date_original_baseline__c,Project_Start_Date_current_baseline__c,Project_End_Date_current_baseline__c,Project_Start_Date_actual__c,Project_End_Date_actual__c,Project_TQL_Phase__c,Project_State__c from Project__c where Overall_Project_Status__c NOT IN (\'Not Started\',\'On hold\') AND Project_TQL_Phase__c !=\'1. Kick-off\' AND Project_State__c IN(\'Active\')';

        
        
        this.Query=ProjectQuery;         
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(Query);
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope){
        List<Project__c> ProjectList =(Project__c[]) scope; 
                           
                        
                        
          List< Quality_of_Delivery__c> detailList = new List< Quality_of_Delivery__c>(); // to insert the record
          List< Quality_of_Delivery__c> detailListUpdate = new List< Quality_of_Delivery__c>(); // to upsert the record
      

        for(Project__c prj : ProjectList){
                      Quality_of_Delivery__c qodDel = new Quality_of_Delivery__c ();
           List< Quality_of_Delivery__c> LstRecord = new List< Quality_of_Delivery__c>();// new line
           lstRecord=[Select CDN__c,Working_Days_to_get_ROM__c,Slow_score__c,PageSpeed_score__c,Number_of_Change_Requests__c,Planned_Cost_current_baseline__c,Planned_Cost_original_baseline__c,Actual_Cost__c,Planned_Scope_originale_baseline__c,Planned_Scope_current_baseline__c,Actual_Scope__c,SIT_defects__c,Minor_UAT_Defects_TA__c,Minor_UAT_Defects_All__c,Major_UAT_Defects_TA__c,Major_UAT_Defects_All__c,Hypercare_Defects_All__c,Hypercare_Defects_TA__c,P_S_Compliance_after_Hypercare__c,P_S_Compliance_as_Launch__c from Quality_of_Delivery__C where Project__c=:Prj.id order by createdDate desc limit 1]; // checking the record in project…
               
                if (!lstrecord.isEmpty()){             // if there a record in the list then copy the field values and creates a new record
                    for(Quality_of_Delivery__c q:lstRecord){
                         qodDel = new Quality_of_Delivery__c ();                        
                         qodDel.Project__c=prj.Id;
                         date currentDate = Date.today();
                         
                         Integer CurrentMonth = currentDate.month(); 
                         Integer  Currentyear=currentDate.year();
                         qodDel.Date__c = System.Today()-1;
                         qodDel.CDN__c = q.CDN__c;     //copying values for all the below fields from the latest created Record..
                         qodDel.Working_Days_to_get_ROM__c = q.Working_Days_to_get_ROM__c;                        
                         qodDel.Slow_score__c = q.Slow_score__c;
                         qodDel.PageSpeed_score__c = q.PageSpeed_score__c;
                         qodDel.Number_of_Change_Requests__c = q.Number_of_Change_Requests__c;
                         qodDel.Planned_Cost_current_baseline__c = q.Planned_Cost_current_baseline__c;
                         qodDel.Planned_Cost_original_baseline__c = q.Planned_Cost_original_baseline__c;
                         qodDel.Actual_Cost__c = q.Actual_Cost__c;
                         qodDel.Planned_Scope_originale_baseline__c = q.Planned_Scope_originale_baseline__c;
                         qodDel.Planned_Scope_current_baseline__c = q.Planned_Scope_current_baseline__c;
                         qodDel.Actual_Scope__c = q.Actual_Scope__c;
                         qodDel.SIT_defects__c = q.SIT_defects__c;
                         qodDel.Minor_UAT_Defects_TA__c = q.Minor_UAT_Defects_TA__c;
                         qodDel.Minor_UAT_Defects_All__c = q.Minor_UAT_Defects_All__c;
                         qodDel.Major_UAT_Defects_TA__c = q.Major_UAT_Defects_TA__c;
                         qodDel.Major_UAT_Defects_All__c = q.Major_UAT_Defects_All__c;
                         qodDel.Hypercare_Defects_All__c = q.Hypercare_Defects_All__c;
                         qodDel.Hypercare_Defects_TA__c = q.Hypercare_Defects_TA__c;
                         qodDel.P_S_Compliance_after_Hypercare__c = q.P_S_Compliance_after_Hypercare__c;
                         qodDel.P_S_Compliance_as_Launch__c = q.P_S_Compliance_as_Launch__c;
                         
                         qodDel.Apex_Context__c=true; // used to bypass validation rule on the record
                         
                        if(CurrentMonth==1){ // used for naming the record
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear-1)+'-'+String.valueof(CurrentMonth+11);
                        }
                        
                        else if(CurrentMonth==11 || CurrentMonth==12) {                        
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+String.valueof(CurrentMonth-1);
                        }
                        
                        else{
                         qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+'0'+String.valueof(CurrentMonth-1);
                         
                        }                        
                         qodDel.Dev_Lead__c=prj.Dev_Lead_User__c;  // lookup to user
                              
                            qodDel.Approved_Status__c ='Pending input from Dev Specialist';
                         
                         if(prj.Project_Start_Date_original_baseline__c!=null && prj.Project_End_Date_original_baseline__c!=null)
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c= prj.Project_Start_Date_original_baseline__c.daysBetween(prj.Project_End_Date_original_baseline__c);
                         }
                         else
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c=0;
                         }
                         if(prj.Project_Start_Date_current_baseline__c!=null && prj.Project_End_Date_current_baseline__c!=null)
                         {
                            qodDel.Planned_Working_Days_current_baseline__c=prj.Project_Start_Date_current_baseline__c.daysBetween(prj.Project_End_Date_current_baseline__c); 
                         }  
                        else
                        {
                            qodDel.Planned_Working_Days_current_baseline__c=0;
                        }
                        if(prj.Project_Start_Date_actual__c!=null && prj.Project_End_Date_actual__c !=null)
                        {
                        qodDel.Actual_Working_Days__c = prj.Project_Start_Date_actual__c.daysBetween(prj.Project_End_Date_actual__c);
                        }
                        else
                        {
                       qodDel.Actual_Working_Days__c=0;
                        }
    
                         
                    detailListUpdate.add(qodDel);
                    // insert detailListUpdate;     
                   
           
                    }
                    }
               

                else   // if there is no record in the list of Qod then creates a new record in Project
                      {      
		         qodDel = new Quality_of_Delivery__c ();
                         date currentDate = Date.today();
                         Integer CurrentMonth = currentDate.month(); 
                         Integer  Currentyear=currentDate.year();
                         qodDel.Date__c = System.Today()-1;   
                         
                         qodDel.Project__c=prj.Id;               // Id of the parent Record
                         
                        if(CurrentMonth==1){
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear-1)+'-'+String.valueof(CurrentMonth+11);
                        }
                        
                        else if(CurrentMonth==11 || CurrentMonth==12) {                        
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+String.valueof(CurrentMonth-1);
                        }
                        
                        else{
                         qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+'0'+String.valueof(CurrentMonth-1);
                         
                        }                        
                         qodDel.Dev_Lead__c=prj.Dev_Lead_User__c;
                              
                         qodDel.Number_of_Change_Requests__c =0;
                         qodDel.Approved_Status__c ='Pending input from Dev Specialist';
                         
                         if(prj.Project_Start_Date_original_baseline__c!=null && prj.Project_End_Date_original_baseline__c!=null)
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c= prj.Project_Start_Date_original_baseline__c.daysBetween(prj.Project_End_Date_original_baseline__c);
                         }
                         else
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c=0;
                         }
                         if(prj.Project_Start_Date_current_baseline__c!=null && prj.Project_End_Date_current_baseline__c!=null)
                         {
                            qodDel.Planned_Working_Days_current_baseline__c=prj.Project_Start_Date_current_baseline__c.daysBetween(prj.Project_End_Date_current_baseline__c); 
                         }  
                        else
                        {
                            qodDel.Planned_Working_Days_current_baseline__c=0;
                        }
                        if(prj.Project_Start_Date_actual__c!=null && prj.Project_End_Date_actual__c !=null)
                        {
                        qodDel.Actual_Working_Days__c = prj.Project_Start_Date_actual__c.daysBetween(prj.Project_End_Date_actual__c);
                        }
                        else
                        {
                       qodDel.Actual_Working_Days__c=0;
                        }
                         detailList.add(qodDel);                          // insert detailList;
                        }                                                        
                                        
                     
        
    
    
}
if(!detailListUpdate.isEmpty()) {
	insert detailListUpdate; 
}
if(!detailList.isEmpty()) {
	insert detailList; 
}

}
global void finish(Database.BatchableContext BC){
}
  

                //Execute batch class with default constructor from schedular
    global void execute(SchedulableContext sc) {
        
        
       try {
    
         database.executeBatch(new CreateQoDelivery());

        }
        catch (Exception e) {
            System.debug('There are no jobs currently scheduled. ' + e.getMessage()); 
        } 
   }   
    
}

 

 I think you need to refresh the qodDel object each time. Check the highlighted code.

This error occurs when you try to insert the record having Id attibute in it.

 

Also the actual reason is you are inserting each record inside for loop which is already inserted in previous iteration.

 

So you need to insert it out side of for loop.

All Answers

Rahul SharmaRahul Sharma
global class CreateQoDelivery implements Database.Batchable<sObject>,
Schedulable {

public String Query;
global CreateQoDelivery(){

String ProjectQuery = 'Select Id,Name,Dev_Lead_User__c,Project_Start_Date_original_baseline__c,Project_End_Date_original_baseline__c,Project_Start_Date_current_baseline__c,Project_End_Date_current_baseline__c,Project_Start_Date_actual__c,Project_End_Date_actual__c,Project_TQL_Phase__c,Project_State__c from Project__c where Overall_Project_Status__c NOT IN (\'Not Started\',\'On hold\') AND Project_TQL_Phase__c !=\'1. Kick-off\' AND Project_State__c IN(\'Active\')';

        
        
        this.Query=ProjectQuery;         
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(Query);
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope){
        List<Project__c> ProjectList =(Project__c[]) scope; 
                           
                        
                        
          List< Quality_of_Delivery__c> detailList = new List< Quality_of_Delivery__c>(); // to insert the record
          List< Quality_of_Delivery__c> detailListUpdate = new List< Quality_of_Delivery__c>(); // to upsert the record
      

        for(Project__c prj : ProjectList){
                      Quality_of_Delivery__c qodDel = new Quality_of_Delivery__c ();
           List< Quality_of_Delivery__c> LstRecord = new List< Quality_of_Delivery__c>();// new line
           lstRecord=[Select CDN__c,Working_Days_to_get_ROM__c,Slow_score__c,PageSpeed_score__c,Number_of_Change_Requests__c,Planned_Cost_current_baseline__c,Planned_Cost_original_baseline__c,Actual_Cost__c,Planned_Scope_originale_baseline__c,Planned_Scope_current_baseline__c,Actual_Scope__c,SIT_defects__c,Minor_UAT_Defects_TA__c,Minor_UAT_Defects_All__c,Major_UAT_Defects_TA__c,Major_UAT_Defects_All__c,Hypercare_Defects_All__c,Hypercare_Defects_TA__c,P_S_Compliance_after_Hypercare__c,P_S_Compliance_as_Launch__c from Quality_of_Delivery__C where Project__c=:Prj.id order by createdDate desc limit 1]; // checking the record in project…
               
                if (!lstrecord.isEmpty()){             // if there a record in the list then copy the field values and creates a new record
                    for(Quality_of_Delivery__c q:lstRecord){
                         qodDel = new Quality_of_Delivery__c ();                        
                         qodDel.Project__c=prj.Id;
                         date currentDate = Date.today();
                         
                         Integer CurrentMonth = currentDate.month(); 
                         Integer  Currentyear=currentDate.year();
                         qodDel.Date__c = System.Today()-1;
                         qodDel.CDN__c = q.CDN__c;     //copying values for all the below fields from the latest created Record..
                         qodDel.Working_Days_to_get_ROM__c = q.Working_Days_to_get_ROM__c;                        
                         qodDel.Slow_score__c = q.Slow_score__c;
                         qodDel.PageSpeed_score__c = q.PageSpeed_score__c;
                         qodDel.Number_of_Change_Requests__c = q.Number_of_Change_Requests__c;
                         qodDel.Planned_Cost_current_baseline__c = q.Planned_Cost_current_baseline__c;
                         qodDel.Planned_Cost_original_baseline__c = q.Planned_Cost_original_baseline__c;
                         qodDel.Actual_Cost__c = q.Actual_Cost__c;
                         qodDel.Planned_Scope_originale_baseline__c = q.Planned_Scope_originale_baseline__c;
                         qodDel.Planned_Scope_current_baseline__c = q.Planned_Scope_current_baseline__c;
                         qodDel.Actual_Scope__c = q.Actual_Scope__c;
                         qodDel.SIT_defects__c = q.SIT_defects__c;
                         qodDel.Minor_UAT_Defects_TA__c = q.Minor_UAT_Defects_TA__c;
                         qodDel.Minor_UAT_Defects_All__c = q.Minor_UAT_Defects_All__c;
                         qodDel.Major_UAT_Defects_TA__c = q.Major_UAT_Defects_TA__c;
                         qodDel.Major_UAT_Defects_All__c = q.Major_UAT_Defects_All__c;
                         qodDel.Hypercare_Defects_All__c = q.Hypercare_Defects_All__c;
                         qodDel.Hypercare_Defects_TA__c = q.Hypercare_Defects_TA__c;
                         qodDel.P_S_Compliance_after_Hypercare__c = q.P_S_Compliance_after_Hypercare__c;
                         qodDel.P_S_Compliance_as_Launch__c = q.P_S_Compliance_as_Launch__c;
                         
                         qodDel.Apex_Context__c=true; // used to bypass validation rule on the record
                         
                        if(CurrentMonth==1){ // used for naming the record
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear-1)+'-'+String.valueof(CurrentMonth+11);
                        }
                        
                        else if(CurrentMonth==11 || CurrentMonth==12) {                        
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+String.valueof(CurrentMonth-1);
                        }
                        
                        else{
                         qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+'0'+String.valueof(CurrentMonth-1);
                         
                        }                        
                         qodDel.Dev_Lead__c=prj.Dev_Lead_User__c;  // lookup to user
                              
                            qodDel.Approved_Status__c ='Pending input from Dev Specialist';
                         
                         if(prj.Project_Start_Date_original_baseline__c!=null && prj.Project_End_Date_original_baseline__c!=null)
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c= prj.Project_Start_Date_original_baseline__c.daysBetween(prj.Project_End_Date_original_baseline__c);
                         }
                         else
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c=0;
                         }
                         if(prj.Project_Start_Date_current_baseline__c!=null && prj.Project_End_Date_current_baseline__c!=null)
                         {
                            qodDel.Planned_Working_Days_current_baseline__c=prj.Project_Start_Date_current_baseline__c.daysBetween(prj.Project_End_Date_current_baseline__c); 
                         }  
                        else
                        {
                            qodDel.Planned_Working_Days_current_baseline__c=0;
                        }
                        if(prj.Project_Start_Date_actual__c!=null && prj.Project_End_Date_actual__c !=null)
                        {
                        qodDel.Actual_Working_Days__c = prj.Project_Start_Date_actual__c.daysBetween(prj.Project_End_Date_actual__c);
                        }
                        else
                        {
                       qodDel.Actual_Working_Days__c=0;
                        }
    
                         
                    detailListUpdate.add(qodDel);
                    // insert detailListUpdate;     
                   
           
                    }
                    }
               

                else   // if there is no record in the list of Qod then creates a new record in Project
                      {      
		         qodDel = new Quality_of_Delivery__c ();
                         date currentDate = Date.today();
                         Integer CurrentMonth = currentDate.month(); 
                         Integer  Currentyear=currentDate.year();
                         qodDel.Date__c = System.Today()-1;   
                         
                         qodDel.Project__c=prj.Id;               // Id of the parent Record
                         
                        if(CurrentMonth==1){
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear-1)+'-'+String.valueof(CurrentMonth+11);
                        }
                        
                        else if(CurrentMonth==11 || CurrentMonth==12) {                        
                        qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+String.valueof(CurrentMonth-1);
                        }
                        
                        else{
                         qodDel.Name=prj.Name+' - '+String.valueof(Currentyear)+'-'+'0'+String.valueof(CurrentMonth-1);
                         
                        }                        
                         qodDel.Dev_Lead__c=prj.Dev_Lead_User__c;
                              
                         qodDel.Number_of_Change_Requests__c =0;
                         qodDel.Approved_Status__c ='Pending input from Dev Specialist';
                         
                         if(prj.Project_Start_Date_original_baseline__c!=null && prj.Project_End_Date_original_baseline__c!=null)
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c= prj.Project_Start_Date_original_baseline__c.daysBetween(prj.Project_End_Date_original_baseline__c);
                         }
                         else
                         {
                         qodDel.Planned_Working_Days_Original_baseline__c=0;
                         }
                         if(prj.Project_Start_Date_current_baseline__c!=null && prj.Project_End_Date_current_baseline__c!=null)
                         {
                            qodDel.Planned_Working_Days_current_baseline__c=prj.Project_Start_Date_current_baseline__c.daysBetween(prj.Project_End_Date_current_baseline__c); 
                         }  
                        else
                        {
                            qodDel.Planned_Working_Days_current_baseline__c=0;
                        }
                        if(prj.Project_Start_Date_actual__c!=null && prj.Project_End_Date_actual__c !=null)
                        {
                        qodDel.Actual_Working_Days__c = prj.Project_Start_Date_actual__c.daysBetween(prj.Project_End_Date_actual__c);
                        }
                        else
                        {
                       qodDel.Actual_Working_Days__c=0;
                        }
                         detailList.add(qodDel);                          // insert detailList;
                        }                                                        
                                        
                     
        
    
    
}
if(!detailListUpdate.isEmpty()) {
	insert detailListUpdate; 
}
if(!detailList.isEmpty()) {
	insert detailList; 
}

}
global void finish(Database.BatchableContext BC){
}
  

                //Execute batch class with default constructor from schedular
    global void execute(SchedulableContext sc) {
        
        
       try {
    
         database.executeBatch(new CreateQoDelivery());

        }
        catch (Exception e) {
            System.debug('There are no jobs currently scheduled. ' + e.getMessage()); 
        } 
   }   
    
}

 

 I think you need to refresh the qodDel object each time. Check the highlighted code.

This error occurs when you try to insert the record having Id attibute in it.

 

Also the actual reason is you are inserting each record inside for loop which is already inserted in previous iteration.

 

So you need to insert it out side of for loop.

This was selected as the best answer
Irish@acc.comIrish@acc.com
Hi Rahul...
Thank you very much it works like a charm...