+ Start a Discussion
Cris9931Cris9931 

how can i convert this code sample in a schedule batch class?

Hello, can anyone give me a help with a batch class? I have this code which is working fine in my developer console if I run it in anonymous ..
 
List<SVMXC__Service_Order_Line__c> workDetails = new List<SVMXC__Service_Order_Line__c>(); 
Set<Id> techIds = new Set<Id>(); 
//Last Run Oct 8 
DateTime refDate = DateTime.newInstance(2019, 10, 01);
DateTime EndDateInterval =  refDate.AddDays(7);
 
for(SVMXC__Service_Order_Line__c wd : [SELECT Id, SVMXC__Group_Member__r.SVMXC__Salesforce_User__c,SVMXC__Start_Date_and_Time__c, SVMXC__End_Date_and_Time__c,SVMX_Duration__c, SVMXC__Line_Type__c,                                        SIG_Activity_type__c, SIG_Ship_To__c, 
                                      (SELECT Id FROM SVMXC__Time_Entrys__r)  FROM SVMXC__Service_Order_Line__c            
		                               WHERE (SVMXC__Line_Type__c='Labor' OR SVMXC__Line_Type__c='Travel')           
									         AND SVMXC__Group_Member__c!=null AND SVMXC__Line_Status__c = 'Processed'           
											 AND (SVMXC__Start_Date_and_Time__c >= :refDate AND SVMXC__Start_Date_and_Time__c <= :EndDateInterval)]) 
   {     
      if(wd.SVMXC__Time_Entrys__r.size() == 0) 
	  {   
	     workDetails.add(wd);   
		 techIds.add(wd.SVMXC__Group_Member__r.SVMXC__Salesforce_User__c);  
	  } 
   } 
   
   
System.debug('### workDetails : ' + workDetails.size());

 
 
Map<String, SVMXC__Timesheet_Day_Entry__c> dayEntries = new Map<String, SVMXC__Timesheet_Day_Entry__c>(); 
 
for(SVMXC__Timesheet_Day_Entry__c tde : [SELECT Id, SVMXC__Timesheet__c, SVMXC__Timesheet__r.SVMXC__User__c, SVMXC__Timsheet_Day__c FROM SVMXC__Timesheet_Day_Entry__c WHERE SVMXC__Timesheet__r.SVMXC__User__c IN :techIds]) 
   {  
       String key = '' + tde.SVMXC__Timesheet__r.SVMXC__User__c + tde.SVMXC__Timsheet_Day__c;  
	   dayEntries.put(key, tde); 
   } 
   
System.debug('### dayEntries : ' + dayEntries.size()); 
 
 
List<SVMXC__Timesheet_Entry__c> timeEntries = new List<SVMXC__Timesheet_Entry__c>(); 


for(SVMXC__Service_Order_Line__c wd : workDetails)  
   {  
       String key = '' + wd.SVMXC__Group_Member__r.SVMXC__Salesforce_User__c + wd.SVMXC__Start_Date_and_Time__c.date();  
	   SVMXC__Timesheet_Day_Entry__c tde = dayEntries.get(key);  
	   
	   if(tde != null)   
	       {   SVMXC__Timesheet_Entry__c timeEntry = new SVMXC__Timesheet_Entry__c(); 
               timeEntry.SVMXC__Timesheet__c = tde.SVMXC__Timesheet__c;   timeEntry.SVMXC__Timesheet_Day__c = tde.Id;   
			   timeEntry.SVMXC__Work_Detail__c = wd.Id;   
			   timeEntry.SVMXC__Start_Time__c = wd.SVMXC__Start_Date_and_Time__c;   
			   timeEntry.SVMXC__End_Time__c = wd.SVMXC__End_Date_and_Time__c;   
			   timeEntry.SVMXC__Comments__c = wd.SIG_Activity_type__c;   
			   timeEntry.SIG_Is_Billable__c  = true;         
			   timeEntry.SVMXC__Account__c  = wd.SIG_Ship_To__c;         
			   timeEntry.SVMXC__Duration__c = wd.SVMX_Duration__c / 60;   
			   timeEntries.add(timeEntry);  
		   } 
   } 
 
System.debug('### timeEntries : ' + timeEntries.size()); 
 
if(timeEntries.size() > 0)  
insert timeEntries;

 
SwethaSwetha (Salesforce Developers) 
Hi Cris,
Recommend reviewing below posts to get started https://salesforce.stackexchange.com/questions/322233/convert-apex-method-to-batch

https://salesforce.stackexchange.com/questions/132504/help-in-converting-my-apex-class-to-batch-job

https://salesforce.stackexchange.com/questions/253095/how-to-convert-the-apex-class-into-batch-apex-class

If this information helps, please mark the answer as best. Thank you