0

My requirement is that when i create a lead with country as 'india', there is a custom field named 'India_Auto_Number__c' where it should update as 1, and again when i create a lead with country name 'india' it should increment to 2...like this it should keep on incrementing till 5th lead, when i try to insert 6th lead, it should again update as 1 how do i achieve this functionality i went through below links but still i could not achieve the functionality any help would be highly appriciated

How to generate auto number using triggers

i have tried the trigger

    trigger LeadIncrementV2 on Lead (before insert,Before Update) {


    Lead lds = new Lead();

    Lead ld = [SELECT India_Auto_Number__c  from Lead LIMIT 1];

    integer LatestNumber = Integer.valueOf(lds.India_Auto_Number__c);

    for(Lead llds : Trigger.New){
        if(Trigger.OldMap.get(llds.id).Country == 'India'){ // i am getting an error as " Null Pointer Exception"

            llds.India_Auto_Number__c = LatestNumber + 1;
            // India_Auto_Number__c is an custom number field
        }
    }

    lds.India_Auto_Number__c= LatestNumber + 1;
    update lds;        

}
  • you're getting null pointer because in case of insert, Trigger.oldmap is not available. – Rohit Mourya Jan 16 at 12:36
  • @Rohith how do i correct it?? – Manjunath S.C Jan 16 at 12:44
  • if(llds.Country == 'India') this should the issue. Also I see there are lot of issues with your code. The SOQL might not give the first record which has country India. You need to add country check in the SOQL. – Rohit Mourya Jan 16 at 12:47
  • I tried this code but the auto number is taking in this sequence 1,3,5,7....etc.....trigger LeadIncrementV3 on Lead (before insert, before update) { for(Lead l : Trigger.new){ if (l.Country =='India'){ l.India_Auto_Number__c = l.India_Auto_Number__c != null ? l.India_Auto_Number__c +1: 0; } } } – Manjunath S.C Jan 16 at 12:51
  • trigger LeadIncrementV3 on Lead (before insert, before update) { for(Lead l : Trigger.new){ if (l.Country =='India'){ l.India_Auto_Number__c = l.India_Auto_Number__c != null ? l.India_Auto_Number__c +1: 0; } } } – Manjunath S.C Jan 16 at 12:52
0

There are couple of issues with your code:

  1. Trigger.oldmap is not available at the time of insert.
  2. SOQL query should fetch the latest India_Auto_Number__c from the database to decide what number should be assigned to new lead.

Based on above points, below are the changes I did in your code:

trigger LeadIncrementV2 on Lead (before insert, Before Update) {
    if (Trigger.isbefore) {
      integer LatestNumber = 1;
      List<Lead> lds = [SELECT India_Auto_Number__c  from Lead where Country = 'India' Order by India_Auto_Number__c desc LIMIT 1];
      if (lds != null && lds.size () > 0) {
        LatestNumber = Integer.valueOf (lds[0].India_Auto_Number__c);
        LatestNumber++;
      }

      for (Lead llds : Trigger.New) {
          if (llds.Country == 'India') { 
              if (LatestNumber == 5)
                LatestNumber = 1;
              llds.India_Auto_Number__c = LatestNumber;
              LatestNumber ++;
          }
      }
    }
}

Please note that above logic is not tested however it should give you fair amount of idea.

| improve this answer | |
  • what about concurrency? – Pranay Jaiswal Jan 16 at 14:15
  • @PranayJaiswal Sorry I didn't get you. What exactly are you trying to say? – Rohit Mourya Jan 16 at 14:16
  • when 2 or more leads created at the same time, in different transactions by different users. – Pranay Jaiswal Jan 16 at 14:17
  • @PranayJaiswal good point however I'm not really sure how exactly we can avoid that. Any insights would be appreciated. – Rohit Mourya Jan 16 at 14:22
  • 1
    @Rohit i modified the code and its working fine, but when i am writing the test class for that trigger, i am getting error know as "too many soql queries 101" in the line 3 of the trigger – Manjunath S.C Jan 17 at 9:53

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.