I'm writing a Trigger that After Insert of a new User, I need to create a Contact
with that same new User information. I'm getting this error:
MIXED_DML_OPERATION
DML operation on setup object is not permitted after you have updated a non-setup object.
Can someone explain how I could work around this error? Thanks!
trigger UserInsertContactInsert1 on User (after insert) {
if(Trigger.isAfter && Trigger.isInsert){
Set<ID> usrIds = new Set<ID>();
for(User u : Trigger.new){
usrIds.add(u.id);
}
List<User> usrList = [SELECT Id, FirstName, LastName, Email, Phone FROM User WHERE Id IN :usrIds];
List<Contact> cList = new List<Contact>();
for(User u : usrList){
cList.add(new Contact(FirstName=u.FirstName, LastName=u.LastName, Email=u.Email, Phone=u.Phone, OwnerID=u.ID, Related_User__c=u.Id));
}
insert cList;
}
}