Roll Up Amount on Parent Object from Child Object In Lookup Relationship
Requirement : Write a Trigger to roll up amount from Student object to Teacher Object
Teacher as parent object and Student as a Child Object
Solution :
trigger RollUpAmountonTeacher on Student__c (after delete, after insert, after update)
{
set<id> setTeacherID = new set<id>();
List<Teacher__c> lstTeacherToUpdate = new List<Teacher__c>();
if(trigger.isAfter)
{
if(trigger.isDelete)
{
for(Student__c objStu : trigger.old)
{
setTeacherID.add(objStu.Teacher__c);
}
}
else
{
for(Student__c objStu : trigger.new)
{
if(trigger.isInsert)
{
setTeacherID.add(objStu.Teacher__c);
}
if(trigger.isUpdate)
{
setTeacherID.add(Trigger.oldmap.get(objStu.id).Teacher__c);
}
}
}
if(setTeacherID != null)
{
map<id,decimal> mapTeacherIdToAmount = new map<id,decimal>();
for(AggregateResult objAggResult : [select Teacher__c, SUM(Amount__c)
from Student__c
where Teacher__c IN :setTeacherID group by Teacher__c])
{
mapTeacherIdToAmount.put((id)objAggResult.get('Teacher__c'),(decimal)objAggResult.get('expr0'));
}
if(setTeacherID != null)
{
//Get the sum value from the map and create a list of Account to update
for(Teacher__c objTeacher : [Select id,Total_Amount__c
from Teacher__c where Id IN :setTeacherID])
{
Double valCreditNoteAmount = mapTeacherIdToAmount.get(objTeacher.Id);
if(valCreditNoteAmount != null){
objTeacher.Total_Amount__c = valCreditNoteAmount ;
}
lstTeacherToUpdate.add(objTeacher);
}
if(lstTeacherToUpdate.size() > 0)
{
update lstTeacherToUpdate;
}
}
}
}
}
Note: In case of any issue feel free call to me @9711055997
Thanks,
Sumit Shukla
9711055997
sumitshukla.mca@gmail.com
cccinfotech.com
Teacher as parent object and Student as a Child Object
Solution :
trigger RollUpAmountonTeacher on Student__c (after delete, after insert, after update)
{
set<id> setTeacherID = new set<id>();
List<Teacher__c> lstTeacherToUpdate = new List<Teacher__c>();
if(trigger.isAfter)
{
if(trigger.isDelete)
{
for(Student__c objStu : trigger.old)
{
setTeacherID.add(objStu.Teacher__c);
}
}
else
{
for(Student__c objStu : trigger.new)
{
if(trigger.isInsert)
{
setTeacherID.add(objStu.Teacher__c);
}
if(trigger.isUpdate)
{
setTeacherID.add(Trigger.oldmap.get(objStu.id).Teacher__c);
}
}
}
if(setTeacherID != null)
{
map<id,decimal> mapTeacherIdToAmount = new map<id,decimal>();
for(AggregateResult objAggResult : [select Teacher__c, SUM(Amount__c)
from Student__c
where Teacher__c IN :setTeacherID group by Teacher__c])
{
mapTeacherIdToAmount.put((id)objAggResult.get('Teacher__c'),(decimal)objAggResult.get('expr0'));
}
if(setTeacherID != null)
{
//Get the sum value from the map and create a list of Account to update
for(Teacher__c objTeacher : [Select id,Total_Amount__c
from Teacher__c where Id IN :setTeacherID])
{
Double valCreditNoteAmount = mapTeacherIdToAmount.get(objTeacher.Id);
if(valCreditNoteAmount != null){
objTeacher.Total_Amount__c = valCreditNoteAmount ;
}
lstTeacherToUpdate.add(objTeacher);
}
if(lstTeacherToUpdate.size() > 0)
{
update lstTeacherToUpdate;
}
}
}
}
}
Note: In case of any issue feel free call to me @9711055997
Thanks,
Sumit Shukla
9711055997
sumitshukla.mca@gmail.com
cccinfotech.com
Comments
Post a Comment