Created
July 26, 2017 19:37
-
-
Save bryanroscoe/29cb413d83ebe55218c79685a80a4176 to your computer and use it in GitHub Desktop.
trigger
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 PullOrderSiteAccount on Orders__c (before insert) { | |
| // Collect site codes for duplicate checking | |
| Set<String> codes = new Set<String>(); | |
| for (Orders__c order : trigger.new) codes.add(order.Site_Code__c); | |
| // Find accounts with matching site codes | |
| Map<String, List<Account>> siteAccounts = new Map<String, List<Account>>(); | |
| for (Account record : [SELECT Site_Code_ID__c FROM Account WHERE Site_Code_ID__c IN :codes] | |
| ) | |
| // Match the two in a map | |
| { | |
| if (!siteAccounts.containsKey(record.Site_Code_ID__c)) | |
| siteAccounts.put(record.Site_Code_ID__c, new List<Account>()); | |
| siteAccounts.get(record.Site_Code_ID__c).add(record); | |
| } | |
| // Only update the lookup if one account matches, else do not update | |
| for (Orders__c order : trigger.new) | |
| { | |
| List<Account> matchingAccounts = siteAccounts.get(order.Site_Code__c); | |
| Id parentId = (matchingAccounts != null && matchingAccounts.size() == 1) ? | |
| matchingAccounts[0].Id : null; | |
| order.Site_Account__c = parentId; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment