Avoiding Mixed DML Operations Exception Example

Salesforce Future Annotation, future annotation example, salesforce tutorial, future method in salesforce,salesforce future method,Avoiding Mixed DML Operations Exception Example

Avoiding Mixed DML Operations Exception Example by using Future method

Trigger :-

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;
 'dineshd@outlook.com', 'Dinesh',
 'dineshd@outlook.com', 'Dineshdk');


Util class (with future method to avoid MIXED_DML_OPERATION Exception on non-setup object(user) ) :-

public class UtilClass {
 public static void userInsertWithRole(
 String uname, String al, String em, String lname) {
 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,
 localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
 insert u;

Be the first to comment

Leave a Reply

Your email address will not be published.