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
OnDem DevOnDem Dev 

Update User Object

Hi All
 
While i am trying to update ManagerId in a User Object , it neither throwing any exception nor updating it.
If i am executing the same update statement through System Log it is getting updated , but the same code in
an apex class is not working, Can anyone suggest what exactly might be the reason.
 
 
Thanks in Advance
 
DevAngelDevAngel
Hi OnDem Dev,

If it is from Apex Code, you need to make sure that you are using the update dml statement.  Why not post a bit of the code and that should tell the story.


Cheers
OnDem DevOnDem Dev
User user=[Select ManagerId from User us where us.Id=:userid];
   user.ManagerId='005T0000000xyzaAE';
try{
update user;
}catch(DmlException dme){
 //return 'From exception '+user.ManagerId+dme;
}
 
Here after catching i found an exception
 
 

with the following message.

 

Update failed:First exception on row 0 with id  ; first error: MIXED_DML_OPERATION on setup object is not permitted after you have updated a non-setup object : User[Id]

 
MyGodItsColdMyGodItsCold
Was this ever solved? I have a trigger that runs after update & insert on a custom object. Along the way, I attempt to update the current user's User record. Is this not possible?

Code:
trigger MaintCurrentML on Mail_List__c (after insert, after update) {
    
    boolean dbug = true;
    
    List<Mail_List__c> origML = trigger.old;
    List<Mail_List__c> changedML = trigger.new;
    List<User> me = [SELECT Id, ALT_Current_Mail_List__c from User WHERE Id = :UserInfo.getUserId()];
    
    for (integer i=0; i<changedML.size(); i++) {
        if (changedML[i].CHANGE_ME__c == 'Make Current List') {
            me[0].ALT_Current_Mail_List__c = changedML[i].Name;
            //changedML[i].CHANGE_ME__c = '';
        }    
            
    } // for i=0
    if (dbug) {
        system.debug('me: ');
        system.debug(me);
    }
    update me;
}

The error is:

Apex trigger MaintCurrentML caused an unexpected exception, contact your administrator: MaintCurrentML: execution of AfterUpdate

caused by: System.DmlException: Update failed. First exception on row 0 with id 00550000000mh92AAA; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object: User: [Id]: Trigger.MaintCurrentML: line 20, column 5

Thanks.

BhawnaBhawna

MyGodItsCold wrote:
Was this ever solved? I have a trigger that runs after update & insert on a custom object. Along the way, I attempt to update the current user's User record. Is this not possible?

Code:
trigger MaintCurrentML on Mail_List__c (after insert, after update) {
    
    boolean dbug = true;
    
    List origML = trigger.old;
    List changedML = trigger.new;
    List me = [SELECT Id, ALT_Current_Mail_List__c from User WHERE Id = :UserInfo.getUserId()];
    
    for (integer i=0; i

The error is:

Apex trigger MaintCurrentML caused an unexpected exception, contact your administrator: MaintCurrentML: execution of AfterUpdate

caused by: System.DmlException: Update failed. First exception on row 0 with id 00550000000mh92AAA; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object: User: [Id]: Trigger.MaintCurrentML: line 20, column 5

Thanks.





Were u able to get any solution for your problem? I am running into exactly same issue of Updating User when any Custom Object record is Updated or added.