You need to sign in to do that
Don't have an account?
Help with trigger--update parent object based on the data in child records
Hello
I am a newbie to triggers and was hoping that someone could help me with this ...Basically I am trying to update a field in my parent object whenever some updates/creates new child records. Below is what I have so far ( both parent and child are custom objects). To make it easier ...this would be like if I have an amount field on the account object and I need to copy the opty amount to this field on the account object. Basically the account amount field should be the sum of all the opty amount fields for the corresponding account ..
trigger License on License_Transfer__c (before update) {
List<DNB_Org__c> dnborgstoupdate = new List <DNB_Org__c>();
for(License_Transfer__c lct : System.Trigger.New) {
if (lct.License_Type__c == 'UE'){
DNB_Org__c dnborg = new DNB_Org__c (Id =lct.Holding_Org__c,Holding_Licenses_UE__c = lct.Licenses_to_Transfer__c);
dnborgstoupdate.add(dnborg);
}
}
update(dnborgstoupdate);
}
Have you considered making your custom objects have a Master-Detail relationship and using Roll-Up Summary Fields?
This would get rid of the need for any triggers
we cannot use roll up summary for this particular requirement.it should be a trigger
In that case you may want to try selecting the parent record rather than creating a new object in memory and then trying to update it.
sorry.. i am not sure if I get it.. can you please help ?
First, it seems like you should have this Trigger run on insert and delete also, but I'm not sure.
Anyway, what you need is a list of DNB_Org__c, and then go through that list to do your logic. Your question seems to be, "How do I find the list of affected DNB_Org__c's?" Try this:
See if that gets you going.
Jeremy
You are trying to update an object you have only created in memory. What you should possibly try is:
if (lct.License_Type__c == 'UE'){
DNB_Org__c dnborg = new DNB_Org__c (Id =lct.Holding_Org__c,Holding_Licenses_UE__c = lct.Licenses_to_Transfer__c);
dnborgstoupdate.add(dnborg);
}
}
insert(dnborgstoupdate);
After this you can then update your DNB_Org__c object and use the update DML statement.
}
After spring 10 release, if user make any changes in child object it will automatically fire parent object trigger(if exists). So you don't need to worry about child object trigger. just put your login in parent object trigger and it will work.
How do you do this on roll up sumarry fields?