Skip to content

Instantly share code, notes, and snippets.

@tsmalara
Last active January 20, 2019 21:45
Show Gist options
  • Save tsmalara/6a312e217a6b6fa73bc3918d2cf2b691 to your computer and use it in GitHub Desktop.
Save tsmalara/6a312e217a6b6fa73bc3918d2cf2b691 to your computer and use it in GitHub Desktop.
public class MixedDMLFuture {
public static void useFutureMethod() {
// First DML operation
Account a = new Account(Name='Acme');
insert a;
// This next operation (insert a user with a role)
// can't be mixed with the previous insert unless
// it is within a future method.
// Call future method to insert a user with a role.
Util.insertUserWithRole(
'johndoe@acme.com', 'jdoe',
'johndoe@acme.com', 'DOE');
}
}
public class Util {
@future
public static void insertUserWithRole(
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,
languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
timezonesidkey='America/Los_Angeles',
username=uname);
insert u;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment