Two object student and school :
1) When student get insert count the number of student for the school also if you update school on student then change the count on both school:
Apex trigger
trigger StudentCountforschool on Student__c (after insert,after update,after delete) {
if(Trigger.isinsert){
Countstudentofschool.countstudent(Trigger.new);
System.debug('Trigger is after insert');
}
else if(Trigger.isupdate){
Countstudentofschool.countstudentupdate(Trigger.new,Trigger.oldMap);
}
else if(Trigger.isdelete){
Countstudentofschool.countstudentdelete(Trigger.old);
}
if(Trigger.isinsert){
Countstudentofschool.countstudent(Trigger.new);
System.debug('Trigger is after insert');
}
else if(Trigger.isupdate){
Countstudentofschool.countstudentupdate(Trigger.new,Trigger.oldMap);
}
else if(Trigger.isdelete){
Countstudentofschool.countstudentdelete(Trigger.old);
}
Appex class:
public class Countstudentofschool {
//Below code has use aggregateresult to count the student for school on insert
public static void countstudent(Student__c[] student){
Set<Id> setOfSchoolId=new Set<Id>();
for(Student__c id:student){
setOfSchoolId.add(id.school__c);
}
//AggregateResult to get list of student with the count of student related to school
List<AggregateResult> AgrList = [Select school__c,count(id) co from Student__c where school__c in:setOfSchoolId GROUP BY school__c];
List<School__c> sch=new List<School__c>();
for(AggregateResult a:AgrList){
School__c sc=new School__c();
sc.id=(Id)a.get('school__c'); //GET the id of school and change the data type to id
sc.Count__c=(Integer)a.get('co');//get the count of student and change the data type to Integer
sch.add(sc);// add the school into the list
}
if(sch.size()>0){
update sch; //Check if the size is grater than zero
}
}
//after update below code will update the count on school
public static void countstudentupdate(Student__c[] student,Map<Id,Student__c> oldMap){
Set<Id> setOfSchoolId=new Set<Id>();
for(Student__c i:student){
if(i.School__c!=oldMap.get(i.Id).School__c){
setOfSchoolId.add(i.school__c);
setOfSchoolId.add(oldMap.get(i.Id).School__c);
}
}
//AggregateResult to get list of student with the count of student related to school
List<AggregateResult> AgrList = [Select school__c,count(id) co from Student__c where school__c in:setOfSchoolId GROUP BY school__c];
List<School__c> sch=new List<School__c>();
for(AggregateResult a:AgrList){
School__c sc=new School__c();
sc.id=(Id)a.get('school__c'); //GET the id of school and change the data type to id
sc.Count__c=(Integer)a.get('co');//get the count of student and change the data type to Integer
sch.add(sc);// add the school into the list
}
if(sch.size()>0){
update sch; //Check if the size is grater than zero
}
}
//after delete below code will update the count on school
public static void countstudentdelete(Student__c[] student){
Set<Id> setofschoolid=new Set<Id>();
for(Student__c s:student){
setofschoolid.add(s.School__c);
}
List<AggregateResult> listAggre=[Select school__c,count(Id) co from Student__c where school__c in:setofschoolid group by school__c];
List<School__c> listsch=new List<School__c>();
for(AggregateResult a:listAggre){
School__c sch=new School__c();
sch.id=(Id)a.get('school__c');
sch.count__c=(Integer)a.get('co');
listsch.add(sch);
}
if(listsch.size()>0){
update listsch;
}
}
/* Below code is using inner query for the counting the student for school
public static void countstudent(Student__c[] student){
Set<id> setofschoolid=new set<id>();
for(Student__c id:student){
setofschoolid.add(id.school__c);
}
List<school__c> lists=[Select id,(select id from Students__r) from school__c where Id In:setofschoolid];
System.debug('C value: '+lists);
List<School__c> sch=new List<School__c>();
for(school__c i:lists){
School__c s=new School__c();
s.Id=i.Id;
s.Count__c=i.Students__r.size();
sch.add(s);
}
if(sch.size()>0)
update sch;
}*/
}