Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Salesforce by (11.9k points)
edited by

I have a trigger on a Contact object and when I try to update a User record in this trigger I get the following exception:

"MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Contact"

Trigger code:

trigger UpdateContactTrigger on Contact (after update) {

    User u = [SELECT Id, IsActive FROM User WHERE IsActive = true];

    u.IsActive = false;

    update u;

}

How can I avoid this error while updating User record's fields from a Contact trigger?

1 Answer

0 votes
by (32.1k points)

You need to specify @future method which will be required to do an update and call this method from trigger.

@future

updateUser(){

    User u = [SELECT Id, IsActive FROM User WHERE IsActive = true];

    u.IsActive = false;

    update u;

}

and call it in trigger:

trigger UpdateContactTrigger on Contact (after update) {

    updateUser();

}    

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...