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
divyanshudivyanshu 

User Role

Hi everyone,

 

Can we Update User role using apex code.??

 

Please give me Pseduo code if there is any.

 

Thanxs

Divyanshu

AhmedPotAhmedPot

Hi,

 

UserRole u = new UserRole();
 u.RollupDescription = 'tt';
u.OpportunityAccessForAccountOwner = 'edit';
u.Name = 'tt';
insert u;

 

Only thing to remember here is that you cannot  do mix DML operations on setup and setup object. Check out Apex developer guide "sObjects That Cannot Be Used Together in DML Operations".

for example if your are updating account/ opportunity/custom object and in same transaction trying to update user or userrole/group objects, then you will get mix ml operation not allowed exception.

You can use future callouts to achieve it if needed.

 

Hope this may help you.

 

Thanks,

Ahmed

 

divyanshudivyanshu

Hi,

Thanxs Ahmed for your reply.

I think this will help me alot...

 

Regards

Divyanshu

AhmedPotAhmedPot

You can marked this post as solved, so that it will be helpful to some1 else with similar issue.