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
Saurabh ThoratSaurabh Thorat 

execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0

I'am trying to create an apex trigger for conditional autonumber on lead based on lead record type. Getting an error ''execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0''. Can someone help pelase? Thanks.

trigger Autonumber on Lead (before insert, before update) {

    list<Lead> lead= [SELECT Id,Name,Company,Status,Lead_Number__c FROM Lead WHERE Lead_Number__c !=:null AND Lead_Number__c > 0 order by Lead_Number__c desc limit 1];
    decimal maxlead=lead[0].Lead_Number__c;    

    for(Lead Lead:Trigger.new){
        if(lead.RecordTypeId =='0126F000001Cmgn'){
            lead.Lead_Number__c = Integer.valueOf(maxlead)+1;
        }
    }  
}
Best Answer chosen by Saurabh Thorat
Raj VakatiRaj Vakati
Try this,.. make the following changes .. 

Get record type id dynamically rather than hardcode the value 

 
trigger Autonumber on Lead (before insert, before update) {

    list<Lead> lead= [SELECT Id,Name,Company,Status,Lead_Number__c FROM Lead WHERE Lead_Number__c !=:null AND Lead_Number__c > 0 order by Lead_Number__c desc limit 1];
	 decimal maxlead=0 ; 
	if(lead.size()>0 && lead[0].Lead_Number__c!=null ){
    decimal maxlead=lead[0].Lead_Number__c;    
	}else{
		maxlead = 0 ;
	}

	String recId =Schema.Sobjecttype.Lead.getRecordTypeInfosByName().get('YOUR RECORD TYPE').getRecordTypeId();


	
    for(Lead Lead:Trigger.new){
        if(lead.RecordTypeId ==recId){
            lead.Lead_Number__c = Integer.valueOf(maxlead)+1;
        }
    }  
}

 

All Answers

*hunter*hunter
There would be no record in your list . Also its recommended to use null check before accessing list element.
Saurabh ThoratSaurabh Thorat
Hi Hunter,

Thanks for the response. I dont understand what you mean by that. Can you elaborate?
*hunter*hunter
The list would be empty. So the below will throw exception.
lead[0].Lead_Number__c;
Before the assignment, it is recommended that you check the size of the list.
if(lead.size()>0){
//then do the assignment.
}

 
devedeve
Hi Saurabh,

This query is not returning any record
 list<Lead> lead= [SELECT Id,Name,Company,Status,Lead_Number__c FROM Lead WHERE Lead_Number__c !=:null AND Lead_Number__c > 0 order by Lead_Number__c desc limit 1];

So when you are trying to access its 1st value by lead[0]:
decimal maxlead=lead[0].Lead_Number__c;  
its giving error 
  
just check its size before assigning its value:

trigger Autonumber on Lead (before insert, before update) {

    list<Lead> lead= [SELECT Id,Name,Company,Status,Lead_Number__c FROM Lead WHERE Lead_Number__c !=:null AND Lead_Number__c > 0 order by Lead_Number__c desc limit 1];
if(lead.size()>0) {
    decimal maxlead=lead[0].Lead_Number__c;    
}

    for(Lead Lead:Trigger.new){
        if(lead.RecordTypeId =='0126F000001Cmgn'){
            lead.Lead_Number__c = Integer.valueOf(maxlead)+1;
        }
    }  
}
 
Raj VakatiRaj Vakati
Try this,.. make the following changes .. 

Get record type id dynamically rather than hardcode the value 

 
trigger Autonumber on Lead (before insert, before update) {

    list<Lead> lead= [SELECT Id,Name,Company,Status,Lead_Number__c FROM Lead WHERE Lead_Number__c !=:null AND Lead_Number__c > 0 order by Lead_Number__c desc limit 1];
	 decimal maxlead=0 ; 
	if(lead.size()>0 && lead[0].Lead_Number__c!=null ){
    decimal maxlead=lead[0].Lead_Number__c;    
	}else{
		maxlead = 0 ;
	}

	String recId =Schema.Sobjecttype.Lead.getRecordTypeInfosByName().get('YOUR RECORD TYPE').getRecordTypeId();


	
    for(Lead Lead:Trigger.new){
        if(lead.RecordTypeId ==recId){
            lead.Lead_Number__c = Integer.valueOf(maxlead)+1;
        }
    }  
}

 
This was selected as the best answer