Created
September 7, 2020 23:39
-
-
Save oakye/47dad0abf7f0a32a43712dc4f12f0051 to your computer and use it in GitHub Desktop.
Week2 homework, extra credit, & test classes from David Liu's Salesforce Development Curriculum (bit.ly/go-apex)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is my submission of Week 2's extra credit from David Liu's | |
// Salesforce Development Curriculum (bit.ly/go-apex) | |
// | |
// Write a trigger that automatically creates an Account | |
// whenever a Lead is created. The Account must be named after the last name of the Lead. | |
trigger CreateAccountFromLead on Lead (after insert) { | |
// create a List that is initially empty | |
List<Account> newAcct = new List<Account>(); | |
for (Lead newLead : Trigger.new) { | |
Account accLoop = new Account(); // create a new Account type variable | |
accLoop.Name = newLead.LastName; // the Account's Name will be the Lead's LastName | |
newAcct.add(accLoop); // use the add method to add that Account to the newAcct list | |
} | |
insert newAcct; // bulkification means you don't do DML statements within a FOR loop | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public class TestCreateAccountFromLead { | |
static testMethod void createNewLead() { | |
Lead testLead = new Lead(); | |
testLead.FirstName = 'Liz'; | |
testLead.LastName = 'TestClass'; | |
testLead.Company = 'TestCo 1'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public class TestUpdateContactEmail { | |
static testMethod void insertNewContact() { | |
Contact contactToCreate = new Contact(); | |
contactToCreate.FirstName = 'Liz'; | |
contactToCreate.LastName = 'KaoTest'; | |
contactToCreate.AccountId = '0013t00001Zw8tXAAR'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is my submission of Week 2's homework from David Liu's | |
// Salesforce Development Curriculum (bit.ly/go-apex) | |
// | |
// Write a trigger that automatically changes a | |
// Contact’s email address to “hello@world.com” whenever a Contact is created. | |
trigger UpdateContactEmail on Contact (before insert) { | |
for (Contact c : Trigger.new) { | |
c.Email = 'hello@world.com'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment