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
SRossSRoss 

Ending position out of bounds: 3

Hi-
I am trying to grab the area code out of the phone number field on the lead object, and then use this as a lookup to a custom object from which I can pull territory assignments.
I am getting this err-> UpdateLeadACA: execution of BeforeUpdate caused by: System.StringException: Ending position out of bounds: 3

 - which i believe means that a three digit return (aka the area code) is NOT three digits.
I tried to add a length formula into the code - but that did not seem to remove the error.

I'm not sure what I am missing, any advice would be very welcome.
Thanks, S.


Here is my class:
public with sharing class UpdateLeadACAHandler {

// Set aca on Lead create

     public void setAreaCodeInsert(List<Lead> leads) {
        Area_Code_Assignment__c defaultAreaCode = null;
        Map<String, Area_Code_Assignment__c> areaCodeMap = new Map<String, Area_Code_Assignment__c>();
     
            for(Area_Code_Assignment__c areaCodeA : [SELECT Id, Name FROM Area_Code_Assignment__c LIMIT 500]) {
                areaCodeMap.put(areaCodeA.Name, areaCodeA);
            }
        
        if(System.Label.Default_Area_Code != null && areaCodeMap.containsKey(System.Label.Default_Area_Code)) {
            defaultAreaCode = areaCodeMap.get(System.Label.Default_Area_Code);
        }
        for(Lead lead : leads) {
                     
            if(lead.Phone != null && lead.Assignment_Area_Code1__c == null) {
                String phone = lead.Phone.replace('(', '').replace(')', '');
                String areaCodeA = phone.substring(0,3);              
                
                if((areaCodeA.Length()==3) && (areaCodeMap.containsKey(areaCodeA))) {
                    lead.Assignment_Area_Code1__c = areaCodeMap.get(areaCodeA).Id;
                }
            
            if((areaCodeA.Length()!=3) || (defaultAreaCode != null && lead.Assignment_Area_Code1__c == null)){
                lead.Assignment_Area_Code1__c = defaultAreaCode.Id;
            }}
        }
    }
    
// Set aca on Lead edit

public void setAreaCodeUpdate(List<Lead> newLeads, Map<Id, Lead> oldLeads) {
        Area_Code_Assignment__c defaultAreaCode = null;
        Map<String, Area_Code_Assignment__c> areaCodeMap = new Map<String, Area_Code_Assignment__c>();
      
            for(Area_Code_Assignment__c areaCodeA : [SELECT Id, Name FROM Area_Code_Assignment__c LIMIT 500]) {
                areaCodeMap.put(areaCodeA.Name, areaCodeA);
            }
      
        if(System.Label.Default_Area_Code != null && areaCodeMap.containsKey(System.Label.Default_Area_Code)) {
            defaultAreaCode = areaCodeMap.get(System.Label.Default_Area_Code);
        }
        for(Lead lead : newLeads) {
            if(lead.Phone != null && lead.Phone != oldLeads.get(lead.Id).Phone ) {
                String phone = lead.Phone.replace('(', '').replace(')', '');
                String areaCodeA = phone.substring(0,3);
                
                if((areaCodeA.Length()==3) && (areaCodeMap.containsKey(areaCodeA))) {
                    lead.Assignment_Area_Code1__c = areaCodeMap.get(areaCodeA).Id;
                }
            
            if((areaCodeA.Length()!=3) || (lead.Assignment_Area_Code1__c == null && defaultAreaCode != null)) {
                lead.Assignment_Area_Code1__c = defaultAreaCode.Id;
            }}
        }
    }
}
and here is my trigger:
 
trigger UpdateLeadACA on Lead (before insert, before update) {
    UpdateLeadACAHandler handler = new UpdateLeadACAHandler();
    if(Trigger.isBefore) {
        if(Trigger.isInsert) {
            handler.setAreaCodeInsert(Trigger.new);
           
        }
        if(Trigger.isUpdate) {
            handler.setAreaCodeUpdate(Trigger.new, Trigger.oldMap);
           
        }
    }

}


 
Best Answer chosen by SRoss
Rohit K SethiRohit K Sethi
Hi ,

When you extract String from subString method if passed indexs not found then it will raise an error. 
So you need to first check condition that the are code atleast 3 character. Then call substring method. 
    

String phone = lead.Phone.replace('(', '').replace(')', '');
if(areaCodeA.Length()>=3){
        String areaCodeA = phone.substring(0,3);              
        if(areaCodeMap.containsKey(areaCodeA)) {
            lead.Assignment_Area_Code1__c = areaCodeMap.get(areaCodeA).Id;
        }
    }
Actually after replace the phone number is not containing 3 character that is why it raise an error.
e.g. String Phone = '(12)';
Here the phone variable contains only two character after replace and then If call the substring function with 3 number index then it will through the error.

If this post solves your problem kindly mark it as solution.
Thanks.
       

 

All Answers

Rohit K SethiRohit K Sethi
Hi ,

When you extract String from subString method if passed indexs not found then it will raise an error. 
So you need to first check condition that the are code atleast 3 character. Then call substring method. 
    

String phone = lead.Phone.replace('(', '').replace(')', '');
if(areaCodeA.Length()>=3){
        String areaCodeA = phone.substring(0,3);              
        if(areaCodeMap.containsKey(areaCodeA)) {
            lead.Assignment_Area_Code1__c = areaCodeMap.get(areaCodeA).Id;
        }
    }
Actually after replace the phone number is not containing 3 character that is why it raise an error.
e.g. String Phone = '(12)';
Here the phone variable contains only two character after replace and then If call the substring function with 3 number index then it will through the error.

If this post solves your problem kindly mark it as solution.
Thanks.
       

 
This was selected as the best answer
SRossSRoss
Hi - 
Thanks for helping ..
This errs and says the variable (areacodeA) does not exist because we are using this before we declare the string variable i think..

 
SRossSRoss
its this variable - right..
if(phone.Length()>=3){