0

I try to a trigger that counts the number of converted leads on the particular account on using number field named Number_of_converted_Leads__c in an account. So I wrote the following in trigger:

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;


        }


    }


}

Here I failed for that get the required result and this is my knowledge in trigger so any one can you help me to solve this problem to get the result in my requirement to followingly, My requirements are following:

  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.**

  • Install dlrs and learn to use it. – Adrian Larson Nov 2 '16 at 13:59
  • @AdrianLarson I don't understand what is dlrs? sir – MohanRaj Nov 2 '16 at 14:03
  • C'mon, man. Surely you have some basic search skills... – Adrian Larson Nov 2 '16 at 14:05
  • @AdrianLarson yeah I questioned to you for you said install dlrs sir that's why now I found from google but I don't know that is you mentioned you say the dlrs salesforce? – MohanRaj Nov 2 '16 at 14:08
  • The first result should be a rollup tool by Andy Fawcett. Should be pretty obvious if you read his docs a bit. I'd normally put it in answer format but I'm on my phone. – Adrian Larson Nov 2 '16 at 14:13
2

You should use 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.

|improve this answer|||||
  • Don't write code you don't have to. Considering you can't get what you have working, you should find lower hanging fruit. – Adrian Larson Nov 2 '16 at 15:00
  • yes you are correct Larson But I also want some code knowledge from like the triggers, controllers it's help a lot for my knowledge! – MohanRaj Nov 2 '16 at 15:03
  • If you want help, use Trailhead. This forum is not for tutorials, whereas that is an environment specifically structured for learning the basics. – Adrian Larson Nov 2 '16 at 15:06
  • yeah so that's why I asked even in here any one trying this problem on my way (trigger)? – MohanRaj Nov 2 '16 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.