You need to sign in to do that
Don't have an account?
SambitNayak
trigger to count the number of related records
Hi,
I have a requirement to count the number of candidates looking upto a Test Center object. The field "No of Candidates" on the Test Center object should calculate the number of candidates associated.
I wrote an After trigger but it's not updating the field:
trigger candidateTrigger on Candidate__c (after insert, after update, after delete) {
candidateTriggerHandler.calcNoOfCandidates(Trigger.isDelete ? Trigger.old : Trigger.new);
}
//Handler class
public class candidateTriggerHandler {
public static void calcNoOfCandidates(List<Candidate__c> candList){
Set<ID> TestCenters= New Set<ID>();
for(Candidate__c can:candList){
if(can.Test_Center__r.ID != NULL){
TestCenters.add(can.Test_Center__r.ID);
}
}
Map<Id,List<Candidate__c>> mapTestCntrToCand = New Map<Id,List<Candidate__c>>();
for(Test_Center__c tc:[SELECT ID, No_of_Candidates__c , (SELECT Id FROM Candidates__r)
FROM Test_Center__c WHERE Id IN:TestCenters]){
mapTestCntrToCand.put(tc.Id, tc.Candidates__r);
}
for(Candidate__c can:candList){
if(mapTestCntrToCand.containsKey(can.Test_Center__r.ID)){
Integer noOfCands=0;
for(Candidate__c c:mapTestCntrToCand.get(can.Test_Center__r.ID)){
noOfCands++;
system.debug('no of cndts inside for loop: '+ noOfCands);
}
can.Test_Center__r.No_of_Candidates__c = noOfCands;
system.debug('no of cndts outside for loop: '+ noOfCands);
}
}
}
I have a requirement to count the number of candidates looking upto a Test Center object. The field "No of Candidates" on the Test Center object should calculate the number of candidates associated.
I wrote an After trigger but it's not updating the field:
trigger candidateTrigger on Candidate__c (after insert, after update, after delete) {
candidateTriggerHandler.calcNoOfCandidates(Trigger.isDelete ? Trigger.old : Trigger.new);
}
//Handler class
public class candidateTriggerHandler {
public static void calcNoOfCandidates(List<Candidate__c> candList){
Set<ID> TestCenters= New Set<ID>();
for(Candidate__c can:candList){
if(can.Test_Center__r.ID != NULL){
TestCenters.add(can.Test_Center__r.ID);
}
}
Map<Id,List<Candidate__c>> mapTestCntrToCand = New Map<Id,List<Candidate__c>>();
for(Test_Center__c tc:[SELECT ID, No_of_Candidates__c , (SELECT Id FROM Candidates__r)
FROM Test_Center__c WHERE Id IN:TestCenters]){
mapTestCntrToCand.put(tc.Id, tc.Candidates__r);
}
for(Candidate__c can:candList){
if(mapTestCntrToCand.containsKey(can.Test_Center__r.ID)){
Integer noOfCands=0;
for(Candidate__c c:mapTestCntrToCand.get(can.Test_Center__r.ID)){
noOfCands++;
system.debug('no of cndts inside for loop: '+ noOfCands);
}
can.Test_Center__r.No_of_Candidates__c = noOfCands;
system.debug('no of cndts outside for loop: '+ noOfCands);
}
}
}
Take Refference from Below Code
Please Mark It As Best Answer If It Helps
Thank You!
All Answers
What does this debug line give
system.debug('no of cndts inside for loop: '+ noOfCands);
Thanks
Take Refference from Below Code
Please Mark It As Best Answer If It Helps
Thank You!
If this information helps, please mark the answer as best. Thank you
Hi CharuDutt: That was really helpful. I tried doing the same. Unfortunately, it's still not updating:
public static void calcNoOfCandidates(List<Candidate__c> candList){
Set<ID> TestCenters= New Set<ID>();
for(Candidate__c can:candList){
//if(can.Test_Center__r.ID != NULL){
TestCenters.add(can.Test_Center__r.ID);
//}
}
List<Test_Center__c> testCntrsToUpdt1 = New List<Test_Center__c>();
List<Test_Center__c> testCntrsToUpdt = [SELECT ID, No_of_Candidates__c , (SELECT Id FROM Candidates__r)
FROM Test_Center__c WHERE Id IN:TestCenters];
for(Test_Center__c tc:testCntrsToUpdt){
tc.No_of_Candidates__c = tc.Candidates__r.size();
testCntrsToUpdt1.add(tc);
}
update testCntrsToUpdt1;
You are Doing Different Read And Compare My Code it succesfully running in my org
Hi CharuDutt....
Same code yet no luck:
public static void calcNoOfCandidates(List<Candidate__c> candList, map<Id,Candidate__c>oldmap){
Set<ID> TestCenters= New Set<ID>();
for(Candidate__c can:candList){
if(can.Test_Center__c != null && can.Test_Center__c != oldMap.get(can.Id).Test_Center__c){
//if(can.Test_Center__r.ID != NULL){
TestCenters.add(can.Test_Center__r.ID);
TestCenters.add(oldMap.get(can.id).Test_Center__r.ID);
}
//}
}
List<Test_Center__c> testCntrsToUpdt1 = New List<Test_Center__c>();
List<Test_Center__c> testCntrsToUpdt = [SELECT ID, No_of_Candidates__c , (SELECT Id FROM Candidates__r)
FROM Test_Center__c WHERE Id IN:TestCenters];
for(Test_Center__c tc:testCntrsToUpdt){
tc.No_of_Candidates__c = tc.Candidates__r.size();
testCntrsToUpdt1.add(tc);
}
update testCntrsToUpdt1;
Your Are Using 'Test_Center__r.ID' in For Loop If Condition While I'm Using This 'Test_Center__c ' This Is The Deiffrence See It Now
Thanks CharuDutt...
It really works now!!