You need to sign in to do that
Don't have an account?
cjen
MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa):
Hi, i want to create a contact when a new user is created. I tried process builder and trigger but get the following error (trigger):
MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa):Contact, original object: User: []: Trigger.NewContactOnUser: line 19, column 1
Here is the code:
trigger NewContactOnUser on User (before insert) {
List<Contact> contacts = new List<Contact>();
for (User u: trigger.new){
Contact c = new Contact();
c.FirstName = u.FirstName;
c.LastName = u.LastName;
c.Email = u.Email;
c.MailingStreet = u.Street;
c.MailingCity = u.City;
c.MailingState = u.State;
c.MailingCountry = u.Country;
c.MailingPostalCode = u.PostalCode;
c.Title = u.Title;
c.Phone = u.Phone;
c.AccountID = '001W000000JgPzp';
contacts.add(c);
}
insert contacts;
Can anyone help fix this or any ideas to create a contact from a user record?
Thanks!
MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa):Contact, original object: User: []: Trigger.NewContactOnUser: line 19, column 1
Here is the code:
trigger NewContactOnUser on User (before insert) {
List<Contact> contacts = new List<Contact>();
for (User u: trigger.new){
Contact c = new Contact();
c.FirstName = u.FirstName;
c.LastName = u.LastName;
c.Email = u.Email;
c.MailingStreet = u.Street;
c.MailingCity = u.City;
c.MailingState = u.State;
c.MailingCountry = u.Country;
c.MailingPostalCode = u.PostalCode;
c.Title = u.Title;
c.Phone = u.Phone;
c.AccountID = '001W000000JgPzp';
contacts.add(c);
}
insert contacts;
Can anyone help fix this or any ideas to create a contact from a user record?
Thanks!
Thanks
All Answers
User is a setup object . DML over setup and non-setup objects cannot be done in same thread or context. For this @future annoation is used to create a contact with the user details. The above problem can be solved by creating a class with a method to create the contact using user details (A future method cannot have SObject parameters, hence the long parameter list) and modifying the trigger as below.
Trigger:
Future method:
Please let me know if you need any help.
Thanks.
Thanks
@isTest
private class MyFutureClassTEST {
static testMethod void validateMyFutureClass() {
Contact c = new Contact();
c.FirstName = 'FName';
c.LastName = 'LName';
c.Email = 'fname@fname.com';
c.MailingStreet = '1234 Some Street';
c.MailingCity = 'Somewhere';
c.MailingState = 'TX';
c.MailingCountry = 'USA';
c.MailingPostalCode = '90210';
c.Title = 'CEO';
c.Phone = '817-555-4564';
c.AccountID = '001W000000JgPzp';
//Insert Contact
insert c;
//Retrieve the new Contact
c = [SELECT FirstName, LastName, Email, MailingStreet, MailingCity, MailingState, MailingCountry, MailingPostalCode, Title, Phone, AccountID from Contact WHERE Id =:c.Id];
//Test correctly created the contact
System.AssertEquals('FName', c.FirstName);
System.AssertEquals('LName',c.LastName);
System.AssertEquals('fname@fname.com',c.Email);
System.AssertEquals('1234 Some Street',c.MailingStreet);
System.AssertEquals('Somewhere',c.MailingCity);
System.AssertEquals('TX',c.MailingState);
System.AssertEquals('USA',c.MailingCountry);
System.AssertEquals('90210',c.MailingPostalCode);
System.AssertEquals('CEO',c.Title);
System.AssertEquals('817-555-4564',c.Phone);
System.AssertEquals('001W000000JgPzp',c.AccountID);
}
}
Note: Put the 'insert userObject;' statement inside test.startTest() and test.stopTest() as the contact is created using a future method.
Thanks.
As a best practice, please mark the answer if it helped you.
Thanks.