You need to sign in to do that
Don't have an account?
Depton
trigger to get the sum of a child (look up) object field
Hi,
I have 2 objects:
Milestones, and Actions, Actions is the child object (look up relationship)
In the Actions object I have a field called "total hours"
I would like to get the sum of the "total hours" in a Milestone object field called “Sum of Hours”
As there is no master detail relationship so I cannot create a roll up summary field.
I guess it can be done with a trigger. Does anyone have a sample trigger for this?
Thank you!
Solved it:
:))
Thank you guys for your great help!!!
All Answers
You should to crate Trigger on Child Object
Below code definately help , just check API fild name and Object Name & Data Type.....
Please mark this post as solution once it is accepted..
trigger OnActionsTrigger On Actions__c(after insert, after update){
set<Id> setMilestonesid=new set<Id>();
Map<string,Decimal> MilStoneWithTotalActionHours=new Map<string,Decimal>();
for(Actions__c acs:Trigger.New){
if(acs.MileStones_c!=null && acs.Hours!=null){
setMilestonesid.add(setMilestonesid);
}
}
if(setMilestonesid.size()>0){
List<Milestones__c> ListMileStones=[Select Id, TotalHours__c from Milestones where MileStones__c =:setMilestonesid];
List<Actions__c> ListMileStoneActions=[Select Id, Hours__c from Actions__c where MileStones_c =:setMilestonesid where Hours__c!=null];
for(Id MileId:setMilestonesid){
Decimal totHours=0;
for(Actions__c acs: ListMileStoneActions){
if(MileId=acs.MileStones_c){
totHours=totHours + acs.Hours__c ;
}
}
MilStoneWithTotalActionHours.put(MileId,totHours);
}
}
List<Milestones__c> UpdateMilestones=new List<Milestones__c>();
for(Milestones__c MC:ListMileStones){
MC.TotalHours__c=MilStoneWithTotalActionHours.get(MC.id);
UpdateMilestones.add(MC);
}
if(UpdateMilestones.size()>0){
update UpdateMilestones;
}
}
You'll also need a trigger to handle when records are deleted (so that you can reduce the sum) and when they are undeleted (which should apply its value once again to the sum). Kritin's example should be straightforward to adapt.
This is how it looks now with the API names:
Milestone1_Project__c: is the Project object
Milestone1_Milestone__c is : is the milestones object (child)
Sum_of_Actual_Hours__c is the custom field in Projects
Total_Actual_Hours__c is the custom field in Milestones
I got this error:
Error: Compile Error: unexpected token: 'where' at line 12 column 152
In this line:
List<Milestone1_Milestone__c> ListMilestones=[Select Id, Total_Actual_Hours__c from Milestone1_Milestone__c where Milestone1_Project__c=:setProjectid where Total_Actual_Hours__c!=null];
???
Thank you!!!
I'd imagine the second 'where' should be replaced with 'and'
Thank you Bob,
Now I got this error:
Error: Compile Error: Variable does not exist: ListProjects at line 27 column 31
It is because is inside the loop?
Thank you guys!!
Its because ListProjects is declared inside an if statement and used outside.
You should probably initialise this outside the loop to an empty list:
Thanks Bob!! (once again) not only for the help but your clarifications
I am Still getting one more error: (I tried to look at the boards help, web, and Apex developer guide without success...:(
setProjectid.add(setProjectid);
Thank you!!
That's a strange line - its adding itself to the set of ids, but as its a set that doesn't work. Looks like it should be the following to me:
as this is managing the project ids that will be retrieved.
this is what i got and getting this:
bit confuse with :
for(Milestone1_Milestone__c mst:Trigger.New){
if(mst.Milestone1_Project__c!=null && mst.Hours!=null){
setProjectId.add(mst.Milestone1_Project__c);
}
}
:)
Milestone1_Project__c is used all over the place - this is the project relationship on your milestone. Simply replace that with the real field I'd guess.
Solved it:
:))
Thank you guys for your great help!!!