Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
David Hien 8

# We have two custom Object Student and College.

We have two custom Object Student and 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
Best Answer chosen by David Hien 8
Ajay K Dubedi
Hi David,

Please copy and run this given below code.
This code is working for insert, update, delete student and update college total amount according to a student.
This code 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){
}
}
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;
}
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);
}

}```

Thank You
Ajay Dubedi

Travis Malle 9
Happy to help on this, but we'll need a lot more information. Do you have a trigger that is currently not working?
Abdul Khatri
1. If the Relationship between Student and College Object is Master Detail you can achieve that by having a Roll-up Summary Field on the College Object referencing the Student field fee.
2. If you want to go with trigger, here is the code. Plese note I just faked the Object Names as per my understanding which may differ what you have so update the field and object names accordingly
```trigger UpdateCollegeFee on Student__c (after insert) {

Set<Id> idCollegeSet = new Set<Id>();
List<College__c> collegeToUpdateList = new List<College__c>();

for(Student__c student : trigger.new) {

if(student.College__c == null) continue;

}

if(idCollegeSet.isEmpty()) return;

AggregateResult[] groupedResults = [SELECT SUM(fee__c) total, College__c FROM Student__c WHERE College__c IN :idCollegeSet GROUP BY College__c];

for(AggregateResult ar : groupedResults) {

Id idCollege = (Id)groupedResults[1].get('College__c');
College__c college = new College__c (Id = idCollege);
college.Total_Amount__c = (Integer)groupedResults[0].get('total');

}

if(collegeToUpdateList.isEmpty())
update collegeToUpdateList;
}```
Ajay K Dubedi
Hi David,

Please copy and run this given below code.
This code is working for insert, update, delete student and update college total amount according to a student.
This code 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){
}
}
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;
}
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);
}

}```

Thank You
Ajay Dubedi

This was selected as the best answer
David Hien 8
Thanks Ajay.
Abdul Khatri
Let's Analyse the code
1. Mine has less number of lines
2. The SOQL is more efficient with no Sub Query
3. No Nested for loops and less loops
All above conditions may not be significant for the less data but for Mass they will be significant and that is what Salesforce dictates. Simple code matters but it need to be taken account the efficiency too. Also as a developer you need to learn less code, more result kind of a thing because it is easy to maintain too.
Anthony Anderson 2
Try this. I think this is good alternative:
def is_divisible(number, divisor): return modulo(number, divisor) == 0 def is_even(number): return is_divisible(number, 2) cheap essay writer (https://cheapessaywriter.co.uk/)
Hope this helps.
Emily Willson
You will need to take a close look to this college essay tips (https://writemyessay4me.org/blog/college-essay-tips). PM me if you would have any questions!
uzaiir uzaiir
The main title here says something about We have two custom Object Student and College, so if you think what this is all about then we hope https://www.dissertation-service.org/ can be what we all are looking forward to for now. This seems to be a good institute that one needs to know about for now.
John Petrella
I understand it's great when for college and school there are applications that work interdependently, in general, I'm amazed lately by the technologies that appear on the market, for example now do my second college and I feel much better than before (besides this I work), is what helps me try these guys (https://edusson.com/write-my-argumentative-essay). I am very happy that the world has evolved so much, now I can do two things at once, to do my studies and to learn, because Edusson helps me with writing my argumentative essay and with much other homework, many times I have already ordered from them the solution to my homework, and I am always satisfied. I don't know who came up with this idea, but creating a service that helps students with homework seems to be the most successful idea because there will always be students, and homework will always have to be done. I would really like to work in the future in that company, or any other company, because I find it very interesting to solve homework because you will be aware of all the problems that arise and you will know how to solve them. I think this is a very important skill.