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
MPBSLMPBSL 

How to deactivate users after Inactive Perion

Hi,

 

I have to deactivate users who have not logged into Salesforce since last 60 days but prior to that I have to send them an email from Apex class on their 53rd day of no logging. I want to perform both functionality in same batch class i.e send email on 53 days criteria to users and inactive users who have completed 60 days mark.

 

Can we call execute method twice?

 

Any help would be good.

 

Thanks,

Mayank Pant

 

 

Best Answer chosen by Admin (Salesforce Developers) 
digamber.prasaddigamber.prasad

Hi,

 

Why you need to execute method twice? Just query all users, query locator will return 200(default) user records. In execute() method, check if user has not logged in since 53 days, make a set of such users, also check for users who have not loggged in since 60 days and save them in other set. Now, send email to users of first set (i.e. users who have not logged in since last 53 days) and in-activate users in second set (i.e. users who have not logged in since last 60 days).

 

Also, you can schedule this batch job using apex scheduler to run every day at your specified time.

 

Let me know if you have any other question around this.

 

Happy to help you!

 

Regards,

Digamber Prasad

All Answers

digamber.prasaddigamber.prasad

Hi,

 

Why you need to execute method twice? Just query all users, query locator will return 200(default) user records. In execute() method, check if user has not logged in since 53 days, make a set of such users, also check for users who have not loggged in since 60 days and save them in other set. Now, send email to users of first set (i.e. users who have not logged in since last 53 days) and in-activate users in second set (i.e. users who have not logged in since last 60 days).

 

Also, you can schedule this batch job using apex scheduler to run every day at your specified time.

 

Let me know if you have any other question around this.

 

Happy to help you!

 

Regards,

Digamber Prasad

This was selected as the best answer
MPBSLMPBSL

Hi,

 

Its really a good solution. One more assistance I need from your.

 

Is there a way in which we can find user records which failed when marked as inactive(update failed) due to any reason in execute block?

 

Thanks,

Mayank Pant

digamber.prasaddigamber.prasad

Hi,

 

Below code snippet should help you to handle each records of user update operation

 

//I assume list of User records is in List lstUser
saveResult = Database.update(lstUser, false); for (Database.SaveResult sr : saveResult) { if (!sr.isSuccess()) { //do error handling if record fails to update } else { //do complementary processing after record successfully updated } }

 

Please let me know if it doesn't work for you.

 

Happy to help you!

 

Regards,

Digamber Prasad