+ Start a Discussion
babloo123babloo123 

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.Trigger12: line 17, column 1

I am getting a issue when I am trying to update by Email id on Contact which inturns updates the username on userobject and email on UserObject.Please can some one guide how to overcome this I need to change the ProfileID as well using the trigger.

trigger Trigger12 on Contact (before update) {
    
    System.debug(trigger.new[0].ID);
    
      Contact cnt=[select email from Contact where id=:trigger.new[0].Id];
    system.debug(cnt);
    String emailid=string.valueof(cnt.Email);
    system.debug('The Email id is:' + emailid);
    
    User usr=[select Email,Username from User where ContactId=:trigger.new[0].Id];
    system.debug(usr);
    
    if(trigger.new[0].Email!=cnt.Email)
    {
        usr.Email=trigger.new[0].Email;
    usr.username=trigger.new[0].Email; 
    }
    update usr;
    }
James LoghryJames Loghry
Try moving your logic to an after update rather than a before update Trigger.

Also, your trigger is not bulkified.  Meaning, if you load many contact records, only the first record is ever updated.

Please read this document for more information: https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
babloo123babloo123
Hi James no before or after is not helping also we just have one contact so I did not bulkify. Can you help me other approach
Balaji BondarBalaji Bondar
Babloo,

you should create a new class and create a new method in that class
add @future keyword
Eg.
@future
public static void CreateUser(List<ID> UserID,Integer x,Map<ID,String> mapOfOldUserNames){

future method should be static I suppose. Now move all your logic to that future method. Now call this method from your trigger directly since this is a static method.

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
babloo123babloo123
Balaji can you guide me how the code looks like stuck here from morning
Balaji BondarBalaji Bondar
Babloo,

Try below code :
trigger Trigger12 on Contact (before update) {
	System.debug(trigger.new[0].ID);
	Contact cnt=[select email from Contact where id=:trigger.new[0].Id];
	system.debug(cnt);

	String emailid=string.valueof(cnt.Email);
	system.debug('The Email id is:' + emailid);

	User usr=[select Email,Username from User where ContactId=:trigger.new[0].Id];
	system.debug(usr);

	if(trigger.new[0].Email!=cnt.Email){
		usr.Email=trigger.new[0].Email;
		usr.username=trigger.new[0].Email; 
		UpdateUserInformation.UpdateUser(usr);
	}
	//update usr;
}
Async Apex class to update the trigger.
public class UpdateUserInformation {
@future
	public static void UpdateUser(User userObj) {
		Update userObj;
	}
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.