+ Start a Discussion
Irish@accIrish@acc 

Schedular Class, need help

HI all,

 

I have written a schedualr class which will run once in a month, the requiremt is in the first month it runs it creates a record in child object and the all the field in this record will be filled manually.....

 

in the 2nd months it will run and  creates a new record but it field value must be copy from the 1st created record and in the third month when it run it again creates a new record and it will copy the filed value from 2nd created record and so on...

we can sort it based on the created date and then we can copy the field value...here is my code..

 

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>();
          
      

        for(Project__c prj : ProjectList){
           // create Master Rec for use as Detail Rec parent reference
           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,date__c,Name from Quality_of_Delivery__C where Project__c=:Prj.id AND final__c=False order by date__c limit 1];//ne line
                if(lstRecord==null){
                                    

                         
                         qodDel.Date__c = System.Today()-1;
                         qodDel.Project__c=prj.Id;                        
                        
                        qodDel.Name=prj.Name;
                        
                        
                        
                         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';
                         qodDel.Planned_Working_Days_Original_baseline__c= prj.Project_Start_Date_original_baseline__c.daysBetween(prj.Project_End_Date_original_baseline__c);
                         qodDel.Planned_Working_Days_current_baseline__c=prj.Project_Start_Date_current_baseline__c.daysBetween(prj.Project_End_Date_current_baseline__c); 
                         qodDel.Actual_Working_Days__c = prj.Project_Start_Date_actual__c.daysBetween(prj.Project_End_Date_actual__c);
                        }
                     
                        else{
                    for(Quality_of_Delivery__c q:lstRecord){
                    	 qodDel = q.Clone();
						 qodDel.Name=prj.Name;
                         qodDel.Project__c=prj.Id;                         
                         qodDel.Date__c = System.Today()-1;
                         qodDel.Project__c=prj.Id;                                             
                    }
           }
         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()); 
        } 
   }   
    
}

 but m getting this error...please help me on this ...

First error: Insert failed. First exception on row 0 with id a1Pe00000002FxoEAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Jeff MayJeff May

If you are updating an existing record, you should us update instead of insert.  If you have a mix of both, you can use upsert.