Skip to main content
added 19 characters in body
Source Link
Rohit Mourya
  • 3.9k
  • 2
  • 16
  • 23

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.

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) {
        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.

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.

Source Link
Rohit Mourya
  • 3.9k
  • 2
  • 16
  • 23

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) {
        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.