function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arpit Gupta 37Arpit Gupta 37 

Email not getting delivered with the list of inactivated users

Original question:
I am facing an issue, I want to get the list of users in an email I am deactivating through Batch Apex.I am using the below code.

I am getting null for the string StrDeactiveUsrId, Kindly suggest

global class User_Deactivation_6_Months implements Database.Batchable<sObject>
{
   global  list<string> lstDeactiveUsr=new list<string>();
 

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        //String query='SELECT Id,Lastlogindate,IsActive,profileid FROM User WHERE Lastlogindate <  LAST_n_MONTHS:24 and  profileid not in('00eA0000000Re9Y','005A0000001AfHE') and isactive= true';
        String query = 'SELECT Id,UserName,Lastlogindate,IsActive,profileid FROM User WHERE ISACTIVE=TRUE and Lastlogindate < LAST_n_MONTHS:24 and ProfileId NOT IN (\'00eA0000000Re9Y\',\'005A0000001AfHE\')';
        return Database.getQueryLocator(query);

    }

 

    global void execute(Database.BatchableContext BC, List<User> scope)

    {

        for ( User usr : scope)

        {
            
            usr.IsActive = False;
            lstDeactiveUsr.add(usr.UserName);
            /*if(a.profileid != '00eA0000000Re9Y' && a.profileid != '005A0000001AfHE')
            {
                 a.IsActive = False;
                 U1.add(a.id);
           }*/
           System.debug(usr.UserName);
        }

        update scope;

    } 

    global void finish(Database.BatchableContext BC)

    {
        String StrDeactiveUsrId;
        AsyncApexJob a = [Select Id, Status,ExtendedStatus,NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.Email    from AsyncApexJob where Id =:BC.getJobId()];

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        String[] toAddresses = new String[] {'arpitg@amgen.com','ARPI1310@GMAIL.COM'};
        /*'supriyos@amgen.com'};*/

        mail.setToAddresses(toAddresses);

        mail.setSubject('Compass License Management' + a.Status);
        for(string s: lstDeactiveUsr)
        {
            StrDeactiveUsrId+=s+',';
        }
        System.debug(StrDeactiveUsrId);

        mail.setPlainTextBody('records processed ' + a.TotalJobItems +   'with '+ a.NumberOfErrors + ' failures.' +'\n\n SFDC ID OF THE USER PROCESSED' + StrDeactiveUsrId );

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        
    }

}