Last active
November 27, 2019 05:01
-
-
Save AlwaysThinkin/a1f6b9bb076d93ed0ea4b8260c63b5b6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 AccountTrigger on Account (after update) { | |
//prepare an empty Map of ID-to-Account for your updates | |
Map<ID, Account> acctMapToUpdate = new Map<Id, Account>(); | |
//prepare a for-loop you can use to compare old and new field values | |
for(Integer i = 0 ; i < trigger.new.size() ; i++){ | |
Account old = trigger.old[i]; | |
Account nw = trigger.new[i]; | |
//Evaluate Custom Settings to decide whether to execute actions | |
//if(Org_Exceptions__c.getOrgDefaults().Bypass_Triggers__c){//Returns only the Organization values, regardless of user | |
//if(Org_Exceptions__c.getInstance().Bypass_Triggers__c){//Returns values based on User and User's Profile | |
//if(Feature_Toggles__c.getValues('AccountTrigger').Activated__c){}//Returns setting for specific toggle record | |
//Check if the value of TickerSymbol changed | |
if(old.TickerSymbol != nw.TickerSymbol){ | |
//Open the account record for editing and change the Rating field | |
Account a = new Account(Id = nw.Id, Rating = 'Needs Evaluation'); | |
//Add the account record to your map | |
acctMapToUpdate.put(nw.Id, a); | |
} | |
} | |
//Check if your Map has any records | |
if(acctMapToUpdate.size() > 0){ | |
//update the account records in your map | |
update acctMapToUpdate.values(); | |
} | |
} |
This file contains hidden or 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 AccountTrigger on Account (after update) { | |
//prepare an empty Map of ID-to-Account for your updates | |
//prepare a for-loop you can use to compare old and new field values | |
//start for-loop | |
//Check if the value of TickerSymbol changed | |
//Open the account record for editing and change the Rating field | |
//Add the account record to your map | |
//end for-loop | |
//Check if your Map has any records | |
//update the account records in your map | |
} |
This file contains hidden or 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 TestAccountTrigger { | |
@testSetup | |
static void initData(){ | |
Account a0 = new Account(Name = 'Account Zero', Industry = 'Finance'); | |
insert a0; | |
Account a1 = new Account(Name = 'Account One', Industry = 'Finance'); | |
insert a1; | |
/* These require you to create Custom Settings and their Custom Fields before you can save it in Apex | |
Feature_Toggles__c toggles = new Feature_Toggles__c(Name = 'AccountTrigger', Activated__c = True); | |
insert toggles; | |
Org_Exceptions__c exception1 = new Org_Exceptions__c(Bypass_Triggers__c = True); | |
insert exception1; | |
*/ | |
} | |
static testMethod void testAll(){ | |
List<Account> getAccts = [Select ID from Account]; | |
List<Account> acctsToUpdate = new List<Account>(); | |
acctsToUpdate.add(new Account(Id = getAccts[0].Id, TickerSymbol = 'aZero')); | |
acctsToUpdate.add(new Account(Id = getAccts[1].Id, TickerSymbol = 'aOne')); | |
update acctsToUpdate; | |
List<Account> updatedAccts = [Select ID, Rating from Account]; | |
for(Account a : updatedAccts){ | |
System.assertEquals('Needs Evaluation', a.Rating, 'This was supposed to match if TickerSymbol changed'); | |
} | |
} | |
static testMethod void negativeTest(){ | |
List<Account> getAccts = [Select ID from Account]; | |
List<Account> acctsToUpdate = new List<Account>(); | |
acctsToUpdate.add(new Account(Id = getAccts[0].Id, Industry = 'Banking')); | |
acctsToUpdate.add(new Account(Id = getAccts[1].Id, Industry = 'Banking')); | |
update acctsToUpdate; | |
List<Account> updatedAccts = [Select ID, Rating from Account]; | |
for(Account a : updatedAccts){ | |
System.assertNotEquals('Needs Evaluation', a.Rating, 'This was supposed to match only if TickerSymbol changed'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment