• AshwinKumar123
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Total number of batches are getting reduced. Without any error out of 3300 batches 152 have got processed. Now all of a sudden total number of batches is getting reduced.

This is a follow up question on this one : https://developer.salesforce.com/forums/ForumsMain?id=9062I000000XygVQAS
Hi All,
I need to update new fields value for 10 million campaign member records. I have written a batch class for it and it would take around 5000 batches to update all records.

Noticed that each batch is taking around 2 mins to run. Would like to know if there is a way I could optimize below code.

global class batchclass implements Database.Batchable<sObject>,Database.stateful {
    global set<id> allIds=new set<id>();
    set<Id> failIds = new set<Id>();
    global set<id> allFailedId=new set<id>();
    global map<id,string> errormsgMap =  new map<id,String>();
    public List<Exception__c> exceptionLists = new List<Exception__c>();
    global set<id> allSuccessId=new set<id>();
    private String StringQuery;
    public batchclass(String strQuery,set<Id> failedIds) {
        StringQuery = strQuery;
        failIds = failedIds;
    }
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(StringQuery);
    }
    
    global void execute(Database.BatchableContext BC, List<CampaignMember> scope) {
        List<CampaignMember> updateCampList = New List<CampaignMember>();
        map<string,double> campmatrxMap = new map<string,double>();
        List<Campaign_Member_Status_Matrix__mdt> campMatrixList = [SELECT id,Order__c,MasterLabel FROM Campaign_Member_Status_Matrix__mdt];
        for(Campaign_Member_Status_Matrix__mdt mtrx: campMatrixList){
            campmatrxMap.put(mtrx.MasterLabel,mtrx.Order__c);       
        }
        for(CampaignMember cmpMember: scope){
            cmpMember.CampaignAccountId__c = (cmpMember.type=='contact')?string.valueof(cmpMember.campaignId)+string.valueof(cmpMember.contact.AccountId) : string.valueof(cmpMember.campaignId)+string.valueof(cmpMember.CompanyOrAccount).toLowerCase();
            cmpMember.Status_Priority__c = (campmatrxMap.containsKey(cmpMember.Status)!=NULL)?campmatrxMap.get(cmpMember.Status):0;
            updateCampList.add(cmpMember);
            allIds.add(cmpMember.Id);
        }
        if (!updateCampList.isempty()) {
            Constants.disableCampaignmembertrigger = true;
            database.SaveResult[] myResult=database.update(updateCampList,false);
            Constants.disableCampaignmembertrigger = false;
            for(integer i=0;i<myResult.size();i++){
                if(myResult.get(i).isSuccess()){
                    allSuccessId.add(myResult.get(i).getID());    
                }else{
                    Database.error error =  myResult.get(i).getErrors().get(0);
                    string errMsg = error.getMessage();
                    errormsgMap.put(updateCampList.get(i).id,errMsg);
                }
                
            }
            
        }
    }
    
    global void finish(Database.BatchableContext BC) {
        for(Id ids :allIds){
            if(!allSuccessId.contains(ids)){
                allFailedId.add(ids); 
            }   
        }
        for(Id recrdId : errormsgMap.keyset()){
            Exception__c exe = new Exception__c();
            exe.Exception_Class_Name__c = 'batchclass';
            exe.Exception_Method_Name__c = 'Execute';
            exe.Exception_Details__c = recrdId+': '+errormsgMap.get(recrdId);
            exceptionLists.add(exe);
        }
        if(!exceptionLists.isEmpty()){
            insert exceptionLists;    
        }
        integer successSize = allSuccessId.size();
        integer FailedSize = allFailedId.size();
        String FailedIds = 'SuccessCount'+successSize +'FailedRecordCount'+FailedSize;
    }
    
}
Hi All,
I need to update new fields value for 10 million campaign member records. I have written a batch class for it and it would take around 5000 batches to update all records.

Noticed that each batch is taking around 2 mins to run. Would like to know if there is a way I could optimize below code.

global class batchclass implements Database.Batchable<sObject>,Database.stateful {
    global set<id> allIds=new set<id>();
    set<Id> failIds = new set<Id>();
    global set<id> allFailedId=new set<id>();
    global map<id,string> errormsgMap =  new map<id,String>();
    public List<Exception__c> exceptionLists = new List<Exception__c>();
    global set<id> allSuccessId=new set<id>();
    private String StringQuery;
    public batchclass(String strQuery,set<Id> failedIds) {
        StringQuery = strQuery;
        failIds = failedIds;
    }
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(StringQuery);
    }
    
    global void execute(Database.BatchableContext BC, List<CampaignMember> scope) {
        List<CampaignMember> updateCampList = New List<CampaignMember>();
        map<string,double> campmatrxMap = new map<string,double>();
        List<Campaign_Member_Status_Matrix__mdt> campMatrixList = [SELECT id,Order__c,MasterLabel FROM Campaign_Member_Status_Matrix__mdt];
        for(Campaign_Member_Status_Matrix__mdt mtrx: campMatrixList){
            campmatrxMap.put(mtrx.MasterLabel,mtrx.Order__c);       
        }
        for(CampaignMember cmpMember: scope){
            cmpMember.CampaignAccountId__c = (cmpMember.type=='contact')?string.valueof(cmpMember.campaignId)+string.valueof(cmpMember.contact.AccountId) : string.valueof(cmpMember.campaignId)+string.valueof(cmpMember.CompanyOrAccount).toLowerCase();
            cmpMember.Status_Priority__c = (campmatrxMap.containsKey(cmpMember.Status)!=NULL)?campmatrxMap.get(cmpMember.Status):0;
            updateCampList.add(cmpMember);
            allIds.add(cmpMember.Id);
        }
        if (!updateCampList.isempty()) {
            Constants.disableCampaignmembertrigger = true;
            database.SaveResult[] myResult=database.update(updateCampList,false);
            Constants.disableCampaignmembertrigger = false;
            for(integer i=0;i<myResult.size();i++){
                if(myResult.get(i).isSuccess()){
                    allSuccessId.add(myResult.get(i).getID());    
                }else{
                    Database.error error =  myResult.get(i).getErrors().get(0);
                    string errMsg = error.getMessage();
                    errormsgMap.put(updateCampList.get(i).id,errMsg);
                }
                
            }
            
        }
    }
    
    global void finish(Database.BatchableContext BC) {
        for(Id ids :allIds){
            if(!allSuccessId.contains(ids)){
                allFailedId.add(ids); 
            }   
        }
        for(Id recrdId : errormsgMap.keyset()){
            Exception__c exe = new Exception__c();
            exe.Exception_Class_Name__c = 'batchclass';
            exe.Exception_Method_Name__c = 'Execute';
            exe.Exception_Details__c = recrdId+': '+errormsgMap.get(recrdId);
            exceptionLists.add(exe);
        }
        if(!exceptionLists.isEmpty()){
            insert exceptionLists;    
        }
        integer successSize = allSuccessId.size();
        integer FailedSize = allFailedId.size();
        String FailedIds = 'SuccessCount'+successSize +'FailedRecordCount'+FailedSize;
    }
    
}