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
Elsa CordonnierElsa Cordonnier 

Trigger count custom object on Leads

Hello everyone, 

I'm looking to create a trigger on a Lead level to count how many custom object are associated to this Lead. The lead and the custom object(Box) are associate through a lookup filed in the Box object.
I create a custom filed call Count__c to collect the result. 

I get inspiration from a trigger which count the number of tasks assiciate to a Lead but this doesn't work. 
Can you provide me advise ? 

Thank you 
Best
Elsa


 
Best Answer chosen by Elsa Cordonnier
vishnu Rvishnu R
hi elsa,

I tried the code its working for me...check this out..

Trigger:::::

trigger CountNatterboxOnLead on shobithapp__NatterboxCallReporting__c (after update,after insert) {
list<id> leadids=new list<id>();
list<Lead> leadnames=new list<Lead>();
if(Trigger.isUpdate|| Trigger.isInsert) 
{
for(shobithapp__NatterboxCallReporting__c n:trigger.new)
   {
leadids.add(n.shobithapp__lead_name__c );
   }
}
AggregateResult[] groupedResults = [SELECT COUNT(Id),shobithapp__lead_name__c FROM shobithapp__NatterboxCallReporting__c where shobithapp__lead_name__c  IN :leadids Group By shobithapp__lead_name__c];
for(AggregateResult ar:groupedResults) {    
     Id accidd=(ID)ar.get('shobithapp__lead_name__c');       
     Integer count = (INTEGER)ar.get('expr0');    
     lead l= new lead(Id=accidd);   
     l.shobithapp__Total_No_of_boxes__c= count;    
     leadnames.add(l);   
   }
   update leadnames;
}


mark this as best answer if  your issue is  resolved

thanks
 Vishnu R

All Answers

Harish RamachandruniHarish Ramachandruni
Hi ,

Write trigger on task  or custom object of  chaild to lead .

You have to write a trigger on child not on parent  .  


 
trigger UpdateOrder on Child__c (after insert, after update, after delete, after undelete) {

   List<Parent__c> ct = new List<Parent__c>();
   
   Set<Id> custord = new Set<Id>();
   
   if(Trigger.isDelete) {
     for(Child__c test:Trigger.Old) {
      
        custord.add(test.Parent__c);   
    
     }   
   
   }
   else
   if(Trigger.isUpdate) {

     for(Child__c test:Trigger.New) {
      
        custord.add(test.Parent__c);   
    
     }

     for(Child__c test:Trigger.Old) {
      
        custord.add(test.Parent__c);   
    
     }   
   
   }
   else
   {
     for(Child__c test:Trigger.New) {
      
        custord.add(test.Parent__c);   
    
     }
   }
   
   AggregateResult[] groupedResults = [SELECT COUNT(Id), Parent__c FROM Child__c where Parent__c IN :custord GROUP      BY Parent__c ];
   
   for(AggregateResult ar:groupedResults) {
     
     Id custid = (ID)ar.get('Parent__c');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Parent__c cust1 = new Parent__c(Id=custid);
     
     cust1.child_count__c = count;
     
     ct.add(cust1);
      
   }
   
   
   update ct;

}


If any query ask me .

Regards,
Harish.R.
 
Elsa CordonnierElsa Cordonnier
Hello Harish, 

I tested and it doesnt seems to work
Error: Compile Error: sObject type 'Leads' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 40 column 39


trigger UpdateOrder on Natterbox_Call_Reporting_Object__c (after insert, after update, after delete, after undelete) {

   List<Lead> ct = new List<Lead>();
   
   Set<Id> custord = new Set<Id>();
   
   if(Trigger.isDelete) {
     for(Natterbox_Call_Reporting_Object__c test:Trigger.Old) {
      
        custord.add(test.Lead);   
    
     }   
   
   }
   else
   if(Trigger.isUpdate) {

     for(Natterbox_Call_Reporting_Object__c test:Trigger.New) {
      
        custord.add(test.Lead);   
    
     }

     for(Natterbox_Call_Reporting_Object__c test:Trigger.Old) {
      
        custord.add(test.Lead);   
    
     }   
   
   }
   else
   {
     for(Natterbox_Call_Reporting_Object__c test:Trigger.New) {
      
        custord.add(test.Lead);   
    
     }
   }
   
   AggregateResult[] groupedResults = [SELECT COUNT(Id), Lead FROM Lead where Natterbox_Call_Reporting_Object__c IN :custord GROUP      BY Lead ];
   
   for(AggregateResult ar:groupedResults) {
     
     Id custid = (ID)ar.get('Lead');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Lead cust1 = new Lead(Id=custid);
     
     cust1.count__c = count;
     
     ct.add(cust1);
      
   }
   
   
   update ct;

}
vishnu Rvishnu R
Hi elsa,

i just tried with Accounts and Contacts.it is working for me..so try with this code.....


Trigger::


trigger ContactsCount on Contact (after update,after insert) {
list<id> accids=new list<id>();
list<Account> accnames=new list<Account>();
if(Trigger.isUpdate) 
{
for(Contact c:trigger.new)
   {
accids.add(c.AccountID);
   }
}
AggregateResult[] groupedResults = [SELECT COUNT(Id),AccountId   FROM contact where AccountId IN :accids GROUP   BY AccountId ];
for(AggregateResult ar:groupedResults) {    
     Id accidd=(ID)ar.get('AccountId');    
     Integer count = (INTEGER)ar.get('expr0');    
     account a = new account(Id=accidd);   
     a.shobithapp__Total_No_of_boxes__c= count;    
     accnames.add(a);   
   }
   update accnames;
}

Mark solved if it resolved your issue.

thanks

Vishnu R
Elsa CordonnierElsa Cordonnier
trigger CountNatterboxOnLead on Natterbox_Call_Reporting_Object__c (after update,after insert) {

list<id> leadsids=new list<id>();
list<Lead> leadsnames=new list<Lead>();
if(Trigger.isUpdate) 
{
for(Natterbox_Call_Reporting_Object__c c:trigger.new)
   {
leadsids.add(c.LeadID);
   }
}
AggregateResult[] groupedResults = [SELECT COUNT(Id),LeadsId   FROM Natterbox_Call_Reporting_Object__c where LeadsId IN :leadsids GROUP   BY LeadsId ];
for(AggregateResult ar:groupedResults) {    
     Id accidd=(ID)ar.get('LeadId');    
     Integer count = (INTEGER)ar.get('expr0');    
     account a = new lead(Id=accidd);   
     a.shobithapp__TNatterbox_activity_count__c= count;    
     accnames.add(a);   
   }
   update accnames;
}

It's still dont work, the errors tell me that the LeadID doesnt exist
vishnu Rvishnu R
Hi elsa,

Account and contact are having a relation so I have used the comman thing i.e AccountId.for your case check whether
  1.  the list of IDs  is  not empty (if it is empty change the line 8 as     leadsids.add(c.Lead);
  2. if the list is not empty then in the aggregate query you should see the below mentioned points carefully and modify the query using lead not leadsId because you should use a field which is common in both the objects.if you use a relational field you won't get the  error "LeadsId does not exist".
  3. use lead instead of account in line 15 .
  4. COUNT() must be the only element in the SELECT list.
  5. You can use COUNT() with a LIMIT clause.
  6. You can't use COUNT() with an ORDER BY clause. Use COUNT(fieldName) instead.You can't use COUNT() with a GROUP BY clause for API version 19.0 and later. Use COUNT(fieldName) instead.
hope this helps you.....

thanks

vishnu R
vishnu Rvishnu R
hi elsa,

I tried the code its working for me...check this out..

Trigger:::::

trigger CountNatterboxOnLead on shobithapp__NatterboxCallReporting__c (after update,after insert) {
list<id> leadids=new list<id>();
list<Lead> leadnames=new list<Lead>();
if(Trigger.isUpdate|| Trigger.isInsert) 
{
for(shobithapp__NatterboxCallReporting__c n:trigger.new)
   {
leadids.add(n.shobithapp__lead_name__c );
   }
}
AggregateResult[] groupedResults = [SELECT COUNT(Id),shobithapp__lead_name__c FROM shobithapp__NatterboxCallReporting__c where shobithapp__lead_name__c  IN :leadids Group By shobithapp__lead_name__c];
for(AggregateResult ar:groupedResults) {    
     Id accidd=(ID)ar.get('shobithapp__lead_name__c');       
     Integer count = (INTEGER)ar.get('expr0');    
     lead l= new lead(Id=accidd);   
     l.shobithapp__Total_No_of_boxes__c= count;    
     leadnames.add(l);   
   }
   update leadnames;
}


mark this as best answer if  your issue is  resolved

thanks
 Vishnu R
This was selected as the best answer
vishnu Rvishnu R
hi elsa,
  • shobithapp__Total_No_of_boxes__c ->this is the API name of the field   to show the count in Lead Object.
  • shobithapp__lead_name__c  -> this is the API name of the  look up field  in  nutterbox object(shobithapp__NatterboxCallReporting__c
Elsa CordonnierElsa Cordonnier
Thank you Vishnu !! It's perfectly working !!