You need to sign in to do that
Don't have an account?
![dmsx2od dmsx2od](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Simple trigger not working
I've got a trigger that is so simple that I can't find a reason why it wouldn't work - and the debug statements show the proper values - but nothing happens when I save the account:
trigger accountFillSite on Account (before insert, before update) { for (Account a: [select id, Site, BillingCity, BillingState FROM Account WHERE id IN :Trigger.New]){ System.Debug('Account Site is ' + a.Site + ' City: '+a.BillingCity+' State: '+a.BillingState); a.Site = a.BillingCity + ', ' + a.BillingState; system.debug('a.Site: '+a.Site); } }
I'm baffled as to why this won't work.
At this point, I'm less concerned with whether or not I'm using proper technique, and more concerned with it working on one record at a time. I edit the record, and click Save.
All field permissions are set appropriately. Debug logs show all values set properly. Even the last debug line, a.Site, shows the expected value.
Thanks,
David
I think you are setting the values in the data set retrieved by the Query (ie Select...) and not the Trigger.new array. Yes, the records are the same but the context of how the trigger works and how it updates the Trigger.new records won't be applied to records you specifically query. You would have to explicitly Update those records. But that is inefficient and unnecessary.
Try this simple re-write. I only removed the Query and instead reference the Trigger.new Account records directly in the for loop.
trigger accountFillSite on Account (before insert, before update) { for (Account a :Trigger.New){ System.Debug('Account Site is ' + a.Site + ' City: '+a.BillingCity+' State: '+a.BillingState); a.Site = a.BillingCity + ', ' + a.BillingState; system.debug('a.Site: '+a.Site); } }
All Answers
I think you are setting the values in the data set retrieved by the Query (ie Select...) and not the Trigger.new array. Yes, the records are the same but the context of how the trigger works and how it updates the Trigger.new records won't be applied to records you specifically query. You would have to explicitly Update those records. But that is inefficient and unnecessary.
Try this simple re-write. I only removed the Query and instead reference the Trigger.new Account records directly in the for loop.
trigger accountFillSite on Account (before insert, before update) { for (Account a :Trigger.New){ System.Debug('Account Site is ' + a.Site + ' City: '+a.BillingCity+' State: '+a.BillingState); a.Site = a.BillingCity + ', ' + a.BillingState; system.debug('a.Site: '+a.Site); } }