+ Start a Discussion

Running existing data through new apex trigger

Say we want to deploy an apex trigger that acts as a counter, but we are deploying it into an existing org. What is the best way to update all the existing records to reflect the accurate count? I'm assuming since they won't have triggered the apex code, that the counter wont reflect accurately. Thanks!
Prafull G.Prafull G.
There are couple of ways I can think of now:
1. Use Developer Console: If record count is not much and can be processed in a single transaction than you can write a SOQL and then the trigger logic in developer console to execute. i.e. if Your trigger is on Account object than query account records and then execute the logic via console
2. If the record count is more and can not be handled than you can write batch class to fix existing data. batch class again should have query (to get list of records) and update counts logic in execute method. This is again one time so you can delete the class once data is corrected.


There are couple of ways of doing it after you have deployed the Trigger.


1.)Create a dummy field and Update that field for each record of the org through Data Loader which would invoke the Trigger to fire and do the required changes.


2.) Create a batch class and schedule it to run sometime so that it would update the existing and future records which are being created.For this also,you need to create a dummy field and update it through Batch class,this would make the process automated.