• Avinash 35
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
I have creted the apex class and the call to trigger update and i have also try with before update but in that i occur the proccesinstance comments null
apex class:
 public static void updateComments(List<ASKWA_Documents__c> doclist){
        List<Id> docIds = New List <Id>();
        List<id>processId=new List<id>();
        for (ASKWA_Documents__c docObj: doclist){
            if(docObj.X2nd_Level_Approved__c == 'N'){
                System.debug('approval' +docObj.X2nd_Level_Approved__c);
                docIds.add (docObj.Id);
            }
        }
        List<ProcessInstance> instances = [SELECT Id, TargetObjectId FROM ProcessInstance WHERE TargetObjectId IN :docIds];
        System.Debug ('** instances: '+instances);
        Map<Id,Id> DocProcessMap = new Map<Id,Id>();
        docIds = New List <Id>();
        for(ProcessInstance piObj:instances){
            DocProcessMap.put (piObj.TargetObjectId,piObj.Id); 
            processId.add (piObj.id);
        }
        System.Debug ('** mapa1: '+DocProcessMap);
        System.Debug ('** processId: '+processId);
        string commentStr=''; 
        List<ProcessInstanceStep> instancesSteps = [SELECT Id,Comments,ProcessInstanceId FROM ProcessInstanceStep WHERE ProcessInstanceId IN:processId];
        System.debug('++instancesSteps' +instancesSteps);
        Map<Id,String> DocProcessStepMap = new Map<Id,String>();
        for(ProcessInstanceStep pisObj:instancesSteps){
            System.debug('+++processid'+pisObj.ProcessInstanceId);
            System.debug('+++processid'+pisObj.Comments);
            DocProcessStepMap.put (pisObj.ProcessInstanceId, pisObj.Comments); 
        }
        System.Debug ('** mapa2: '+DocProcessStepMap);
        for (ASKWA_Documents__c docObj: doclist){
            //if (l.X2nd_Level_Approved__c == 'N'){
            System.debug ('** razon2: '+ DocProcessStepMap.get(DocProcessMap.get(docObj.Id)));
            docObj.X2nd_Level_Remark__c = DocProcessStepMap.get(DocProcessMap.get(docObj.Id));
            //System.debug ('** razon: '+docObj.X2nd_Level_Remark__c);
            //}
        } 
    }
please let mi know what is wrong i will not able to update .
I have requirment that I have send the birthday email toclient,
parent account under that person accunt if the  2 person account under that parent then 2 record email id's same then the email goes only onnce when batch is running,

batch apex:

global class ASK_ClientBirthdayBatch Implements Database.Batchable<sObject>,Schedulable {
    global Database.QueryLocator start(Database.BatchableContext bc) {   
        Id ASKIMPersonAccountId = Schema.SObjectType.account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId(); 
      
        return Database.getQueryLocator([SELECT Id,NAME,PAN__pc,Parent_Account__c,PersonEmail,PersonContactId,PersonBirthdate,Back_Office_Equity_Code__pc,Active__pc,Investor_Type__pc,RecordType.Name FROM Account WHERE RecordType.Name='Person Account' AND PersonBirthdate!=Null AND Investor_Type__pc!=Null AND Active__pc='A' AND (Investor_Type__pc='Individual - Resident' 
                                                                                                                                                                                                                                                                                                                                         OR Investor_Type__pc='Individual - Non Resident' OR Investor_Type__pc='Minor through Guardian'OR Investor_Type__pc= 'Individual') order By Parent_Account__
       
    }
    // excute method
    global void execute(Database.BatchableContext BC, list<Account> accountList) {
        map<String,Account> MapOfEmailVsAcc_Birthday = new map<String,Account>();
        map<id,set<String>> mapOfIndividualIdVsEmail= new map<id,set<String>>();
        map<Id,List<Account>> mapOfIndividualAccIdvsPersonACCRec= new map<id,list<account>>();
        if(accountList !=null && accountList.size()>0){
            for(Account conobj:accountList){
                // creating a map of Individual Account  vs list of  related person account
                if (String.isNotBlank(conobj.Parent_Account__c)) {
                    System.debug('AAA');
                    if (!mapOfIndividualAccIdvsPersonACCRec.containsKey(conobj.Parent_Account__c)) {
                        System.debug('BBB');                           
                        // putting the  value in the map
                        mapOfIndividualAccIdvsPersonACCRec.put(conobj.Parent_Account__c, new List < Account > ());
                    }
                    System.debug('CCC');
                    mapOfIndividualAccIdvsPersonACCRec.get(conobj.Parent_Account__c).add(conobj);
                }
            }
        }
        
        if(mapOfIndividualAccIdvsPersonACCRec !=null && mapOfIndividualAccIdvsPersonACCRec.size()>0){
            for( Id accId:mapOfIndividualAccIdvsPersonACCRec.keyset()){
                System.debug('DDD');
                list<Account> listOfPersonalAcc=mapOfIndividualAccIdvsPersonACCRec.get(accId);
                if(listOfPersonalAcc !=null && listOfPersonalAcc.size()>0){
                    // getting all the person account related to individual account
                    System.debug('Record>>>>'+ listOfPersonalAcc);
                    System.debug('OK');
                    for(Account acc:accountList){
                        /* if birthday  is equal to current day below condition will execute*/
                        if(acc.PersonBirthdate!=null && acc.PersonBirthdate.day()==system.Today().day() && acc.PersonBirthdate.month()==system.Today().month()){
                            String Bocode=acc.Back_Office_Equity_Code__pc.trim();
                            MapOfEmailVsAcc_Birthday.put(Bocode,acc);
                        }
                    }
                }
                if(MapOfEmailVsAcc_Birthday !=null && MapOfEmailVsAcc_Birthday.size()>0){
                    // calling helper method to send  birthday email
                    system.debug('line 60 Tamasa'+ MapOfEmailVsAcc_Birthday);
                    ASK_ClientBirthdayHandler.sendClientBirthdayMail(MapOfEmailVsAcc_Birthday);
                }
            }
        }
    }
    global void finish(Database.BatchableContext BC) {
        
    } 
    /*Implementing execute method of Schedulable interface with batch invoking logic*/
    global void execute(SchedulableContext sc) {
        /*chacking if a job is already being executed*/
        if(!isBatchRunning('ASK_ClientBirthdayBatch')) {
            
        }
    }
    /*Method to check if the batch is running already*/ 
    private boolean isBatchRunning (String className){
        Set<String> activeJobs = new Set<String>{'Preparing','Processing'}; 
            integer recordCount = [SELECT count() FROM AsyncApexJob where Status in :activeJobs and ApexClass.name=:className ];
        if(recordCount>0)
        {
            return true;
        }
        else
        {
            return false;
        } 
    } 
}


and below is handler:
public class ASK_ClientBirthdayHandler {
    Public static void sendClientBirthdayMail(map<string,account> mapOfEmailIdVsAccountinfo){
        System.debug('#mapOfEmailIdVsAccountinfo '+mapOfEmailIdVsAccountinfo);
        List<Messaging.SingleEmailMessage> msgListToBeSend = new List<Messaging.SingleEmailMessage>();
        map<String,Set<String>> mapPanVsBoCode= new map<String,Set<String>>(); // for BoCode
        map<String,Set<String>> mapPanVsMailIds= new map<String,Set<String>>(); // for client Email
        map<String,Account> mapPanVsAccount= new map<String,Account>(); // for pan and Account Id
        String templateName = 'Birthday_Greetings';
        Id templateId = [SELECT Id FROM EmailTemplate WHERE DeveloperName = : templateName Limit 1].Id;
        if(mapOfEmailIdVsAccountinfo !=null && mapOfEmailIdVsAccountinfo.size()>0){
            List<Account>allAccountList = mapOfEmailIdVsAccountinfo.values();
            for(Account ac:allAccountList){
                mapPanVsAccount.put(ac.PAN__pc,ac);
                if(mapPanVsBoCode.containsKey(ac.PAN__pc)){
                    String accBocodeEx=ac.Back_Office_Equity_Code__pc.trim();
                    Set<string>temAccIdEx=mapPanVsBoCode.get(ac.PAN__pc);
                    temAccIdEx.add(accBocodeEx);
                    mapPanVsBoCode.put(ac.PAN__pc,temAccIdEx);
                }
                else{
                    String accBocode=ac.Back_Office_Equity_Code__pc.trim();
                    set<string>tempAccId=new set<string>();
                    tempAccId.add(accBocode);
                    mapPanVsBoCode.put(ac.PAN__pc,tempAccId);
                    
                }
            }
            for(Account a:allAccountList){
                if(mapPanVsMailIds.containsKey(a.pan__pc)){
                    Set<String>clientEmailExt= new set<string>();
                    System.debug('###'+clientEmailExt);
                    if(!String.isBlank(a.PersonEmail)){
                        clientEmailExt.add(a.PersonEmail);
                        System.debug('#test'+clientEmailExt.add(a.PersonEmail));
                    }
                    mapPanVsMailIds.put(a.PAN__pc,clientEmailExt);
                    System.debug('#'+mapPanVsMailIds.put(a.PAN__pc,clientEmailExt));
                }
                
                else{
                    System.debug('test');
                    Set<String>clientEmail=new set<String>();
                    if(!String.isBlank(a.PersonEmail)){
                        clientEmail.add(a.PersonEmail);
                    }
                    mapPanVsMailIds.put(a.PAN__pc,clientEmail);
                }
                
            }
            
            for(String Key:mapPanVsBoCode.keyset()){
                Messaging.SingleEmailMessage mailtoBeSend =  new Messaging.SingleEmailMessage();
                mailtoBeSend=PrepareEmailForClient(mapPanVsBoCode.get(key),templateId,mapPanVsMailIds.get(key),mapPanVsAccount.get(Key));
                system.debug('line '+mailtoBeSend );
                if(mailtoBeSend !=null){
                    msgListToBeSend.add(mailtoBeSend);
                }
                
            }
            if(msgListToBeSend != null && msgListToBeSend.size()>0){
                try{
                    system.debug('## msgListToBeSend'+msgListToBeSend );
                    Messaging.sendEmail(msgListToBeSend);
                    
                }catch ( exception e){
                    
                    system.debug('*****Exception*****'+e.getmessage());
                }
            }
        }
    }
    
    
    public static  Messaging.SingleEmailMessage PrepareEmailForClient( Set<String> PanAccNoClientId,Id templateId,Set<String> clientEmailIds, Account Acc){
        List<OrgWideEmailAddress>  oweList= ASK_GlobalUtilityCtlr.getOWEID();
        
        System.debug('##PanAccNoClientId '+PanAccNoClientId);
        System.debug('##clientEmailIds '+clientEmailIds);
        
        //New instance of a single email message
        
        List<String> ccAddress = new  List<String>();
        List<String> toAddress = new List<String>();
        if(!clientEmailIds.isEmpty()){
            toAddress.addAll(clientEmailIds);
        }   
        System.debug('cc emais>>'+ccAddress); 
        System.debug('toAddress emais>>'+toAddress); 
        // System.debug('Orgwide email address>>>'+oweList[0]);
        Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
        mail.setTargetObjectId(Acc.PersonContactId);
        if(oweList !=null && oweList.size()>0){
            mail.setOrgWideEmailAddressId(oweList[0].id);
        }  
        mail.setTemplateId(templateId);
        
        mail.setToAddresses(toAddress); //CC-66 added toList of Addresses
        mail.setCcAddresses(ccAddress); //CC-66 Changes made to the data type
        
        mail.setWhatId(Acc.Id);    
        mail.setBccSender(false);
        mail.setUseSignature(false);
        
        
        // mail.setReplyTo('recruiting@acme.com');
        //mail.setSenderDisplayName('HR Recruiting');
        // mail.setSenderDisplayName('ASK Wealth advisors');
        mail.setSaveAsActivity(true); 
        system.debug('Mail'+mail);
        return mail; 
        //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
    
    
}