+ Start a Discussion
knightknight 

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);
}

ScoobieScoobie

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

knightknight

we cannot use roll up summary for this particular requirement.it should be a trigger

ScoobieScoobie

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.

knightknight

sorry.. i am not sure if I get it.. can you please help ?

Jeremy.NottinghJeremy.Nottingh

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:

 

 

Trigger License on License_Transfer__c (before update) {
  set<id> affectedDNBOs = new set<id>();
  for (License_Transfer__c lct : Trigger.New) {
    //build set of DNB_Org ids. 
    if (lct.License_Type__c == 'UE') affectedDNBOs.add(lct.Holding_Org__c);
  }
  
  //query for the DNB_Org__c's we want. Assuming the name of the relationship is License_Transfers__r. Check in your org if it doesn't work.
  list<DNB_Org__c> dnborgstoupdate = [select Holding_Licenses_UE__c, (select Licenses_to_Transfer__c from License_Transfers__r) from DNB_Org__c where id in :affectedDNBOs];

  //now you've got your list. Go through it and do whatever you need to do
  for (DNB_Org__c d : dnborgstoupdate) {
    //logic...
  }
  update dnborgstoupdate;

 

See if that gets you going.

 

Jeremy 

 

ScoobieScoobie

 

 

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);
}


 

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.
}

 

NaishadhNaishadh

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.

snubby2929snubby2929

How do you do this on roll up sumarry fields?