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
Sumant HegdeSumant Hegde 

How to make the new trigger work in the production which updates particular field


I have a situation here. I have created a trigger to update the field value present in the parent object whenever a child object gets created or updated. The trigger is created on parent object. I have pushed the code in the production, but the problem is unless parent or the child object gets refreshed or updated, the trigger is not getting hit. This results in stale value in the filed that was supposed to be updated. 

Is there any easy way that I can refresh all the production data so that the trigger gets hit and update the field value.?
Since this is a priority issue, any help would be highly appreciated.

Niraj Kr SinghNiraj Kr Singh
First things, you have written your trigger on Parent object whereas you are doing your event create/update on child object. That is not getting a proper flow.
It should be like, Trigger should be on Child object and while creating/updating child record then Parent records should get updated.

Now for your specific questions, There are way to updated your production data:
1. Export your all production data and update the particular field value in csv file and Use Dataloader/ Bulk API
2. Create Batch class and execute to update your field

Saurabh Sood 12Saurabh Sood 12
Hi Sumant,
I appreciate your question but firstly you have execute a flow on paper what you are trying to do and check relationship b/w two objects.
See whether we can achive this things with Description way or not means with automated tools if non of solutions meet your requirement then go for trigger.
Can you post your trigger.. I will make correction on that.
Or Check this try might be helpful for you.
Sumant HegdeSumant Hegde
Thanks all of you for your suggestions. 

I understand that I should not be writting trigger on the parent, but apparently for my scenario, my trigger is working fine whenever the child is updated or inserted. 
According to my understanding, even if I write the trigger on the child object, it will execute only when some operations is being performed on the child object. So, I have to refresh the data, once I upload the trigger into the production environment. Please correct me if I am wrong.

At the moment, I need to find a way to refresh the data for the first time. Because if my trigger executes at least once on each parent object, then everything will be good.

Sumant HegdeSumant Hegde

I could use "Data Loader" to refresh the data. It worked for other triggers, but not for the trigger that I mentioned earlier in this thread. 
The issue is, when I refresh one item at a time, it is working and when I run a job or refresh bulk data using data loader, it is not hitting this particular trigger. 

I tried writing the trigger in two different ways
1. Retrieved the count of unique child records using SOQL AggregateResults and then assinged the size of the result to the field of the parent object.
2. Retrieved count of child records and added them to the set(Since it take only unique values). Then assigned the size of the set to the field of parent object.

In both the case, it is not working whenever I tried to update the data in bulk. It is only working for single data at a time.
Unfortunately, I can't post the code because of the confidentiality issue. So, if anyone can help me without asking my actual code that would be very helpful. Anyway, I will try to post similar code as mine soon.
Sumant HegdeSumant Hegde
My code is something like this.

trigger triggerRollupChild on Department__c(before insert, before update)
   AggregateResult[] groupedResults = [select Student_Name__c from Course__c where Department_Name__c in GROUP BY Student_Name__c HAVING count(Student_Name__c)>=1];

        d.StudentCount__c = 0;
  for(Course__c c:[select id,Department_Name__c,Student_Name__c from Course__c where Department_Name__c in])
          Trigger.newMap.get(c.Department_Name__c).StudentCount__c = groupedResults.size();

Can somebody help me correcting this trigger please?!