-1

In this case example please explain .... what exactly happening if i am not calling future method.

trigger Automatecontact on Account(after insert) {
 List<contact> lc = new List<contact>();

for (Account acc : Trigger.new) {
 lc.add( new contact(lastname ='dk',accountId =acc.id) );
 }
 insert lc;
 UtilClass.userInsertWithRole(
 'dineshd@outlook.com', 'Dinesh',
 'dineshd@outlook.com', 'Dineshdk');

}

future method.....

public class UtilClass {
 @future
 public static void userInsertWithRole(
 String uname, String al, String em, String lname) {
 System.debug('dk');
 Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
 UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
 // Create new user with a non-null user role ID
 User u = new User(alias = al, email=em,
 emailencodingkey='UTF-8', lastname=lname,
 languagelocalekey='en_US',
 localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
 timezonesidkey='America/Los_Angeles',
 username=uname);
 insert u;
 }
 }
1

Most of this code is copied verbatim from the documentation on sObjects That Cannot Be Used Together in DML Operations, which describes the sources of Mixed DML Operation errors.

The succinct explanation from that document is

DML operations on certain sObjects, sometimes referred to as setup objects, can’t be mixed with DML on other sObjects in the same transaction. This restriction exists because some sObjects affect the user’s access to records in the org.

That's why the User DML operation is performed in a future method: future methods are executed in a separate transaction, which makes it possible to "switch" from inserting standard sObjects to inserting setup objects.

  • I know some code is copied.... I am looking for the exactly how it effect permission if I insert contact and user in single transaction. Please explain in detail – Rahul Chaudhary Dec 8 '18 at 14:48
  • If you insert the records in a single transaction, you will receive a Mixed DML Operation exception. As the documentation states, this is because changing setup objects can alter the visibility and sharing landscape. For example, if a new user were inserted in a role above the current running user's, and the new user owned records that were also inserted, the current running user would not be entitled to see them under a Private Org-Wide Default. – David Reed Dec 8 '18 at 14:58
0

@Rahul, salesforce does not allow to do dml on Setup and Non-Setup object in single transaction(synchronously). It will give Mixed DML Exception if we do that. Now to achieve the above scenario, we have to executed one dml(either on Setup or Non-Setup object) asynchronously. In your case you have used @future annotation to asynchronously do dml on user.

  • I am looking for the exactly how it effect permission if I insert contact and user in single transaction or calling future. Please explain in detail – Rahul Chaudhary Dec 8 '18 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.