Skip to content

Instantly share code, notes, and snippets.

@TheShubhamVsnv
Created June 30, 2021 13:15
Show Gist options
  • Save TheShubhamVsnv/6d6ccb4787f6cc5e1ab18be57b0ed8d6 to your computer and use it in GitHub Desktop.
Save TheShubhamVsnv/6d6ccb4787f6cc5e1ab18be57b0ed8d6 to your computer and use it in GitHub Desktop.
Create Account record whenever contact is created without an account.
trigger ContactCustomTriggerExample on Contact (after insert) {
List<Account> accListToInsert = new List<Account>();
for(Contact con : Trigger.New) {
//check if account is null on contact
if(con.AccountId == null ) {
Account acc = new Account();
//Add all required field on Account
acc.Name = con.LastName;
acc.Phone = con.Phone;
accListToInsert.add(acc);
}
}
if(!accListToInsert.isEmpty()){
insert accListToInsert;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment