0

I need to write batch class for inserting records to one standard object(case) to custome object. also if any record get updated, then it should also get updated in custom object also. i wrote below code for insert operation but don't know what nto do for update operation.

global class Batch_Tableau implements Database.Batchable <Sobject>,Database.Stateful {
    List<Case> Clist = new List<Case>();
    global Database.QueryLocator start(Database.BatchableContext BC){

       // String query = 'Select  ACS_Defect_Payslip_Counts__c,ContactEmail,Country__c,Course__c,CycleTimeTotal__c,CreatedDate,ContactFax,Contributor__c,ContactMobile,ActualCycleTimeHours__c,Contact_Work_Country__c,Contact_Work_Title__c,Contact_Work_City__c,Contact_via_Email__c,Contact_SBG__c,Contact_Region__c,Contact_Preferred_Name__c,Contact_EID__c,Contact_Email_Check__c,Contact_Center_Case__c,Completed_Checklist_Items__c,Checklist_Updated__c,ClosedDate,Class_Start_Date__c,Cloned__c,Closed_Within__c,Closed_Case_in_GMT__c, Acutal_Target_Time__c,Adela__c, Area_Text__c, AskHR_Status__c, AssignmentTimeHours__c,Checklist_Progress__c, Audit__c, Audit_Due_Date_Time__c, Audit_Due_In_days__c, Audit_Fail__c, Audit_Priority__c, Audit_Type__c, Auditor__c, Auditor_Assigned_On__c, Cancelled__c, Case_Age__c, Case_Close_Month__c, Case_Contact_Country__c, Case_Due_by_Day__c, Case_Due_by_Time__c, Case_Open_Month__c, Case_Reason__c, Case_Status__c, Case_Target__c, Case_Update_Alert__c, CaseAge__c, CaseNumber, CET_met__c,Checklist_generated__c,Date_Audited__c,Date_Committed__c,Date_Committed_Change_Reason__c  from case where createddate = system.today() AND (RecordType.Name not like \'%Finance%\' OR RecordType.Name not like \'%Decom%\')';          
        String query = 'Select id,ACS_Defect_Payslip_Counts__c,ContactEmail,Country__c,Course__c,CycleTimeTotal__c,CreatedDate,ContactFax,Contributor__c,ContactMobile,ActualCycleTimeHours__c,Contact_Work_Country__c,Contact_Work_Title__c,Contact_Work_City__c,Contact_via_Email__c,Contact_SBG__c,Contact_Region__c,Contact_Preferred_Name__c,Contact_EID__c,Contact_Email_Check__c,Contact_Center_Case__c,Completed_Checklist_Items__c,Checklist_Updated__c,ClosedDate,Class_Start_Date__c,Cloned__c,Closed_Within__c,Closed_Case_in_GMT__c, Acutal_Target_Time__c,Adela__c, Area_Text__c, AskHR_Status__c, AssignmentTimeHours__c,Checklist_Progress__c, Audit__c, Audit_Due_Date_Time__c, Audit_Due_In_days__c, Audit_Fail__c, Audit_Priority__c, Audit_Type__c, Auditor__c, Auditor_Assigned_On__c, Cancelled__c, Case_Age__c, Case_Close_Month__c, Case_Contact_Country__c, Case_Due_by_Day__c, Case_Due_by_Time__c, Case_Open_Month__c, Case_Reason__c, Case_Status__c, Case_Target__c, Case_Update_Alert__c, CaseAge__c, CaseNumber, CET_met__c,Checklist_generated__c,Date_Audited__c,Date_Committed__c,Date_Committed_Change_Reason__c  from case where createddate = system.today() AND (RecordType.Name not like \'%Finance%\' OR RecordType.Name not like \'%Decom%\')';
        return Database.getQueryLocator(query);

    }

    global void execute(Database.BatchableContext BC, List<Case> batch){

        List<Tablue__c> Tablist = new List<Tablue__c>();
        List<Tablue__c> query_tablue=new List<Tablue__c>();// change name
        Map<id,Tablue__c> case_id_to_tablue= new  Map<id,Tablue__c>();
        //query List of tablue.
         query_tablue=[Select id,case__c from Tablue__c where case__c in : batch];

        For(Case c:batch)
        {
            Tablue__c tab= new Tablue__c();
            //Tablist.add(tab.ACS_Defect_Payslip_Counts__c = c.ACS_Defect_Payslip_Counts__c);
            tab.Actual_Cycle_Time_Hours__c = c.ActualCycleTimeHours__c;
            tab.Actual_Target_Time__c = c.Acutal_Target_Time__c;
            tab.Adela__c = c.Adela__c;
            tab.Area_Text__c = c.Area_Text__c;
            tab.AskHR_Status__c = c.AskHR_Status__c;
            tab.Assignment_time_hours__c = c.AssignmentTimeHours__c;
            tab.Audit__c = c.Audit__c;
            tab.Audit_Due_Date_Time__c = c.Audit_Due_Date_Time__c;
            tab.Audit_Due_In_days__c = c.Audit_Due_In_days__c;
            tab.Audit_Fail__c = c.Audit_Fail__c;
            tab.Audit_Priority__c = c.Audit_Priority__c;
            tab.Audit_Type__c = c.Audit_Type__c;
            tab.Auditor__c = c.Auditor__c;
            tab.Auditor_Assigned_On__c = c.Auditor_Assigned_On__c;
            tab.Cancelled__c = c.Cancelled__c;
            tab.Case_Age__c = c.Case_Age__c;
            tab.Case_Close_Month__c = c.Case_Close_Month__c;
            tab.Case_Contact_Country__c = c.Case_Contact_Country__c;
            tab.Case_Due_by_Day__c = c.Case_Due_by_Day__c;
            tab.Case_Due_by_Time__c = c.Case_Due_by_Time__c;
            tab.Case_Open_Month__c = c.Case_Open_Month__c;
            tab.Case_Reason__c = c.Case_Reason__c;
            tab.Case_Status__c = c.Case_Status__c;
            tab.Case_Target__c = c.Case_Target__c;
            tab.Case_Update_Alert__c = c.Case_Update_Alert__c;
            tab.CaseAge__c = c.CaseAge__c;
            tab.Case_number__c = c.CaseNumber;
            tab.CET_met__c = c.CET_met__c;
            tab.Checklist_generated__c = c.Checklist_generated__c;
            tab.Checklist_Progress__c = c.Checklist_Progress__c;
            tab.Checklist_Updated__c =c.Checklist_Updated__c;
            tab.Class_Start_Date__c = c.Class_Start_Date__c;
            tab.Cloned__c =c.Cloned__c;
            tab.Closed_Date_in_GMT__c =c.Closed_Case_in_GMT__c;
            tab.Closed_Within__c = c.Closed_Within__c;
            tab.ClosedDate__c = c.ClosedDate;
            tab.Completed_Checklist_Items__c = c.Completed_Checklist_Items__c;
            tab.Contact_Center_Case__c = c.Contact_Center_Case__c;
            tab.Contact_EID__c = c.Contact_EID__c;
            tab.Contact_Email_Check__c = c.Contact_Email_Check__c;
            tab.Contact_Preferred_Name__c = c.Contact_Preferred_Name__c;
            tab.Contact_Region__c =c.Contact_Region__c;
            tab.Contact_SBG__c = c.Contact_SBG__c;
            tab.Contact_via_Email__c = c.Contact_via_Email__c;
            tab.Contact_Work_City__c =c.Contact_Work_City__c;
            tab.Contact_Work_Country__c = c.Contact_Work_Country__c;
            tab.Contact_Work_Title__c = c.Contact_Work_Title__c;
            tab.Contact_Email__c = c.ContactEmail;
            tab.Contact_Fax__c = c.ContactFax;
            tab.Contact_Mobile__c = c.ContactMobile;
            tab.Contributor__c =c.Contributor__c;
            tab.Country__c =c.Country__c;
            tab.Course__c =c.Course__c;
            tab.CreatedDate__c = c.CreatedDate;
            tab.CycleTimeTotal__c = c.CycleTimeTotal__c;
            tab.Date_Audited__c=c.Date_Audited__c;// added by me
            tab.Date_Committed__c=c.Date_Committed__c;
            tab.Date_Committed_Change_Reason__c=c.Date_Committed_Change_Reason__c;
            tab.Date_Committed_Change_Reason_Picklist__c=c.Date_Committed_Change_Reason_Picklist__c;
            tab.Date_Committed_Change_To__c=c.Date_Committed_Change_To__c;
            tab.Date_Committed_Date__c=c.Date_Committed_Date__c;
            tab.Date_Committed_Past_Due__c=c.Date_Committed_Past_Due__c;
            tab.Date_of_Meeting__c=c.Date_of_Meeting__c;
            tab.Days_Past_Target__c=c.Days_Past_Target__c;
            tab.Days_Remaining__c=c.Days_Remaining__c;
            tab.Days_Remaining_Formula__c=c.Days_Remaining_Formula__c;
            tab.Days_Remaining_NonVendorCases__c=c.Days_Remaining_NonVendorCases__c;
            tab.Default_Entitlement__c=c.Default_Entitlement__c;
tab.Defect_File_Feed_Date__c=c.Defect_File_Feed_Date__c;
            tab.Delayed_Payslips_Overall__c=c.Delayed_Payslips_Overall__c;
            tab.Description__c=c.Description;
            tab.Due_Date__c=c.Due_Date__c;
            tab.Effective_Date__c=c.Effective_Date__c;  
            tab.Effective_Date_of_Transaction__c=c.Effective_Date_of_Transaction__c;
            tab.Email_BCC_Address__c=c.Email_BCC_Address__c;
            tab.Email_Sent_Date__c=c.Email_Sent_Date__c;
            tab.Email_sent_Date_Time__c=c.Email_sent_Date_Time__c;
            tab.Email_Status__c=c.Email_Status__c;
            tab.Email_Subject__c=c.Email_Subject__c;
            tab.Email_To_Address__c=c.Email_To_Address__c;
            tab.Email_to_Case__c=c.Email_to_Case__c;
            tab.Employee__c=c.Employee__c;
            tab.Employee_EID__c=c.Employee_EID__c;
            tab.Employee_Email__c=c.Employee_Email__c;
            tab.Expected_Net_Working_Days__c=c.Expected_Net_Working_Days__c;
            tab.Fail_Reason__c=c.Fail_Reason__c;
            tab.FlowParent__c=c.FlowParent__c;
            tab.FRCS_Contact_Country__c=c.FRCS_Contact_Country__c;
            tab.Frequency__c=c.Frequency__c;
            tab.From_Address__c=c.From_Address__c;
            tab.From_Email_Address__c=c.From_Email_Address__c;
            tab.Function__c=c.Function__c;
            tab.Function_BDD__c=c.Function_BDD__c;
            tab.Function_MTS__c=c.Function_MTS__c;






            Tablist.add(tab);

        }

        insert Tablist;
    }



    global void finish(Database.BatchableContext BC){

}
  • Are you copying data from Case to a custom object? – rahul gawale Feb 11 at 7:43
0

you can develop the same using process builder on case object, where you can handle both insert and update. If you want to go with batch class only then, your start query should fetch both created or modified records

where createddate = system.today() OR lastmodifieddate = system.today()

and in your execute method, instead of adding into a list query_tablue, you can have a map<Id,Tablue__c > and in the for iteration make a check accordingly using map.containsKey() to insert or update

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.