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
Bryan Leaman 6Bryan Leaman 6 

Trigger can no longer change user role in Winter 2022?

We have a trigger that changes a user's role based on changes made to the User.Division field, but as of Winter 2022 it is no longer working.  

I've added debug statements and I see that the code is assigning a new role id to the user record in a before update trigger context on the user object, but that change is no longer being saved to the user record.

Can anyone else confirm this? I haven't found an open issue yet and salesforce support doesn't help us at all when custom code is involved.
AbhinavAbhinav (Salesforce Developers) 
Please provide sample code snippet as well so that we can check if its breaking at our end or not.
Bryan Leaman 6Bryan Leaman 6
/* Dummy sample to illustrate issue 

This will change the user's role to the first role it finds that is NOT the user's current role.
My findings is that the role will not change when a regular user updates their own user
record, but it WILL change if a System Administrator changes the user record.

I HAVE NOT TESTED THIS CODE. IT IS FOR DEMONSTRATION PURPOSES ONLY.
PLEASE ADD WHATEVER OTHER CRITERIA YOU FEEL IS NECESSARY TO
TRY THIS SAFELY IN YOUR SANDBOX
*/
trigger UserTriggerSample on User (before update) {

	Organization org = [SELECT Id, isSandbox from Organization LIMIT 1];

	Map<Id, UserRole> roleMap = new Map<Id, UserRole>([
		SELECT Id, Name, DeveloperName
		FROM UserRole
		ORDER BY DeveloperName
		LIMIT 2
	]);
	List<Id> roleIds = new List<Id>();
	roleIds.addAll(roleMap.keySet());

	// Only run in a sandbox environment.
	// Only bother trying if there are at least 2 roles available
	if (org.isSandbox && roleIds.size()>=2) {
		for(User u : Trigger.New) {
			if (u.UserRoleId!=roleIds[0]) u.UserRoleId = roleIds[0];
			else u.UserRoleId = roleIds[1];
		}
	}
}

 
Bryan Leaman 6Bryan Leaman 6
If anyone else is interested in this issue, the way I finally had to work around this issue was to generate a platform event for the update and the platform even trigger handler accepts the event and is able to update the user record, so it's still happening in real-time with the request rather than trying to run it @future without sharing (not sure if that would've worked or not).
Noah CorletteNoah Corlette
Thanks for your answer, I am also having the same issue and I want to get rid of this problem. So, I started searching for it online and I am glad I have found your post. Today, I have also found my other solution over https://www.topessaywriting.org/samples/geology here. Because I want to write an essay and I need some help.
Big Baby Tape 7Big Baby Tape 7
I recently needed to write an essay, only there was no way to do it myself, so I had to use essay writing services. You can go to the link https://australianwritings.net/nursing-assignment-help/ and find out more about this service, the experts are great at writing essays. I think this is exactly what you are looking for.
Big Baby Tape 7Big Baby Tape 7
I recently needed to write an essay, only there was no way to do it myself, so I had to use essay writing services. You can go to the link https://australianwritings.net/nursing-assignment-help/ and find out more about this service, the experts are great at writing essays. I think this is exactly what you are looking for.