+ Start a Discussion
Elsa CordonnierElsa Cordonnier 

Leads and Call trigger

Hello, 
I try to write a trigger which count the number of Custom Object (Naterbox Call) linked to a Lead. 

I dont have any error in the code, but I don't have results neither. 
Can someone help me ?

Best
Elsa

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

Set<ID> LeadIds = new Set<ID>();

//We only care about tasks linked to Leads.

String leadPrefix = Lead.SObjectType.getDescribe().getKeyPrefix();

//Add any Lead ids coming from the new data

if(trigger.new!=null){
    for (Natterbox Call Reporting Object, t : Trigger.new) {
     if (t.WhoId!= null && string.valueof(t.WhoId).startsWith(leadPrefix) ) {

if(!LeadIds.contains(t.WhoId)){
//adding unique lead ids since there can be many tasks with single lead
LeadIds.add(t.WhoId);
}
}
      }
}
 
//Also add any Lead ids coming from the old data (deletes, moving an activity from one Lead to another)

if(trigger.old!=null){
    for (Natterbox Call Reporting Object t2 : Trigger.old) {
     if (t2.WhoId!= null && string.valueof(t2.WhoId).startsWith(leadPrefix) )
         {
if(!LeadIds.contains(t2.WhoId)){
//adding unique lead ids since there can be many tasks with single lead
LeadIds.add(t2.WhoId);
}
}
      }
}

     if (LeadIds.size() > 0){



List<Lead> leadsWithNatterbox Call Reporting Objects = [select id,Natterbox_activity_Count__c,(select id from Natterbox Call Reporting Objects) from Lead where Id IN : Leadids];

List<Lead> leadsUpdatable = new List<Lead>();

for(Lead L : leadsWithNatterbox Call Reporting Objects){

L.Activity_Count__c = L.Natterbox Call Reporting Objects.size();
leadsUpdatable.add(L);

}

if(leadsUpdatable.size()>0){

update leadsUpdatable;
//update all the leads with activity count

}

    }
}
Elsa CordonnierElsa Cordonnier
trigger NatterboxUpdateLead on Natterbox_Call_Reporting_Object__c (after delete, after insert, after undelete, after update) {

Set<ID> LeadIds = new Set<ID>();

//We only care about tasks linked to Leads.

String leadPrefix = Lead.SObjectType.getDescribe().getKeyPrefix();

//Add any Lead ids coming from the new data

if(trigger.new!=null){
    for (Natterbox Call Reporting Object, t : Trigger.new) {
     if (t.WhoId!= null && string.valueof(t.WhoId).startsWith(leadPrefix) ) {

if(!LeadIds.contains(t.WhoId)){
//adding unique lead ids since there can be many tasks with single lead
LeadIds.add(t.WhoId);
}
}
      }
}
 
//Also add any Lead ids coming from the old data (deletes, moving an activity from one Lead to another)

if(trigger.old!=null){
    for (Natterbox Call Reporting Object t2 : Trigger.old) {
     if (t2.WhoId!= null && string.valueof(t2.WhoId).startsWith(leadPrefix) )
         {
if(!LeadIds.contains(t2.WhoId)){
//adding unique lead ids since there can be many tasks with single lead
LeadIds.add(t2.WhoId);
}
}
      }
}

     if (LeadIds.size() > 0){



List<Lead> leadsWithNatterbox Call Reporting Objects = [select id,Natterbox_activity_Count__c,(select id from Natterbox Call Reporting Objects) from Lead where Id IN : Leadids];

List<Lead> leadsUpdatable = new List<Lead>();

for(Lead L : leadsWithNatterbox Call Reporting Objects){

L.Activity_Count__c = L.Natterbox Call Reporting Objects.size();
leadsUpdatable.add(L);

}

if(leadsUpdatable.size()>0){

update leadsUpdatable;
//update all the leads with activity count

}

    }
}
Balaji BondarBalaji Bondar
Hi Elsa,

You can use SOQL Aggregate Queries in your Account trigger.
for (AggregateResult ar : [
     Select Count(Id) numRecs, Account__c acctId From NaterboxCall__c Where Account__c In :Trigger.New
     Group By Account__c]) {
    Id acctId = (Id) ar.get('acctId');
    Account acct = Trigger.newMap.get(acctId);
    acct.NaterboxCallCount__c = (Decimal) ar.get('numRecs');
}
Note:NaterboxCallCount__c is the custom field on Account.

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.