Untitled

 avatar
unknown
plain_text
a year ago
3.9 kB
6
Indexable
/*************************************************************************************************
Developer :  Sai Kalikota
Created   :  Apr 23, 2021
Objective :  This batch class updates the Account's Industry Category & Industry SubCategory based
             on the Account_Industry_Mapping__c Custom Setting
Dovetail URL: https://twlo.lightning.force.com/lightning/r/Workstream__c/a181W0000041JPmQAM/view
Test Class:  AccountIndustryUpdateBatchTest
*************************************************************************************************/
public class AccountIndustryUpdateBatch implements Database.Batchable<sObject>, Schedulable {
    public Map<String,Account_Industry_Mapping__c> industryMapping;

    public AccountIndustryUpdateBatch(){
        industryMapping=new Map<String,Account_Industry_Mapping__c>();
        List<Account_Industry_Mapping__c> accountIndustryMappings=Account_Industry_Mapping__c.getAll().values();
        //Create a map with combination of Datafox Industry & Datafox Sub Industry as the key and Account_Industry_Mapping__c as the value
        for(Account_Industry_Mapping__c mapping:accountIndustryMappings){
            if(mapping.DF_Sub_Industry__c!=null){
                industryMapping.put(mapping.DF_Industry__c+'-'+mapping.DF_Sub_Industry__c,mapping);
            }
            else{
                industryMapping.put(mapping.DF_Industry__c,mapping);
            }
        }
    }

    public Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute       
        //String query = 'SELECT Id,DataFox_Industry__c,Datafox_Subindustry__c FROM Account WHERE DataFox_Industry__c != Null AND Industry_Category__c = Null';
        return Database.getQueryLocator('');
    }
    
    public void execute(Database.BatchableContext BC, List<Account> accList) {
        List<Account> accountsToUpdate=new List<Account>();
        List<Apex_Error_Log__c> errorLogs=new List<Apex_Error_Log__c>();
        String key='';
        for(Account acc:accList){
            /*if(acc.Datafox_Subindustry__c!=null){
                key=acc.DataFox_Industry__c+'-'+acc.Datafox_Subindustry__c;
            }else{
                key=acc.DataFox_Industry__c;
            }*/
            //Fetch the Industry & Sub Industry mapping from the Custom Settings and update the account
            if(industryMapping.containsKey(key)){
                acc.Industry_Category__c=industryMapping.get(key).TWLO_Industry__c;
                acc.Industry_Sub_Category__c=industryMapping.get(key).TWLO_Sub_Industry__c;
                accountsToUpdate.add(acc);
            }
        }
        if(!accountsToUpdate.isEmpty()){
            Database.SaveResult[] srList=Database.update(accountsToUpdate,false);
            for(Database.SaveResult sr:srList){
                //Create an error if the update is failed
                if(!sr.isSuccess()){
                    Apex_Error_Log__c log=new Apex_Error_Log__c(Error_message__c=sr.getErrors().get(0).getMessage(),Type__c='AccountIndustryUpdateBatch update Exception',Record_Id__c=sr.getId());                    
                    errorLogs.add(log);
                }
            }
        }
        if(!errorLogs.isEmpty()){
            insert errorLogs;
        }
    }   
    
    public void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
    }
    
    public void execute( SchedulableContext SC ) {
        List<DeprecateField__c> CallApex = DeprecateField__c.getAll().values();
        DeprecateField__c deprecatefield = DeprecateField__c.getvalues('DataFoxFieldDeprecate');
        Boolean callBatch = deprecatefield.DeprecateCheck__c;
        if(callBatch == False){
        AccountIndustryUpdateBatch be = new AccountIndustryUpdateBatch();
        Database.executeBatch(be,25);  
        }
    }
}
Editor is loading...