0 votes
1 view
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 (30.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();

}    

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...