function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Meghana SharmaMeghana Sharma 

Need Help with Apex Class

Hello Developers

need small Assistance with the apex class

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 achive this functionality
 
public class IncrementonLead {
    public void onInsert( list<Lead> newList){

    List<Lead> lstld = [SELECT Id,India_Auto_Number__c FROM Lead];
    Integer intCounter = lstld.size() != 0 ? lstld[0].India_Auto_Number__c : 0;
    for(Lead objLead: newList)
    {
        intCounter ++;
        objLead.India_Auto_Number__c = intCounter;
    }

    }
}

This is what i have  come up with so far
Best Answer chosen by Meghana Sharma
Maulik D ShahMaulik D Shah
@Meghana
Trigger:
trigger LeadIncrementV3 on Lead (before insert,Before Update) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
        Integer leadList = [Select Count() From Lead Where Region__c = 'India'];
        Integer leadListt = [Select Count() From Lead Where Region__c = 'US'];
        Integer leadListm = [Select Count() From Lead Where Region__c = 'EU'];
        Integer leadListo = [Select Count() From Lead Where Region__c = 'SEA'];
        Integer leadListp = [Select Count() From Lead Where Region__c = 'ME'];
        For(Lead l : trigger.New) {
            if(l.Region__c == 'India') {
                if(leadList > 0){
                    l.Lead_No_Region_IN__c = leadList+0;   
                } else {
                    l.Lead_No_Region_IN__c = 1;
                }
            }
            if(l.Region__c == 'US') {
                if(leadListt > 0){
                    l.Lead_No_Region_USA__c = leadListt+0;   
                } else {
                    l.Lead_No_Region_USA__c = 1;
                }
            }
            if(l.Region__c == 'EU') {
                if(leadListm > 0){
                    l.Lead_No_Region_EU__c = leadListm+0;   
                } else {
                    l.Lead_No_Region_EU__c = 1;
                }
            }
            if(l.Region__c == 'SEA') {
                if(leadListo > 0){
                    l.Lead_No_Region_SEA__c = leadListo+0;   
                } else {
                    l.Lead_No_Region_SEA__c = 1;
                }
            }
            if(l.Region__c == 'ME') {
                if(leadListp > 0){
                    l.Lead_No_Region_ME__c = leadListp+0;   
                } else {
                    l.Lead_No_Region_ME__c = 1;
                }
            }
        }         
    }
}

Test Class:
@isTest
public class LeadIncrementV3Test {
    @isTest
    public static void LeadIncrementV3TestMethod() {
        List<Lead> leadList = new List<Lead>();

        Lead l = new Lead();
        l.Country = 'India';
        l.Company = 'Test';
        l.LastName = 'testLast';
        l.Status = 'Open - Not Contacted';
        leadList.add(l);

        Lead l1 = new Lead();
        l1.Country = 'India';
        l1.Company = 'Test1';
        l1.LastName = 'testLast1';
        l1.Status = 'Open - Not Contacted';
        leadList.add(l1);

        Lead l2 = new Lead();
        l2.Country = 'US';
        l2.Company = 'Test2';
        l2.LastName = 'testLast2';
        l2.Status = 'Open - Not Contacted';
        leadList.add(l2);

        Lead l21 = new Lead();
        l21.Country = 'USA';
        l21.Company = 'Test21';
        l21.LastName = 'testLast21';
        l21.Status = 'Open - Not Contacted';
        leadList.add(l21);

        Lead l3 = new Lead();
        l3.Country = 'Brazil';
        l3.Company = 'Test3';
        l3.LastName = 'testLast3';
        l3.Status = 'Open - Not Contacted';
        leadList.add(l3);

        Lead l31 = new Lead();
        l31.Country = 'Belgium';
        l31.Company = 'Test31';
        l31.LastName = 'testLast31';
        l31.Status = 'Open - Not Contacted';
        leadList.add(l31);

        Lead l4 = new Lead();
        l4.Country = 'Thailand';
        l4.Company = 'Test4';
        l4.LastName = 'testLast4';
        l4.Status = 'Open - Not Contacted';
        leadList.add(l4);

        Lead l41 = new Lead();
        l41.Country = 'Japan';
        l41.Company = 'Test41';
        l41.LastName = 'testLast41';
        l41.Status = 'Open - Not Contacted';
        leadList.add(l41);

        Lead l5 = new Lead();
        l5.Country = 'Iran';
        l5.Company = 'Test5';
        l5.LastName = 'testLast5';
        l5.Status = 'Open - Not Contacted';
        leadList.add(l5);

        Lead l51 = new Lead();
        l51.Country = 'Kuwait';
        l51.Company = 'Test51';
        l51.LastName = 'testLast51';
        l51.Status = 'Open - Not Contacted';
        leadList.add(l51);  
        
        Insert leadList;
    }
}

​​​​​​​

All Answers

Meghana SharmaMeghana Sharma
i have even Tried trigger - But i am getting the error as"System.NullPointerException: Attempt to de-reference a null object  "

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'){          // This is where i am getting the error (System.NullPointerException: Attempt to de-reference a null object) //
            llds.India_Auto_Number__c = LatestNumber + 1;
            //Numero_Account__c is a text number of Account
        }
    }
    
    lds.India_Auto_Number__c= LatestNumber + 1;
    update lds;        
  
}
Maulik D ShahMaulik D Shah
Hello Meghana,

Try this,
trigger LeadIncrementV2 on Lead (before insert,Before Update) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
        List<Lead> leadList = [Select Id,India_Auto_Number__c From Lead Where Country = 'India'];
        For(Lead l : trigger.New) {
            if(l.Country == 'India') {
                if(leadList.size() > 0){
                    l.India_Auto_Number__c = leadList.size()+1;   
                } else {
                    l.India_Auto_Number__c = 1;
                }
            }
        }        
    }
}

I hope this helps you.
Meghana SharmaMeghana Sharma
@Maulik

Thank you very much for the code its working fine, When i create a 6th Lead with Country as 'India' the custom number field  'India_Auto_Number__c' should be updated again as 1,   

how to achive this functionality??
Maulik D ShahMaulik D Shah
Hello Meghana,

can you create any other trigger or workflow rule which updates this custom field?

 
Meghana SharmaMeghana Sharma
@Maulik
in the Workflow rule, i am taking field update, where i need to define a formula, cant we modify in the existing trigger itself, because it may become an issue while creating a test class
Meghana SharmaMeghana Sharma
@Maulik
can you suggest any formula where if India_Auto_Number__c = 6 it should automatically update to 1, if its 7 it should update as 2....etc etc
Maulik D ShahMaulik D Shah
Hello Meghana,

When India_Auto_Number__c = 6 then which field you want to update as 1?
 
Meghana SharmaMeghana Sharma
@ Maulik
Never Mind, i figured it out, it dint want the mod from the begining, can you please help me with the test class

This  is my perfectly working Trigger code

trigger LeadIncrementV3 on Lead (before insert,Before Update) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
        List<Lead> leadList = [Select Id,Lead_No_Region_IN__c From Lead Where Region__c = 'India'];
        For(Lead l : trigger.New) {
            if(l.Region__c == 'India') {
                if(leadList.size() > 0){
                    l.Lead_No_Region_IN__c = leadList.size()+0;   
                } else {
                    l.Lead_No_Region_IN__c = 1;
                }
            }
        } 
        List<Lead> leadListt = [Select Id,Lead_No_Region_USA__c From Lead Where Region__c = 'US'];
        For(Lead m : trigger.New) {
            if(m.Region__c == 'US') {
                if(leadListt.size() > 0){
                    m.Lead_No_Region_USA__c = leadListt.size()+0;   
                } else {
                    m.Lead_No_Region_USA__c = 1;
                }
            }
        } 
        List<Lead> leadListm = [Select Id,Lead_No_Region_EU__c From Lead Where Region__c = 'EU'];
        For(Lead n : trigger.New) {
            
            if(n.Region__c == 'EU') {
                if(leadListm.size() > 0){
                    n.Lead_No_Region_EU__c = leadListm.size()+0;   
                } else {
                    n.Lead_No_Region_EU__c = 1;
                }
            }
        } 
        List<Lead> leadListo = [Select Id,Lead_No_Region_EU__c From Lead Where Region__c = 'SEA'];
        For(Lead o : trigger.New) {
            if(o.Region__c == 'SEA') {
                if(leadListo.size() > 0){
                    o.Lead_No_Region_SEA__c = leadListo.size()+0;   
                } else {
                    o.Lead_No_Region_SEA__c = 1;
                }
            }
        }
        List<Lead> leadListp = [Select Id,Lead_No_Region_ME__c From Lead Where Region__c = 'ME'];
        For(Lead p : trigger.New) {
            if(p.Region__c == 'ME') {
                if(leadListp.size() > 0){
                    p.Lead_No_Region_ME__c = leadListp.size()+0;   
                } else {
                    p.Lead_No_Region_ME__c = 1;
                }
            }
        } 
        
    }
}


Can you please help me with the Test class??
Maulik D ShahMaulik D Shah
Hello Meghana,

Trigger:
trigger LeadIncrementV3 on Lead (before insert,Before Update) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
        List<Lead> leadList = [Select Id,Lead_No_Region_IN__c From Lead Where Region__c = 'India'];
        For(Lead l : trigger.New) {
            if(l.Region__c == 'India') {
                if(leadList.size() > 0){
                    l.Lead_No_Region_IN__c = leadList.size()+0;   
                } else {
                    l.Lead_No_Region_IN__c = 1;
                }
            }
        } 
        List<Lead> leadListt = [Select Id,Lead_No_Region_USA__c From Lead Where Region__c = 'US'];
        For(Lead m : trigger.New) {
            if(m.Region__c == 'US') {
                if(leadListt.size() > 0){
                    m.Lead_No_Region_USA__c = leadListt.size()+0;   
                } else {
                    m.Lead_No_Region_USA__c = 1;
                }
            }
        } 
        List<Lead> leadListm = [Select Id,Lead_No_Region_EU__c From Lead Where Region__c = 'EU'];
        For(Lead n : trigger.New) {
            
            if(n.Region__c == 'EU') {
                if(leadListm.size() > 0){
                    n.Lead_No_Region_EU__c = leadListm.size()+0;   
                } else {
                    n.Lead_No_Region_EU__c = 1;
                }
            }
        } 
        List<Lead> leadListo = [Select Id,Lead_No_Region_SEA__c From Lead Where Region__c = 'SEA'];
        For(Lead o : trigger.New) {
            if(o.Region__c == 'SEA') {
                if(leadListo.size() > 0){
                    o.Lead_No_Region_SEA__c = leadListo.size()+0;   
                } else {
                    o.Lead_No_Region_SEA__c = 1;
                }
            }
        }
        List<Lead> leadListp = [Select Id,Lead_No_Region_ME__c From Lead Where Region__c = 'ME'];
        For(Lead p : trigger.New) {
            if(p.Region__c == 'ME') {
                if(leadListp.size() > 0){
                    p.Lead_No_Region_ME__c = leadListp.size()+0;   
                } else {
                    p.Lead_No_Region_ME__c = 1;
                }
            }
        } 
        
    }
}

Test Class:
@isTest
public class LeadIncrementV3Test {
    @isTest
    public static void LeadIncrementV3TestMethod() {
        Lead l = new Lead();
        l.Region__c = 'India';
        l.Company = 'Test';
        l.LastName = 'testLast';
        l.Status = 'Open - Not Contacted';
        Insert l;

        Lead l1 = new Lead();
        l1.Region__c = 'India';
        l1.Company = 'Test1';
        l1.LastName = 'testLast1';
        l1.Status = 'Open - Not Contacted';
        Insert l1;

        Lead l2 = new Lead();
        l2.Region__c = 'US';
        l2.Company = 'Test2';
        l2.LastName = 'testLast2';
        l2.Status = 'Open - Not Contacted';
        Insert l2;

        Lead l21 = new Lead();
        l21.Region__c = 'US';
        l21.Company = 'Test21';
        l21.LastName = 'testLast21';
        l21.Status = 'Open - Not Contacted';
        Insert l21;

        Lead l3 = new Lead();
        l3.Region__c = 'EU';
        l3.Company = 'Test3';
        l3.LastName = 'testLast3';
        l3.Status = 'Open - Not Contacted';
        Insert l3;

        Lead l31 = new Lead();
        l31.Region__c = 'EU';
        l31.Company = 'Test31';
        l31.LastName = 'testLast31';
        l31.Status = 'Open - Not Contacted';
        Insert l31;

        Lead l4 = new Lead();
        l4.Region__c = 'SEA';
        l4.Company = 'Test4';
        l4.LastName = 'testLast4';
        l4.Status = 'Open - Not Contacted';
        Insert l4;

        Lead l41 = new Lead();
        l41.Region__c = 'SEA';
        l41.Company = 'Test41';
        l41.LastName = 'testLast41';
        l41.Status = 'Open - Not Contacted';
        Insert l41;

        Lead l5 = new Lead();
        l5.Region__c = 'ME';
        l5.Company = 'Test5';
        l5.LastName = 'testLast5';
        l5.Status = 'Open - Not Contacted';
        Insert l5;

        Lead l51 = new Lead();
        l51.Region__c = 'ME';
        l51.Company = 'Test51';
        l51.LastName = 'testLast51';
        l51.Status = 'Open - Not Contacted';
        Insert l51;        
    }
}

I hope this helps you.
Meghana SharmaMeghana Sharma
@Maulik
i am getting an error , (Field is not writeable: Lead.Region__c),   Region__c is an formula field, how do i cover formula field in test class? and i am getting 51% code coverage, how do i increase the test coverage??
Meghana SharmaMeghana Sharma
@Maulik
i solved the Field is not Writeable issue, when i try to run the test, i am getting the error in the line 3 of the trigger, 
User-added image
Maulik D ShahMaulik D Shah
Hi Meghana,

can you please share your formula for Region__c?
Meghana SharmaMeghana Sharma
This is the formula for the Region__c
CASE(Country, "United States", "US",
"Canada", "US",
"USA", "US",
"US", "US",
"Brazil", "EU",
"Guyana", "SA",
"Paraguay", "SA",
"Peru", "SA",
"Uruguay", "SA",
"Venezuela", "SA",
"Germany", "EU",
"Deutschland", "EU",
"United Kingdom", "EU",
"France", "EU",
"Italy", "EU",
"Spain", "EU",
"Ukraine", "EU",
"Poland", "EU",
"Romania", "EU",
"Netherlands", "EU",
"Belgium", "EU",
"Greece", "EU",
"Czech Republic", "EU",
"Portugal", "EU",
"Sweden", "EU",
"Hungary", "EU",
"Belarus", "EU",
"Serbia", "EU",
"Austria", "EU",
"Switzerland", "EU",
"Bulgaria", "EU",
"Denmark", "EU",
"Finland", "EU",
"Slovakia", "EU",
"Norway", "EU",
"Ireland", "EU",
"Russia", "EU",
"Albania", "EU",
"Georgia", "EU",
"Latvia", "EU",
"Lithuania", "EU",
"Malta", "EU",
"Croatia", "EU",
"Polska", "EU",
"San Marino", "EU",
"Bosnia and Herzegovina", "EU",
"Sweden", "EU",
"Bahrain", "ME",
"Cyprus", "ME",
"Egypt", "ME",
"Iran", "ME",
"Iraq", "ME",
"Israel", "ME",
"Jordan", "ME",
"Kuwait", "ME",
"Lebanon", "ME",
"Oman", "ME",
"Qatar", "ME",
"Saudi Arabia", "ME",
"Turkey", "ME",
"United Arab Emirates", "ME",
"UAE", "ME",
"Yemen", "ME",
"India", "India",
"Indonesia", "SEA",
"Malaysia", "SEA",
"Philippines", "SEA",
"Singapore", "SEA",
"Thailand", "SEA",
"Viet Nam", "SEA",
"Taiwan", "SEA",
"Hong Kong", "SEA",
"Cambodia", "SEA",
"China", "SEA",
"Japan", "SEA",
"Australia", "SEA",
"ROW")
Maulik D ShahMaulik D Shah
@Meghana,

Try this,
@isTest
public class LeadIncrementV3Test {
    @isTest
    public static void LeadIncrementV3TestMethod() {
        Lead l = new Lead();
        l.Country = 'India';
        l.Company = 'Test';
        l.LastName = 'testLast';
        l.Status = 'Open - Not Contacted';
        Insert l;
        Lead l1 = new Lead();
        l1.Country = 'India';
        l1.Company = 'Test1';
        l1.LastName = 'testLast1';
        l1.Status = 'Open - Not Contacted';
        Insert l1;
        Lead l2 = new Lead();
        l2.Country = 'US';
        l2.Company = 'Test2';
        l2.LastName = 'testLast2';
        l2.Status = 'Open - Not Contacted';
        Insert l2;
        Lead l21 = new Lead();
        l21.Country = 'USA';
        l21.Company = 'Test21';
        l21.LastName = 'testLast21';
        l21.Status = 'Open - Not Contacted';
        Insert l21;
        Lead l3 = new Lead();
        l3.Country = 'Brazil';
        l3.Company = 'Test3';
        l3.LastName = 'testLast3';
        l3.Status = 'Open - Not Contacted';
        Insert l3;
        Lead l31 = new Lead();
        l31.Country = 'Belgium';
        l31.Company = 'Test31';
        l31.LastName = 'testLast31';
        l31.Status = 'Open - Not Contacted';
        Insert l31;
        Lead l4 = new Lead();
        l4.Country = 'Thailand';
        l4.Company = 'Test4';
        l4.LastName = 'testLast4';
        l4.Status = 'Open - Not Contacted';
        Insert l4;
        Lead l41 = new Lead();
        l41.Country = 'Japan';
        l41.Company = 'Test41';
        l41.LastName = 'testLast41';
        l41.Status = 'Open - Not Contacted';
        Insert l41;
        Lead l5 = new Lead();
        l5.Country = 'Iran';
        l5.Company = 'Test5';
        l5.LastName = 'testLast5';
        l5.Status = 'Open - Not Contacted';
        Insert l5;
        Lead l51 = new Lead();
        l51.Country = 'Kuwait';
        l51.Company = 'Test51';
        l51.LastName = 'testLast51';
        l51.Status = 'Open - Not Contacted';
        Insert l51;        
    }
}

 
Meghana SharmaMeghana Sharma
@Maulik
still i am getting the same  error as Too manu SOQL queries, in the line 3 of the trigger
Maulik D ShahMaulik D Shah
@Meghana
How many records of lead at the time you update or insert?
When test class run then you get this error?
Meghana SharmaMeghana Sharma
@Maulik
the error is coming from the below line
 List<Lead> leadList = [Select Id,Lead_No_Region_IN__c From Lead Where Region__c = 'India'];

when i try to queery - Select Id,Lead_No_Region_IN__c From Lead Where Region__c = 'India'
it in the Querry editor it shows as 72 Records

still i am getting this error in the test results

User-added image
Maulik D ShahMaulik D Shah
@Meghana,

Try this for trigger,
trigger LeadIncrementV3 on Lead (before insert,Before Update) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
        Integer leadList = [Select Count() From Lead Where Region__c = 'India'];
        Integer leadListt = [Select Count() From Lead Where Region__c = 'US'];
        Integer leadListm = [Select Count() From Lead Where Region__c = 'EU'];
        Integer leadListo = [Select Count() From Lead Where Region__c = 'SEA'];
        Integer leadListp = [Select Count() From Lead Where Region__c = 'ME'];
        For(Lead l : trigger.New) {
            if(l.Region__c == 'India') {
                if(leadList > 0){
                    l.Lead_No_Region_IN__c = leadList+0;   
                } else {
                    l.Lead_No_Region_IN__c = 1;
                }
            }
            if(l.Region__c == 'US') {
                if(leadListt > 0){
                    l.Lead_No_Region_USA__c = leadListt+0;   
                } else {
                    l.Lead_No_Region_USA__c = 1;
                }
            }
            if(l.Region__c == 'EU') {
                if(leadListm > 0){
                    l.Lead_No_Region_EU__c = leadListm+0;   
                } else {
                    l.Lead_No_Region_EU__c = 1;
                }
            }
            if(l.Region__c == 'SEA') {
                if(leadListo > 0){
                    l.Lead_No_Region_SEA__c = leadListo+0;   
                } else {
                    l.Lead_No_Region_SEA__c = 1;
                }
            }
            if(l.Region__c == 'ME') {
                if(leadListp > 0){
                    l.Lead_No_Region_ME__c = leadListp+0;   
                } else {
                    l.Lead_No_Region_ME__c = 1;
                }
            }
        }         
    }
}

and for test class,
 
@isTest
public class LeadIncrementV3Test {
    @isTest
    public static void LeadIncrementV3TestMethod() {
        List<Lead> leadList = new List<Lead>();

        Lead l = new Lead();
        l.Country = 'India';
        l.Company = 'Test';
        l.LastName = 'testLast';
        l.Status = 'Open - Not Contacted';
        leadList.add(l);

        Lead l1 = new Lead();
        l1.Country = 'India';
        l1.Company = 'Test1';
        l1.LastName = 'testLast1';
        l1.Status = 'Open - Not Contacted';
        leadList.add(l1);

        Lead l2 = new Lead();
        l2.Country = 'US';
        l2.Company = 'Test2';
        l2.LastName = 'testLast2';
        l2.Status = 'Open - Not Contacted';
        leadList.add(l2);

        Lead l21 = new Lead();
        l21.Country = 'USA';
        l21.Company = 'Test21';
        l21.LastName = 'testLast21';
        l21.Status = 'Open - Not Contacted';
        leadList.add(l21);

        Lead l3 = new Lead();
        l3.Country = 'Brazil';
        l3.Company = 'Test3';
        l3.LastName = 'testLast3';
        l3.Status = 'Open - Not Contacted';
        leadList.add(l3);

        Lead l31 = new Lead();
        l31.Country = 'Belgium';
        l31.Company = 'Test31';
        l31.LastName = 'testLast31';
        l31.Status = 'Open - Not Contacted';
        leadList.add(l31);

        Lead l4 = new Lead();
        l4.Country = 'Thailand';
        l4.Company = 'Test4';
        l4.LastName = 'testLast4';
        l4.Status = 'Open - Not Contacted';
        leadList.add(l4);

        Lead l41 = new Lead();
        l41.Country = 'Japan';
        l41.Company = 'Test41';
        l41.LastName = 'testLast41';
        l41.Status = 'Open - Not Contacted';
        leadList.add(l41);

        Lead l5 = new Lead();
        l5.Country = 'Iran';
        l5.Company = 'Test5';
        l5.LastName = 'testLast5';
        l5.Status = 'Open - Not Contacted';
        leadList.add(l5);

        Lead l51 = new Lead();
        l51.Country = 'Kuwait';
        l51.Company = 'Test51';
        l51.LastName = 'testLast51';
        l51.Status = 'Open - Not Contacted';
        leadList.add(l51);  
        
        Insert leadList;
    }
}

​​​​​​​
Meghana SharmaMeghana Sharma
@Maulik
It finally worked, thank you very much for taking your time for Corrections and suggestions
Maulik D ShahMaulik D Shah
@Meghana
Trigger:
trigger LeadIncrementV3 on Lead (before insert,Before Update) {
    if(trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {
        Integer leadList = [Select Count() From Lead Where Region__c = 'India'];
        Integer leadListt = [Select Count() From Lead Where Region__c = 'US'];
        Integer leadListm = [Select Count() From Lead Where Region__c = 'EU'];
        Integer leadListo = [Select Count() From Lead Where Region__c = 'SEA'];
        Integer leadListp = [Select Count() From Lead Where Region__c = 'ME'];
        For(Lead l : trigger.New) {
            if(l.Region__c == 'India') {
                if(leadList > 0){
                    l.Lead_No_Region_IN__c = leadList+0;   
                } else {
                    l.Lead_No_Region_IN__c = 1;
                }
            }
            if(l.Region__c == 'US') {
                if(leadListt > 0){
                    l.Lead_No_Region_USA__c = leadListt+0;   
                } else {
                    l.Lead_No_Region_USA__c = 1;
                }
            }
            if(l.Region__c == 'EU') {
                if(leadListm > 0){
                    l.Lead_No_Region_EU__c = leadListm+0;   
                } else {
                    l.Lead_No_Region_EU__c = 1;
                }
            }
            if(l.Region__c == 'SEA') {
                if(leadListo > 0){
                    l.Lead_No_Region_SEA__c = leadListo+0;   
                } else {
                    l.Lead_No_Region_SEA__c = 1;
                }
            }
            if(l.Region__c == 'ME') {
                if(leadListp > 0){
                    l.Lead_No_Region_ME__c = leadListp+0;   
                } else {
                    l.Lead_No_Region_ME__c = 1;
                }
            }
        }         
    }
}

Test Class:
@isTest
public class LeadIncrementV3Test {
    @isTest
    public static void LeadIncrementV3TestMethod() {
        List<Lead> leadList = new List<Lead>();

        Lead l = new Lead();
        l.Country = 'India';
        l.Company = 'Test';
        l.LastName = 'testLast';
        l.Status = 'Open - Not Contacted';
        leadList.add(l);

        Lead l1 = new Lead();
        l1.Country = 'India';
        l1.Company = 'Test1';
        l1.LastName = 'testLast1';
        l1.Status = 'Open - Not Contacted';
        leadList.add(l1);

        Lead l2 = new Lead();
        l2.Country = 'US';
        l2.Company = 'Test2';
        l2.LastName = 'testLast2';
        l2.Status = 'Open - Not Contacted';
        leadList.add(l2);

        Lead l21 = new Lead();
        l21.Country = 'USA';
        l21.Company = 'Test21';
        l21.LastName = 'testLast21';
        l21.Status = 'Open - Not Contacted';
        leadList.add(l21);

        Lead l3 = new Lead();
        l3.Country = 'Brazil';
        l3.Company = 'Test3';
        l3.LastName = 'testLast3';
        l3.Status = 'Open - Not Contacted';
        leadList.add(l3);

        Lead l31 = new Lead();
        l31.Country = 'Belgium';
        l31.Company = 'Test31';
        l31.LastName = 'testLast31';
        l31.Status = 'Open - Not Contacted';
        leadList.add(l31);

        Lead l4 = new Lead();
        l4.Country = 'Thailand';
        l4.Company = 'Test4';
        l4.LastName = 'testLast4';
        l4.Status = 'Open - Not Contacted';
        leadList.add(l4);

        Lead l41 = new Lead();
        l41.Country = 'Japan';
        l41.Company = 'Test41';
        l41.LastName = 'testLast41';
        l41.Status = 'Open - Not Contacted';
        leadList.add(l41);

        Lead l5 = new Lead();
        l5.Country = 'Iran';
        l5.Company = 'Test5';
        l5.LastName = 'testLast5';
        l5.Status = 'Open - Not Contacted';
        leadList.add(l5);

        Lead l51 = new Lead();
        l51.Country = 'Kuwait';
        l51.Company = 'Test51';
        l51.LastName = 'testLast51';
        l51.Status = 'Open - Not Contacted';
        leadList.add(l51);  
        
        Insert leadList;
    }
}

​​​​​​​
This was selected as the best answer
Maulik D ShahMaulik D Shah
@Meghana

If your issue is solved then this question make solved. so it helps others.

Thanks. 
Meghana SharmaMeghana Sharma
@Mauilk
i have marked the best answer, and once again thank you very much for the help