Last active
June 8, 2019 14:53
-
-
Save AlwaysThinkin/d27ff7c2f82892cba712 to your computer and use it in GitHub Desktop.
After Trigger and its unit test
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
trigger ContactAfterTrigger on Contact (after insert) { | |
List<Contact> consToUpdate = new List<Contact>(); | |
for(Contact conInTrigger : trigger.new){ | |
Contact c = new Contact();//Create a new instance of an as-yet-unidentified Contact | |
c.Id = conInTrigger.Id;//Set that Contact's ID to an existing ID from the Contacts in Trigger.new | |
c.Department = 'IT';//Change a field value on that Contact | |
consToUpdate.add(c);//Add it to our List so we can update it later when all Contacts in Trigger.new have been prepared | |
} | |
update consToUpdate;//Perform the DML update call outside of our for-loop to avoid Governor Limits (150 DML per transaction)! | |
} |
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 ContactAfterTriggerTest { | |
public static testMethod void testMany(){ | |
//Must create a test Account for the test Contacts! | |
Account a = new Account(Name = 'Test Account'); | |
insert a; | |
//Use a for-loop to populate a List of test Contacts | |
List<Contact> testContacts = new List<Contact>(); | |
for(Integer i = 0; i < 2 ; i++){ | |
Contact c = new Contact(LastName = 'name' + i,//Use i to make each name unique | |
AccountId = a.Id); | |
testContacts.add(c); | |
} | |
insert testContacts; | |
//Bulk Tests also require bulk assert tests | |
for(Contact c : [Select Department from Contact]){ | |
System.assertEquals('IT', c.Department); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment