You need to sign in to do that
Don't have an account?
cambart14
Finding Account ID if External ID Contains Value
Below is our code to find the account ID for inserted child records that have a unique identify (R12 Customer Number). This works great if the R12 Customer Number is a unique match, but we are now consolidating accounts in SFDC and this may contain a string of customer numbers on the account level (example, R12 Customer Number = 'GEN12412;GEN12342;GEN12542' rather than R12 Customer Number = 'GEN12412').
I am trying to figure out how a child record being inserted with the R12 Customer Number = 'GEN12412' can find the right account ID if on the account the R12 Customer Number = 'GEN12412;GEN12342;GEN12542'. The child record needs to trigger a contains search, but I am not certian how that would look in an Apex Trigger.
I am trying to figure out how a child record being inserted with the R12 Customer Number = 'GEN12412' can find the right account ID if on the account the R12 Customer Number = 'GEN12412;GEN12342;GEN12542'. The child record needs to trigger a contains search, but I am not certian how that would look in an Apex Trigger.
trigger UpdateCPInvoiceHeaderId on Customer_Order_Header__c (before insert) { //get the R12 Customer Numbers and store it in a set. set<String> acct = new set<String>(); set<String> acct2 = new set<String>(); for(Customer_Order_Header__c i: trigger.new){ if(i.R12_Customer_Number__c != null){ acct.add(i.R12_Customer_Number__c); } } //query the account records and get the associated IDs map<string, Account> acctMap = new map<string, Account>(); for(Account a:[SELECT Id, R12_Customer_Number__c FROM Account WHERE R12_Customer_Number__c IN: acct]){ acctMap.put(a.R12_Customer_Number__c,a); } //update the account value based on the R12 Customer Number in the record for(Customer_Order_Header__c i:trigger.new){ if(i.R12_Customer_Number__c !=null && acctMap.containsKey(i.R12_Customer_Number__c)){ i.Account__c=acctMap.get(i.R12_Customer_Number__c).Id; } } }
Won't be as good as a map, but for these kind of situation map doesn't help. Assumption is that you don't have too many accounts. It is better to put a Limit in the SOQL query for safer side.
--
Abhi
If my answer helps to solve the issue/problem/doubt, please mark it as Best Answer; so that people who are stuck in the similar issue get benefitted.
All Answers
Won't be as good as a map, but for these kind of situation map doesn't help. Assumption is that you don't have too many accounts. It is better to put a Limit in the SOQL query for safer side.
--
Abhi
If my answer helps to solve the issue/problem/doubt, please mark it as Best Answer; so that people who are stuck in the similar issue get benefitted.
for(Account acc: acctList) {
if(coh.R12_Customer_Number__c == acc.R12_Customer_Number__c) {
coh.Account__c=acc.Id;
break;
}
}
if(coh.R12_Customer_Number__c == acc.R12_Customer_Number__c) {
line 18 now reads:
if(acc.R12_Customer_Number__c.contains(coh.R12_Customer_Number__c)) {