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
Rachel LinderRachel Linder 

Need to Write a Trigger to Convert a Formula Field on a Child Record to a Number/Value

I need to help writing a trigger as I am not a developer.

We have a parent object called "Projects" and a child object called "Project Work Products". On the child object we have a formula field called "Min Docs Formula" which places a 1 or 0 in the field.

On the parent object ("Projects") I would like a field that counts the project work products "Min Docs Formula" field output (1 or 0). Currently I can't accomplish this as the "Min Docs Formula" field is a formula. The output of the "Min Docs Formula" field needs to be converted into a number/value field called "Min Docs Value", which should allow me to have a field on the parent object ("Project") called "Min Docs Sum" that adds the values from the "Min Docs Value" fields.

Can someone help with writing the trigger to this conversion from "Min Docs Formula" to "Min Docs Value"?
AmulAmul
Hi Rachel,

This will help you.

trigger ProjectWorkTrigger on Project_Work_Products__c(after insert, after update){

set<Id> setProjectId=new set<Id>();

for(Project_Work_Products__c ProjWorkProd:Trigger.New){
    if(ProjWorkProd.Project__c!=null){
        setProjectId.add(ProjWorkProd.Project__c);
    }

}


Map<Id, Project__c> MapProject=new Map<Id, Project__c>([Select id, Min_Docs_Sum__c,name,(Select id,Min_Docs_Formula__c from Project_Work_Products__r where Min_Docs_Formula__c!=0) from Project__c where id in:setProjectId]);

list<Project__c> updateProject=new list<Project__c>();

for(Project__c proj: MapProject.values()){
    list<Project_Work_Products__c> lstProjectWorkProducts=proj.Project_Work_Products__r;
    proj.Min_Docs_Sum__c=lstProjectWorkProducts.size();
    updateProject.add(proj);
}


if(updateProject.size()>0)
    update updateProject;




}
KaranrajKaranraj
Rachel - You don't have to write trigger for this scenario. With simply new custom field with number type and workflow rule you can solve this problem.I understand that both objects are master-detail relationship, since the field is formula field you can't able to create rollup summary field in parent. Let solve this problem by clicks.

Follow the belwo steps

1. Crete new custom field in the child object with the 'Number' type. You can name that as 'Min Docs Value'
2. Create workflow rule in the child object with any rule criteria and set the action as every time update or edited
3. In the workflow action, choose Field update.
4. In the Field to update option select the new field which we created now 'Min Docs Value'
5. In the fomula editor copy and paste the below formula
Value(Min_Docs_Formula__c)
6. Save your workflow and activate.

The above steps is to convert your formula field into number field. Now simply this logic by creating new records, whether its copying the value from formula field and put it in our custom field. Now create rollup summary field in Parent object and choose our Min Docs Value with SUM operation.