Skip to content

Instantly share code, notes, and snippets.

@oakye
Created September 7, 2020 23:39
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/47dad0abf7f0a32a43712dc4f12f0051 to your computer and use it in GitHub Desktop.
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 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
}
@isTest
public class TestCreateAccountFromLead {
static testMethod void createNewLead() {
Lead testLead = new Lead();
testLead.FirstName = 'Liz';
testLead.LastName = 'TestClass';
testLead.Company = 'TestCo 1';
}
}
@isTest
public class TestUpdateContactEmail {
static testMethod void insertNewContact() {
Contact contactToCreate = new Contact();
contactToCreate.FirstName = 'Liz';
contactToCreate.LastName = 'KaoTest';
contactToCreate.AccountId = '0013t00001Zw8tXAAR';
}
}
// 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