function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dmsx2oddmsx2od 

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

 

 

Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

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

aalbertaalbert

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); } }

 

 

 

This was selected as the best answer
dmsx2oddmsx2od
Perfect.  Thanks!