+ Start a Discussion
Mohan Raj 33Mohan Raj 33 

Count the number of converted leads to be in the particular account by trigger

Hi, to all I am new for the trigger here I try to the trigger for  Count the number of converted leads to the particular account for the field in account that indicates the converted leads is Number_of_lead_conversion__c fields(number field) on my account standard object. Here  I don't know to how write the trigger for this case so I try followingly, it's producee the error. 
For My trying trigger is followingly,
public class LeadConversionIndicationHandler {

    Public static void insertTrigger(List<Lead> LeadConvert){
        Map<Id,Integer> NumberofconvertedLeads=new Map<Id,Integer>();
        for(Lead j:LeadConvert){
            if(NumberofconvertedLeads.get(j.AccountId)==Null){
                NumberofconvertedLeads.put(j.AccountId,1);
            }
            else{
                NumberofconvertedLeads.put(j.AccountId,NumberofconvertedLeads.get(j.AccountId)+1);
            }
            Set<id> setofid=NumberofconvertedLeads.keySet();
            List<Account> acctoupdate=[SELECT Id,Number_of_converted_Leads__c FROM Account WHERE Id IN :setofid];
            List<Account> listone=new List<Account>();
            for(Account a:acctoupdate){
                if(a.Number_of_converted_Leads__c==Null)
                    a.Number_of_converted_Leads__c=0;
                a.Number_of_converted_Leads__c=a.Number_of_converted_Leads__c+NumberofconvertedLeads.get(a.Id);
                listone.add(a);
                
            }
            Update listone;

        
        }
        

    }
    
   
}

If it very worst I apologize for that  becoz I got the error in AccountId field is invalid for lead sobject here I am try to once the lead converted it's has the accountId so that's my concept to do in this trigger so please help me to done this function.
My requirements are followingly,
1.I have a account has the number field(number of convertedleads) for counting the number of converted leads for that account(it's a number field).
2.here Once the lead is to be converted to the particular account that account having the  number of convertedleads that field is to be incremented the value by one on after the lead should be converted.
3. Once I deleted the converted lead record on the particular account the account field (number of convertedleads) has to be decreased the value by one in automatically.

this are all achieved by trigger on lead sobject.
thank you,Mohan
Best Answer chosen by Mohan Raj 33
NagendraNagendra (Salesforce Developers) 
Hi Mohan,

You should use declarative-lookup-rollup-summaries(https://github.com/afawcett/declarative-lookup-rollup-summaries) to implement this functionality, rather than rolling your own triggers. Clicks not code!

Once you install the tool, you simply have to create a Lookup Rollup Summary record with the following values and activate it.
  • Parent Object - Account
  • Relationship Field - AccountId
  • Child Object - Lead
  • Field to Aggregate - Id
  • Aggregation Operation - Count
  • Aggregate Result Field - Number_of_converted_Leads__c
It should take you under five minutes to set up the rollup, and it's already tested for you. Don't write code you don't have to write.



Kindly mark this post as solved if the information help's so that it gets removed from the unanswered queue which results in helping others who are really in need of it.

Best Regards,
Nagendra.P