+ Start a Discussion
avianna lopexavianna lopex 

Batch Job that takes data from Executive_RT custom object and populates it into Case Type Data custom object Bi weekly

Hi ,

Please help me in writing a Batch Job code and test class that takes data from fields  Case__c, level1__c,level2__c, level3__c from Executive_RT custom object  and
 populates it into below fields namely number__c, level1__c,level2__c, level3__c of Case Type Data custom object  
 
 I want this job to get executed Bi weekly

Thanks In advance
Avianna
Best Answer chosen by avianna lopex
SFDC_SaurabhSFDC_Saurabh
you can treat this as pseudo code :
global class batchExample implements Database.Batchable<sObject> {
  
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
        
        String query = 'SELECT Case__c,Case__r.name, level1__c,level2__c, level3__c FROM Executive_RT';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Executive_RT> exeList) {
       
        // process each batch of records
List<Case_Type_Data> listCTD = new List<Case_Type_Data>();
        
        for(Executive_RT exe : exeList)
        {        
           	listCTD.add(new Case_Type_Data(name=exe.Case__r.name,exe.level1__c=exe.level1__c,exe.level2__c=exe.level2__c,exe.level3__c=exe.level3__c));
        }
        try {
        	
            insert listCTD;
        
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
  }
}


You might want to run below code from Run Anonymous window
batchExample batch = new batchExample ();
String sch = '0 0 15 * *?';
String jobIDNew = system.schedule('Batch Scheduled', sch, batch);

All Answers

SFDC_SaurabhSFDC_Saurabh
you can treat this as pseudo code :
global class batchExample implements Database.Batchable<sObject> {
  
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
        
        String query = 'SELECT Case__c,Case__r.name, level1__c,level2__c, level3__c FROM Executive_RT';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Executive_RT> exeList) {
       
        // process each batch of records
List<Case_Type_Data> listCTD = new List<Case_Type_Data>();
        
        for(Executive_RT exe : exeList)
        {        
           	listCTD.add(new Case_Type_Data(name=exe.Case__r.name,exe.level1__c=exe.level1__c,exe.level2__c=exe.level2__c,exe.level3__c=exe.level3__c));
        }
        try {
        	
            insert listCTD;
        
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
  }
}


You might want to run below code from Run Anonymous window
batchExample batch = new batchExample ();
String sch = '0 0 15 * *?';
String jobIDNew = system.schedule('Batch Scheduled', sch, batch);
This was selected as the best answer
avianna lopexavianna lopex
Thank you,could you also provide the test class as well
SFDC_SaurabhSFDC_Saurabh
@isTest 
public class batchExampleTest 
{
    static testMethod void testMethod1() 
    {
        List<Executive_RT> lstExecutive_RT= new List<Executive_RT>();
        for(Integer i=0 ;i <200;i++)
        {
            Executive_RT rt = new Executive_RT();
            rt.Name ='Name'+i;
            lstLead.add(rt);
        }
        
        insert lstExecutive_RT;
        
        Test.startTest();

            batchExample obj = new batchExample();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();
    }
}

See if this helps you