Untitled

 avatar
unknown
plain_text
2 months ago
4.2 kB
2
Indexable
/**
 * @description       : This is a batch class created to update the existing account and opportunity roles
 * @author            : Rohit Goel
 * @group             : 
 * @last modified on  : 08-19-2024
 * @last modified by  : Rohit Goel
**/
public without sharing class MassAssignRoleBatch implements Database.Batchable<Sobject>,System.Schedulable {
    String query;
    Boolean enableChain ;
    String objectName;
    String username;
    Id recordtypeId; // Added to store the recordtype id as it will be used in start method
    /**
    * @description Removed the runFullScan attribtue as we are always running full scan now
    * @author Rohit Goel | 08-05-2024 
    * @param objectName 
    * @param runChain 
    **/
    public MassAssignRoleBatch(String objectName, Boolean runChain) { 
        this.objectName = objectName;
        this.query = 'SELECT Id,userId,TeamMemberRole from  '+ objectName + ' where  user.isActive = true ';
        if(objectName  == 'OpportunityTeamMember'){
            // Added the record type check as part of PLIG 3976
             this.recordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName()
        .get('BKU_Deposit').getRecordTypeId();
            // We have added additional filter for opportunity team member to only change open opportunities or closed opportunity which have clsoed date for this year
            this.query += ' and ( (not opportunity.stageName like \'Closed%\') or (opportunity.stageName = \'Closed Won - Accounts Opened\' and Opportunity.recordtypeId =:recordtypeId)) ';
        }
        this.enableChain = runChain;
    }
    
    /**
    * @description This is the start method for the batch
    * @author Rohit Goel | 06-24-2024 
    * @param BC 
    * @return Database.QueryLocator 
    **/
    public Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator(query);
     }
     /**
     * @description This is the execute method where we are calling existing AutoAssignRole class
     * @author Rohit Goel | 06-24-2024 
     * @param BC 
     * @param lst 
     **/
     public void execute(Database.BatchableContext bc, List<Sobject> lst){
        try{
           // System.debug('Called the class with ' + lst.size());
            AutoAssignRole.updateRoleNames(lst);
        }
        catch(Exception e){
            // There is no logging framework atm hence just printing the error
            System.debug(LoggingLevel.Info,'Exception is ' + e.getMessage());
        }
     }
     /**
     * @description This is the finish method where we are recalling the batch with OpportunityTeamMember
     * 
     * @author Rohit Goel | 06-24-2024 
     * @param BC 
     **/
     public void finish(Database.BatchableContext bc){
        AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
                        TotalJobItems, CreatedBy.Email
                        FROM AsyncApexJob WHERE Id =
                        :bc.getJobId() WITH SYSTEM_MODE];
        String htmlBody = '<html><body>';
        String teamName = this.objectName == 'AccountTeamMember'? 'Account' : 'Opportunity' ;
        htmlBody += '<div>Dear Admins'+',</div><br/>';  
        htmlBody += '<div>The batch job for correcting role alignment of users has completed for '+ teamName +  '.</div><br/>';
        htmlBody += '<div>The batch Apex job processed ' + a.TotalJobItems +' batches with '+ a.NumberOfErrors + ' failures </div><br/>';
        Messaging.SingleEmailMessage mail = BKUUtility.singleMessageHTML(new List<String>{System.Label.Batch_Email_Recipient},teamName + ' ' +System.Label.Team_Batch_Subject,  htmlBody,'');
        Messaging.sendEmailResult [] result = Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{mail});
        // System.debug(' In finish method ' + this.chainBatch );
        if(this.enableChain == true ){
            Database.executeBatch(new MassAssignRoleBatch('OpportunityTeamMember',false),2000);
        }
     }
     public void execute(SchedulableContext sc){
        Database.executeBatch(new MassAssignRoleBatch('AccountTeamMember',true),2000);
     }
}
Leave a Comment