You need to sign in to do that
Don't have an account?
sen19
How to achive rollupsummary by using of agregate functions?(lookup relation between custom object)
How to achive rollupsummary by using of agregate functions?(lookup relation between custom object)
obj1 - count__c(custom field) - will calculate the total amount__c
obj2 - having lookup to obj1 - amount__c (available in obj2)
How can I achive this by aggregate functions?
obj1 - count__c(custom field) - will calculate the total amount__c
obj2 - having lookup to obj1 - amount__c (available in obj2)
How can I achive this by aggregate functions?
You need to write trigger on obj2. Please find below code where I used obj1 as parent and obj2 as child:
trigger trigRollup on obj2__c (after insert, after update, after delete) {
set<id> parentIds = new set<id>();
if(Trigger.isDelete)
{
for(obj2__c objchild : Trigger.Old)
{
parentIds.add(objchild.LookupField__c);
}
}
else
{
for(obj2__c objchild : Trigger.New)
{
parentIds.add(objchild.LookupField__c);
}
}
list<obj1__c> lstObj1 = new list<obj1__c>();
list<AggregateResult> lstAgrRes = [select LookupField__r.Id parentId,sum(amount__c) total from obj2__c where LookupField__r.id in :parentIds Group By LookupField__r.Id];
for(AggregateResult agr : lstAgrRes){
obj1__c objmaster = new obj1__c();
objmaster.Id = (Id)agr.get('parentId');
objmaster.Total_Amount__c = (Decimal) agr.get('total');
lstObj1.add(objmaster);
}
if(lstObj1.size() > 0)
update lstObj1;
}
Please let me know for any query on this and mark this as best answer if it helps you .
Best Regards
Tikam Sangwani
All Answers
You need to write trigger on obj2. Please find below code where I used obj1 as parent and obj2 as child:
trigger trigRollup on obj2__c (after insert, after update, after delete) {
set<id> parentIds = new set<id>();
if(Trigger.isDelete)
{
for(obj2__c objchild : Trigger.Old)
{
parentIds.add(objchild.LookupField__c);
}
}
else
{
for(obj2__c objchild : Trigger.New)
{
parentIds.add(objchild.LookupField__c);
}
}
list<obj1__c> lstObj1 = new list<obj1__c>();
list<AggregateResult> lstAgrRes = [select LookupField__r.Id parentId,sum(amount__c) total from obj2__c where LookupField__r.id in :parentIds Group By LookupField__r.Id];
for(AggregateResult agr : lstAgrRes){
obj1__c objmaster = new obj1__c();
objmaster.Id = (Id)agr.get('parentId');
objmaster.Total_Amount__c = (Decimal) agr.get('total');
lstObj1.add(objmaster);
}
if(lstObj1.size() > 0)
update lstObj1;
}
Please let me know for any query on this and mark this as best answer if it helps you .
Best Regards
Tikam Sangwani