function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Oliver Freeman 9Oliver Freeman 9 

Count Related Records on Case

Hi There!

I have a custom object (Contract Loan) which is related to the Case object, and I'd like to be able to count how many Contract Loan records there are related to each case.
I can't use a ROLLUP Summary, because we don't have (don't want) a Master-Detail Relationship field on the Contract Loan object to the case, so it'd need to be done by an APEX Trigger - I won't lie, I have no idea what I'm doing.

Can anyone give me a hand at starting this, please?

Thanks,
Oli
Best Answer chosen by Oliver Freeman 9
rajat Maheshwari 6rajat Maheshwari 6

Hi Oliver,

Below is code snippet which will help you definately :)

 

Trigger ContractLoanTrigger on Contract_Loan__c(After insert,After Delete,After Undelete){
 
 Set<Id> setCaseIds = new Set<Id>();
 List<Case> lst_Case = new List<Case>();
  
 
  if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate))
{
   for(Contract_Loan__c con : Trigger.new)
  {
       setCaseIds.add(con.Case__c);
   }
  }
  
  if(Trigger.isDelete)
{
   for(Contract_Loan__c con : Trigger.old) 
   {
       setCaseIds.add(con.Case__c);
   }
  }

Map<Id,Double> mp_Double = new Map<Id,Double>();

  for(AggregateResult agg : [Select Case__c, count(Id) from Contract_Loan__c where Case__c IN: setCaseIds group by Case__c])

{
    mp_Double.put((Id)agg.get('Case__c'), (Double)agg.get('expr0'));
}

for(Case cs : [Select Id, number_of_Contract_Loans from Case where Id IN: setCaseIds])
    {
       if(mp_Double!=null && mp_Double.containsKey(cs.Id))
            {
                cs.number_of_Contract_Loans = mp_Double.get(cs.Id);
                lst_Case.add(cs);
            }
     }

if(lst_Case!=null && lst_Case.size()>0)
  update lst_Case;
}
 

Please Mark as best answer, If it works otherwise let me know the issue :)

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

 

 

All Answers

Akhil ReddyAkhil Reddy
Trigger ContractLoanTrigger on Contract_Loan__c(After insert,After Delete,After Undelete){
  Set<Id> setCaseIds = new Set<Id>();
  
  //Whenever your working with After Undelete operation you can access data through 
  //Trigger.new or Trigger.newMap but not with Trigger.old or Trigger.oldmap variables
  if(Trigger.isInsert || Trigger.isUndelete){
   for(Contract_Loan__c con : Trigger.new){
    setCaseIds.add(con.AccountId);
   }
  }
  
  if(Trigger.isDelete){
   //if you use Trigger.new below in place of Trigger.old you will end up with 
   //System.NullPointerException:Attempt to de-reference a null object
   for(Contact con : Trigger.old) {
       setCaseIds.add(con.AccountId);
   }
  }
  
 List<Case> listcss = [Select id,name,number_of_Contract_Loans ,(Select id from Contract_Loan__c) from Case where                Id in : setCaseIds];
  for(Case cs :listcss ){
   cs.number_of_Contract_Loans= cs.Contract_Loan__c.size();
  }
  update listcss;
}

 
rajat Maheshwari 6rajat Maheshwari 6

Hi Oliver,

Below is code snippet which will help you definately :)

 

Trigger ContractLoanTrigger on Contract_Loan__c(After insert,After Delete,After Undelete){
 
 Set<Id> setCaseIds = new Set<Id>();
 List<Case> lst_Case = new List<Case>();
  
 
  if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate))
{
   for(Contract_Loan__c con : Trigger.new)
  {
       setCaseIds.add(con.Case__c);
   }
  }
  
  if(Trigger.isDelete)
{
   for(Contract_Loan__c con : Trigger.old) 
   {
       setCaseIds.add(con.Case__c);
   }
  }

Map<Id,Double> mp_Double = new Map<Id,Double>();

  for(AggregateResult agg : [Select Case__c, count(Id) from Contract_Loan__c where Case__c IN: setCaseIds group by Case__c])

{
    mp_Double.put((Id)agg.get('Case__c'), (Double)agg.get('expr0'));
}

for(Case cs : [Select Id, number_of_Contract_Loans from Case where Id IN: setCaseIds])
    {
       if(mp_Double!=null && mp_Double.containsKey(cs.Id))
            {
                cs.number_of_Contract_Loans = mp_Double.get(cs.Id);
                lst_Case.add(cs);
            }
     }

if(lst_Case!=null && lst_Case.size()>0)
  update lst_Case;
}
 

Please Mark as best answer, If it works otherwise let me know the issue :)

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

 

 

This was selected as the best answer
rajat Maheshwari 6rajat Maheshwari 6

Here in code, 

1. Contract_Loan__c is child object

2. Case is parent object

3. Case__c is lookupField on Contract_Loan__c

4. number_of_Contract_Loans is number field in Case which will hold the count of related record

Thanks

rajat Maheshwari 6rajat Maheshwari 6

Hi Oliver,

Does my code helps to fix your issue? please let me know

Thanks

Oliver Freeman 9Oliver Freeman 9

Hey,

Thanks for your help on this, I've only just got around to implementing this.
Is there any way that I can make this trigger depend on the Status__c field value on the contract loan object? For example, if status set to closed or rejected, remove from count.

Thanks,

Oli