• SRoss
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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);
           
        }
    }

}


 
  • May 13, 2016
  • Like
  • 0
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);
           
        }
    }

}


 
  • May 13, 2016
  • Like
  • 0