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
Menachem EjdelmanMenachem Ejdelman 

trigger to update a number field automatically when a formula field value changes

Hello,

I am trying to create a trigger that makes a number field automatically be populated with data that is in a formula field whenever the formula field is changed. (regardless of whether a user edited the record or not).

The code of the formula field (Parent_number_reached__c) is as follows: Parent_Object__r.Number_Reached__c
The Number field is called Parent_number_reached_local__c

I want Parent_number_reached_local__c to have the data be synchronized with Parent_number_reached__c, so that whenever Parent_number_reached__c is changed, change Parent_number_reached_local__c to the same value.

I am told that this is a relatively simple Trigger to write. Please send me such a code.

Thank you,
Menachem
RavitejaRaviteja

Hi,

Write trigger on parent object to populate same value in child as well.

please refer below,
trigger testTrigger on Parent_Object__c (after update){
map<id,Parent_Object__c> parentids =new map<id,Parent_Object__c>();
for(Parent_Object__c c :trigger.new){
parentids.put(c.id,c);
}
List<Childrecords> lstchilds =[select id,Parent_number_reached_local__c from child where parentid in :parentids.keyset()];


for(Childrecords cc : lstchilds ){

cc.Number field is called Parent_number_reached_local__c = parentids.get(parent ID).parentfield;
}
update lstchilds;
}
Menachem EjdelmanMenachem Ejdelman
Hi srirama, Thank you for your quick reply. I am getting the following error: "Compile Error: expecting a semi-colon, found 'is' at line 11 column 16". Also, No where in this code do I see the Number field in the Parent Object (called Number_Reached__c). There must be something in this code that tells the system to put the value of Number_Reached__c to be put into Parent_number_reached_local__c. Menachem Ejdelman Technology Manager Big Tent Judaism/Jewish Outreach Institute 1270 Broadway, Ste. 609 New York, NY 10001 212.760.1440 x120 212.760.1569 (fax) www.joi.org
RavitejaRaviteja
Hi Meachem,

Apologies, My mistake..copied some extra text from above.Please modify this for loop.


for(Childrecords cc : lstchilds ){

cc.Parent_number_reached_local__c = parentids.get(parent ID).Number_Reached__c;

}

Please ping me in Skype at 'raviteja.epuri' in case of any query.or drop me mail at raviteja8899@gmail.com. Happy to Help :)