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;
}
Trigger.oldmap
is not available.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.