I am trying to write a trigger that will take a user's input into a text field Orders__c.Site_Code__c
and find the account which has the matching value in Account.Site_Code__c
.
My trigger passes its test with 100% coverage but when trying to create a new Orders__c
I get the following error:
Apex trigger PullOrderSiteAccount caused an unexpected exception, contact your administrator: PullOrderSiteAccount: execution of AfterInsert caused by: System.StringException: Invalid id: 1234: External entry point
Where did I go wrong?
--snip-- Working trigger!
trigger PullOrderSiteAccount on Orders__c (before insert) {
Set<String> codes = new Set<String>();
for (Orders__c o : trigger.new) codes.add(o.Site_Code__c);
Map<String, Account> siteAccounts = new Map<String, Account>();
for (Account a : [
SELECT Site_Code__c FROM Account
WHERE Site_Code__c IN :codes
]) siteAccounts.put(a.Site_Code__c, a);
for (Orders__c o : Trigger.new)
{
Account siteAccount = siteAccounts.get(o.Site_Code__c);
Id parentId = (siteAccount == null) ? null : siteAccount.Id;
o.Site_Account__c = parentId;
}
}
updated:
This now works!