Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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