You need to sign in to do that
Don't have an account?
Trigger - Before Insert Not Working
I have a trigger on a Custom Object (Ordered Ads). The object is a Master/Detail with the Standard Object, "Account" (Account is the master).
When inserting the Ordered Ads each day, I have a trigger that *should* associate the Ordered Ad record with the appropriate Account record based on AccountNumber. However, it's not working. When I attempt to enter a new record, I get an error message stating that I must enter a value in the Account__c field. I would greatly appreciate any thoughts / advice on where I'm going wrong here...
trigger updateOrderedAdsAccount on Ordered_Ads__c (before insert) { //Create record set of Account Numbers to tie to Ordered Ads Set<String> acctnums = new Set<String>(); for(Ordered_Ads__c a:trigger.new) { acctnums.add(a.Admarc_Number__c); } //Create empty Maps to hold relationships. MAP<String, Id> orderedAdsAcctRel = new Map<String, Id>(); //Iterate through Accounts and find matches for ID based on Account Number for (Account u:[select id, AccountNumber from Account where External_Id__c != null AND External_Id__c in :acctnums limit 1]) { if(u.AccountNumber != null) { String acctnum = u.AccountNumber; orderedAdsAcctRel.put(acctnum, u.id); } else { String acctnum = u.AccountNumber; orderedAdsAcctRel.put(acctnum, '0014000000Ektmx'); } } //Iterate through Ordered Ads and update according to Account data from Map for (Ordered_Ads__c a:trigger.new) { Id u1 = orderedAdsAcctRel.get(a.Admarc_Number__c); a.Account__c = u1; if (a.Account__c = '') { a.Account__c = '0014000000Ektmx'; } } }
I do have a test case, and it passes the test case only when I select an existing account. If I try to create a new account through the Test Case, I get the same error (Must supply a value for Account__c).
I have the trigger in my Sandbox, and it does work there. The means for moving to Production was through the SFDC Deploy feature.
Again, I appreciate any advice on this.
Thanks.
Please ignore the post listed above. The Salesforce.com Developer who created said post was temporarily out of his mind. Having returned, the error of his ways was quickly realized, and the proper code put in place to alleviate any issues moving forward.
For any who may be similiarly challenged; It helps to make sure there is actual data in the field you might be referencing. :smileysurprised:
Funny how that works...