You need to sign in to do that
Don't have an account?
kk1985
Trigger to sum the value of a particular field from the related list and store in custom object field
Hi, I have 2 custom objects...
1. Renewal (Renewal__c)
2. Module (Module__c)
I have used Lookup relationship between the 2 objects - Module Custom object is listed as the Related lists under Renewal object. I'm unable to use Master Detail relationship as i do not want the Master record to be deleted when a child is deleted.
Module custom object (Module__c) has a field called Price_with_Tax__c and
Renewal object has a field called Total_Amount_Modules__c.
Module will have multiple records added. I need to sum the total of Price_with_Tax__c field and store it in the Total_Amount_Modules__c field.
I found few samples for Opportunity object. I tried but couldnt get it to work. Can anyone help me with a sample trigger for this case ?
Thank you
1. Renewal (Renewal__c)
2. Module (Module__c)
I have used Lookup relationship between the 2 objects - Module Custom object is listed as the Related lists under Renewal object. I'm unable to use Master Detail relationship as i do not want the Master record to be deleted when a child is deleted.
Module custom object (Module__c) has a field called Price_with_Tax__c and
Renewal object has a field called Total_Amount_Modules__c.
Module will have multiple records added. I need to sum the total of Price_with_Tax__c field and store it in the Total_Amount_Modules__c field.
I found few samples for Opportunity object. I tried but couldnt get it to work. Can anyone help me with a sample trigger for this case ?
Thank you
Hi,
Please use below class and trigger:
public class OpportunityHandler
{
list<Renewal__c> oppList = new list<Renewal__c>();
set<id> oppIDs = new set<id>();
public void oppInsert(List<Module__c> oppLists)
{
for(Module__c opp : oppLists)
{
if(opp.Renewal__c!=null)
{
oppIDs.add(opp.Renewal__c);
}
}
aggregateResults();
}
public void oppUpdate(List<Module__c> oppLists,Map<Id,Module__c> oppOldmap)
{
for(Module__c opp : oppLists)
{
if(opp.Renewal__c!=null)
{
oppIDs.add(opp.Renewal__c);
if(oppOldmap.get(opp.id).Renewal__c!=opp.Renewal__c)
{
oppIDs.add(oppOldmap.get(opp.id).Renewal__c);
}
}
}
aggregateResults();
}
public void oppDelete(List<Module__c> oppLists)
{
for(Module__c opp : oppLists)
{
if(opp.Renewal__c!=null)
{
oppIDs.add(opp.Renewal__c);
}
}
aggregateResults();
}
public void oppundelete(List<Module__c> oppLists)
{
for(Module__c opp : oppLists)
{
if(opp.Renewal__c!=null)
{
oppIDs.add(opp.Renewal__c);
}
}
aggregateResults();
}
public void aggregateResults()
{
AggregateResult[] groupedResults = [SELECT SUM(Price_with_Tax__c), Renewal__c FROM Module__c where Renewal__c IN :oppIDs GROUP BY Renewal__c];
for(AggregateResult ar:groupedResults)
{
Id oppId = (ID)ar.get('Renewal__c');
Double sum = (DOUBLE)ar.get('expr0');
Renewal__c opp1 = new Renewal__c(Id=oppId);
opp1.Total_Amount_Modules__c= sum;
oppList.add(opp1);
}
if(oppList.size()>0 && oppList!=null)
{
update oppList;
}
}
***********************************************Trigger*************************************************************************************************
trigger oppTrigger on Module__c (after delete,after update, after insert, after undelete)
{
if(trigger.isInsert)
{
OpportunityHandler oppHandler =new OpportunityHandler();
oppHandler.oppInsert(Trigger.New);
}
else if(trigger.isDelete)
{
OpportunityHandler oppHandler =new OpportunityHandler();
oppHandler.oppDelete(Trigger.Old);
}
else if(trigger.isUnDelete)
{
OpportunityHandler oppHandler =new OpportunityHandler();
oppHandler.oppundelete(Trigger.New);
}
else if(trigger.isUpdate)
{
OpportunityHandler oppHandler =new OpportunityHandler();
oppHandler.oppUpdate(Trigger.New,Trigger.Oldmap);
}
}
Thanks
All Answers
Hi,
Please use below class and trigger:
public class OpportunityHandler
{
list<Renewal__c> oppList = new list<Renewal__c>();
set<id> oppIDs = new set<id>();
public void oppInsert(List<Module__c> oppLists)
{
for(Module__c opp : oppLists)
{
if(opp.Renewal__c!=null)
{
oppIDs.add(opp.Renewal__c);
}
}
aggregateResults();
}
public void oppUpdate(List<Module__c> oppLists,Map<Id,Module__c> oppOldmap)
{
for(Module__c opp : oppLists)
{
if(opp.Renewal__c!=null)
{
oppIDs.add(opp.Renewal__c);
if(oppOldmap.get(opp.id).Renewal__c!=opp.Renewal__c)
{
oppIDs.add(oppOldmap.get(opp.id).Renewal__c);
}
}
}
aggregateResults();
}
public void oppDelete(List<Module__c> oppLists)
{
for(Module__c opp : oppLists)
{
if(opp.Renewal__c!=null)
{
oppIDs.add(opp.Renewal__c);
}
}
aggregateResults();
}
public void oppundelete(List<Module__c> oppLists)
{
for(Module__c opp : oppLists)
{
if(opp.Renewal__c!=null)
{
oppIDs.add(opp.Renewal__c);
}
}
aggregateResults();
}
public void aggregateResults()
{
AggregateResult[] groupedResults = [SELECT SUM(Price_with_Tax__c), Renewal__c FROM Module__c where Renewal__c IN :oppIDs GROUP BY Renewal__c];
for(AggregateResult ar:groupedResults)
{
Id oppId = (ID)ar.get('Renewal__c');
Double sum = (DOUBLE)ar.get('expr0');
Renewal__c opp1 = new Renewal__c(Id=oppId);
opp1.Total_Amount_Modules__c= sum;
oppList.add(opp1);
}
if(oppList.size()>0 && oppList!=null)
{
update oppList;
}
}
***********************************************Trigger*************************************************************************************************
trigger oppTrigger on Module__c (after delete,after update, after insert, after undelete)
{
if(trigger.isInsert)
{
OpportunityHandler oppHandler =new OpportunityHandler();
oppHandler.oppInsert(Trigger.New);
}
else if(trigger.isDelete)
{
OpportunityHandler oppHandler =new OpportunityHandler();
oppHandler.oppDelete(Trigger.Old);
}
else if(trigger.isUnDelete)
{
OpportunityHandler oppHandler =new OpportunityHandler();
oppHandler.oppundelete(Trigger.New);
}
else if(trigger.isUpdate)
{
OpportunityHandler oppHandler =new OpportunityHandler();
oppHandler.oppUpdate(Trigger.New,Trigger.Oldmap);
}
}
Thanks
Hi,
Did you try to insert, delete or update Module records?
Thanks
The only thing this code did not cover is when the last child record is deleted. This code will not update the master to 0 when the last child record is deleted. This was very helpful 👌