Use Case: Deactivate Users with The Last Login Date
We have utilized the Schedule-Triggered flow to automate sending the emails. Now, we want to show you how to use Schedule-Triggered flow to automatically deactivate the users! In Flow Builder, there is a standard field for the User object called “Last Login Date“. This field makes the solution very easy to build, so let’s dive in!
* Big thanks to Gustavo Seluja for sending in the case!
Lulu Mobile wants to automate their user management. If a user hasn’t logged in for the past 90 days, they want to deactivate this user automatically. At the same time, they want to add a note of “90 Days No Login” in their custom Note field.
(Assumption: There is a “Note” text field on the User object)
We will use the Schedule-Triggered flow. In the automate email post, we created a formula field and let it run daily. However, we cannot do the same for the User object because we can only reference the Last Login Date in Flow Builder but not in the formula field. So instead of evaluating the last login date in Choose Object and Filter Conditions, we will use a Decision element to do so.
This means, whenever the flow runs, all active users will be included in the flow. Thus, I will recommend running this flow weekly instead. Of course if you believe your instance has enough capacity and you don’t have too many users, you can still run this daily.
Schedule-Triggered Flow | Get RecordsUpdate Records | Assignment |
FormulaFormula | Decision |
Does the solution solve your problem? If not, write us what your problem is and we will build the flow for you!
Melody: you nailed it. Beautiful. I tested it today. Thank you!!!!!!
My pleasure Gustavo. Good luck with all the future flow building 😊
Hi Gustavo,
I’ve just tried to do the same for user who haven’t logged in over 90 days, but it only updated 1 user :/
Hi Melody,
I tried replicating the steps you provided but it didn’t work for me.
I got the following error: Failed to update records whose IDs are in {!$Record}.
I’d appreciate your help.
Here are the screenshots:
https://picd.in/IqJ7kfFm
https://picd.in/sBJ0VuwB
https://picd.in/onSTaraL
https://picd.in/NS2E4DqT
https://picd.in/CAUbY6pN
https://picd.in/bA9yYwz8
https://picd.in/IpT8dEil
Thanks!
Hi Afshar,
I searched a bit and found this post – it might be because the user you were testing has a special license. Try to add one more condition as in the screenshot to see if it works. Let me know how it goes!
Thank you so much for replying.
Although, this time states as completed I still got the same error regarding the login date null.
https://picd.in/c1X2N8h7
https://picd.in/rjRkqZS4
Hi Afshar,
The flow is now without a problem – When you are debugging a schedule-triggered flow, the system will only debug the first user it gets. Since the first one it got is the analytic user, the system won’t run the rest of the flow.
If you want to test on a regular user, add a “temporary” condition on the Start element (ex. UserId = xxxxx). Then you can debug the flow on this specific user.
Once you finish debugging, make sure to remove this condition before you activate the flow.
Hope this explains
No chance, I tried different methods and now I got UNABLE_TO_LOCK_ROW error. I checked Google but it seems too advanced for me. How can I resolve that issue? It didn’t work for me.
Hi Al, this error happens when there are several jobs or people trying to update the same record. Do you know if there’s any other automation that also runs on the User record?
Hmm, there might be but now I get this error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: SendEmailForSalesRepsUserTrigger: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject Trigger.SendEmailForSalesRepsUserTrigger: line 5, column 1. You can look up ExceptionCode value
Hi Al, hmm so you have a Apex trigger that are sending emails to the sales reps? It seems like the error is thrown from that trigger. If you verify the code on line 5, can you think of any possible scenarios that your trigger won’t work? (It seems like you queried out an empty list). Not an apex expert here but let’s try to debug that together!
Hi Melody,
This works like a charm. Thank you for this. Only one thing I would point out is if someone has to do it on thousands of Users (e.g. Customers, Partners Profile Users), it would be better to deactivate the Users which are already beyond the threshold limit of 60/90 days and then schedule this Flow so that there would be less load the first time itself as the number of Users on which the flow has to run would be greatly reduced.
Regards,
Soham
Thanks Soham for sharing your observations with us!:)
Hi Melody
this is amazing
consider following scenario
Can we following this using flow
also
Is it possible using FLOW
I’ve not done much with Schedule Triggered flows, so there may be things going on that I’m not aware of that would answer these questions, but I see two things that make me pause.
There may be a reason that this won’t work, but it seems like it would make the flow significantly more performant, and less likely to run up against a system limit. (Our org isn’t likely to have enough inactive users to cause that problem, but a large org with a large number of users certainly could.)
So what am I missing?
On step 4, how could logic be added to ignore users that have been created in the last 90 days and have profile names that include words like community, integration, or security?
Hi Melody! Thanks for this flow and it was helpful in my case. Question associated with the flow: let’s say I want to add a condition to only have a specific profile to evaluate, how can we reference the profile name for that? I can see that we can use ProfileId as a criteria but not best practice to hardcode IDs. What’s your recommendation? (ex I want to use the Customer Community Plus as a condition when the flow starts). Appreciate your time and effort sharing Flow knowledge!
Hello, I’m trying to add a new condition on this flow, I would like to deactivate inactive users that last login date X and uses Salesforce licence AND can not be Profile ‘System Administrator’ but till now i don’t know how to set this using $User. Glad for your attention
Hi Melody,
I have problem :/ I created this flow, but always this flow get only one User (always the same) and only this one user is considering to deactive. Then the flow is finished. How can I fix it to iterate for all Users?
Hello! The debug will only fetch one record, but if you activate this flow it will work on all records that fits the entry criteria (one record triggers one flow interview)
Hi Melody – running into an error after building this flow:
The flow tried to update these records: {record.id}. This error occurred: FIELD_INTEGRITY_EXCEPTION: To self-deactivate, the org must enable the user self-deactivation setting and the usertype must be external.: Active.
This error implies it can’t be run on internal users, only portal users. Any thoughts on this?
Thanks,
-David