Reputation: 1
There is College and Student object and college has two fields a) Total day Scholars Students and Total Hostler Students and Students have field type (picklist-value, Hostler and Day Scholer) and update college field with total Hostler and and total Days Scholars whenever student is created, edited and deleted.
public static void afterUpdate(List<Student__c>StuList,Map<id,Student__c>StOldMap) { List<College__c>collList=[SELECT id,Name,Total_Day_Scholar_Students__c,Total_Hostler_Students__c FROM College__c]; list<College__c>ColUpdate=new List<College__c>(); Map<id,College__c>colMap=new Map<id,College__c>(); setcollId=new set(); for(Student__c stud:StuList) { if(stud.College__c!=NULL && stud.Student_Type__c!=StOldMap.get(stud.id).Student_Type__c) { collId.add(stud.College__c);
}
}
//system.debug('line 18 the collId '+collId);
List<AggregateResult>agRes=[SELECT COUNT(id)total,Student_Type__c stype,college__c clgId FROM Student__c WHERE College__c IN:collId GROUP BY Student_Type__c,college__c];
system.debug('line 81 the aggregateRes '+agRes);
for(AggregateResult res:agRes)
{ String Cid=String.valueOf(res.get('clgId'));
System.debug('line 84 the collId contains cid'+collId.contains(Cid));
system.debug('the count of day schol or hostler stud is '+res.get('total'));
String studType=String.valueOf(res.get('stype'));
if(studType=='Day Scholar')
{
System.debug('line 88 day scholaar should be the stud type -' +studType);
Integer dayScholar=Integer.valueOf(res.get('total'));
College__c collg=new College__c();
collg.id=Cid;
collg.Total_Day_Scholar_Students__c=dayScholar;
//collg.Total_Hostler_Students__c=ColObj.Total_Hostler_Students__c-1;
System.debug('the line 96 dayScholar val is'+dayScholar);
//System.debug('the line 97 Total_Hostler_Students__c val is'+collg.Total_Hostler_Students__c);
ColUpdate.add(collg);
System.debug('line 100 ColUpdate '+ColUpdate);//Id=a00GC00004jw4aaYAA, Total_Day_Scholar_Students__c=2
}
if(studType=='Hostler'){
Integer Host=Integer.valueOf(res.get('total'));
College__c collg=new College__c();
collg.id=Cid;
System.debug('the line 101 Host val is'+Host);
collg.Total_Hostler_Students__c=Host;
//collg.Total_Day_Scholar_Students__c=ColObj.Total_Day_Scholar_Students__c-1;
// System.debug('the line 107 dayScholar val is'+collg.Total_Day_Scholar_Students__c);
System.debug('the line 108 Total_Hostler_Students__c val is'+collg.Total_Hostler_Students__c);
ColUpdate.add(collg);
System.debug('line 113 ColUpdate '+ColUpdate);//Id=a00GC00004jw4aaYAA, Total_Day_Scholar_Students__c=2},
//College__c:{Id=a00GC00004jw4aaYAA, Total_Hostler_Students__c=1}
}
}
//if(collId.contains(DaySchId))
colMap.putall(ColUpdate);
if(colMap.size()>0){
update colMap.values();
System.debug('line 120 colMap value '+colMap);
}
}
}
I want below output to be stored in map.->line 113 ColUpdate '+ColUpdate);//Id=a00GC00004jw4aaYAA, Total_Day_Scholar_Students__c=2}, //College__c:{Id=a00GC00004jw4aaYAA, Total_Hostler_Students__c=1}
Upvotes: 0
Views: 105