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

Problem: Changing user's role from roles hierarchy is not considered user transaction



I am relatively new to Apex coding. I noticed that there is no functionality for upding the user's manager details automatically when the user's role changes. So, I am trying to implement the same using a trigger on User object. While I am getting satisfactory results when I am updating the user manually (editing the user and changing the role). However, when I am assigning the user to another role from the role hierarchy, the manager details are not getting updated.


On a hunch, I decided to edit the user (after such transaction) and save without modifying anything and the trigger picked up the changes.


So, this essentially means assigning users to different roles using role hierarchy is not considered as a "User" transaction and hence will not be picked up by the trigger (before/after update).


Any idea on how to work around this? Because in a large organizations, usually these types of transactions are done in bulk, specially during a re-alignment period.


Any help/suggestions will be welcome..!!



Best Answer chosen by AyanHore



I tried to create a Workflow rule on User that would fire when the RoleID changes, but you can't use "ISCHANGED()" in the formula for some reason, so that doesn't work.


If the update doesn't have to happen immediately, you could do it with a scheduled job.  Add a field to User that holds the last known Role ID (or name?).  When the scheduled job runs, it checks all users to see if their Role is the same as the last known Role.  If it has changed, process the user and update the last known role field.


It's a little kludgy, but if you can't find any other way, it might have to do...


If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!


-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator