Skip to content

Instantly share code, notes, and snippets.

@oakye
Created September 22, 2020 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oakye/f23c0c4c7b06691880e5bd2f35fb8bb5 to your computer and use it in GitHub Desktop.
Save oakye/f23c0c4c7b06691880e5bd2f35fb8bb5 to your computer and use it in GitHub Desktop.
Week 4 homework from the Salesforce Developer Curriculum by David Liu (bit.ly/go-apex)
trigger createTwoContacts on Account (after insert) {
// Homework from Week 4 of the Salesforce Developer Curriculum
// bit.ly/go-apex
// Write a trigger that creates two identical Contacts whenever an Account is created.
// Make sure both Contacts are associated with the Account.
// Use any values for the fields on the Contacts - just make sure to use variables when
// populating the fields of each Contact to make sure they are identical.
// create a List to contain the Contacts that are being created
List<Contact> newContacts = new List<Contact>();
for (Account acc: Trigger.new) {
// To create 2 contacts that are identical,
// first assign variables to all the elements so they can be re-used
String myFirstName = 'SFDCLiz';
String myLastName = 'SFDCKao';
String myAccId = acc.Id;
// Create your contacts and assign values to certain fields
Contact firstContact = new Contact();
firstContact.FirstName = myFirstName;
firstContact.LastName = myLastName;
firstContact.AccountId = myAccId;
Contact secondContact = new Contact();
secondContact.FirstName = myFirstName;
secondContact.LastName = myLastName;
secondContact.AccountId = myAccId;
// Add these contacts to a List
newContacts.add(firstContact);
newContacts.add(secondContact);
}
// Use the DML statement outside the FOR loop, for bulkification purposes
insert newContacts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment