You need to sign in to do that
Don't have an account?
babloo123
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;
}
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;
}
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
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.
Try below code : Async Apex class to update the trigger. 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.