There are couple of issues with your code:
- Trigger.oldmap is not available at the time of insert.
- 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.