You need to sign in to do that
Don't have an account?
srikanth cheera 11
Use Trigger
Here Child is Student
Parent is College
I have one custom field in Student fee
And in Parent Total Amount
Whenever student joined to college Total Amount will be update using trigger
Parent is College
I have one custom field in Student fee
And in Parent Total Amount
Whenever student joined to college Total Amount will be update using trigger
I will suggest you to start learning from Trailhead.
1) https://trailhead.salesforce.com/modules/apex_triggers
Try to write code from your self and post your code here if you will get any issue we will help you
Please copy and run this given code.
This code is working for insert, update, delete student and update college total amount according to the student.
This code is simple and easy to understand for new salesforce developer.
//Helper Class//
public class StudentJoinToCollege {
public static void getFee(List<Student__c> stuList){
Set<Id> setCollegeIds = new Set<Id>();
for(Student__c stu:stuList){
if(stu.College__c!=null){
setCollegeIds.add(stu.College__c);
}
}
List<College__c> collegeList = new List<College__c>();
List<College__c> collegeList2 = new List<College__c>();
collegeList = [SELECT Total_Amount__c,(SELECT Fee__c FROM Students__r ) FROM College__c WHERE ID IN:setCollegeIds];
for(College__c col:collegeList)
{
Decimal TotalAmt=0;
if(col.Students__r.size() >0){
for(Student__c st:col.Students__r)
{
if(st.Fee__c!=null)
{
TotalAmt+=st.Fee__c;
}
}
col.Total_Amount__c = TotalAmt;
collegeList2.add(col);
}
if(collegeList2!=null){
update collegeList2;
}
}
}
}
//Trigger Class//
trigger StudentJoinToCollegeTrigger on Student__c (after insert,after update,after delete) {
if(Trigger.isInsert && Trigger.isAfter){
StudentJoinToCollege.getFee(Trigger.New);
}
if((Trigger.isAfter && Trigger.isUpdate) || (Trigger.isAfter && Trigger.isdelete)){
StudentJoinToCollege.getFee(Trigger.Old);
}
}
Please mark it as best Answer if you find it helpful.
Thank You
Ajay Dubedi