You need to sign in to do that
Don't have an account?
RossG
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: LeadTrigger: execution of BeforeUpdate caused by: System.StringException: Ending position out of bounds: 5 Trigger.LeadTrigger: line 58, column 1\n
I'm getting this error on my lead trigger when a lead is updated.
The offending line is in the code below where it says:
String zipStringx = l.PostalCode.substring(0,5);
Does anyone know how I could tweak this code to prevent that error? Thanks. Here's the full code for hte lead trigger where this occurs:
The offending line is in the code below where it says:
String zipStringx = l.PostalCode.substring(0,5);
Does anyone know how I could tweak this code to prevent that error? Thanks. Here's the full code for hte lead trigger where this occurs:
/*Begin Processing United States leads*/ if(Trigger.isInsert || Trigger.isUpdate){ if(HelperClass.firstRun2){ HelperClass.firstRun2=false; for(Lead l1: trigger.new){ if((UserInfo.getUserId() == Label.MarketoIntegrationUser || UserInfo.getUserId() == Label.WebsiteIntegrationUser) && (l1.RecordTypeId == leadRecordTypeId) && (l1.Country == 'United States' || l1.Country == 'United States of America' || l1.Country == 'USA' || l1.Country == 'U.S.' || l1.Country == 'U.S.A' ) ){ Set<String> postalCodes = new Set<String>(); for( Lead l : trigger.new ) { if(l.PostalCode != null){ String zipStringx = l.PostalCode.substring(0,5); postalCodes.add( zipStringx ); // postalCodes.add( l.PostalCode ); } else{} } Map<String, Zip_State_Table__c > validZips = new Map<String, Zip_State_Table__c >(); for( Zip_State_Table__c obj : [SELECT Id, Name, ZipCode__c, Territory__c FROM Zip_State_Table__c WHERE ZipCode__c IN :postalCodes] ) { validZips.put( obj.ZipCode__c, obj ); } for( Lead l : trigger.new ) { if( l.PostalCode != null ) { if( validZips.containsKey(l.PostalCode.substring(0,5)) ) { l.State = validZips.get(l.PostalCode.substring(0,5)).Name; l.RegionTerritoryHidden__c = validZips.get(l.PostalCode.substring(0,5)).Territory__c; } else {} } } } } } }
The PostalCode string is less than 5 digits.Try to debug code at below line: Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
So the code as it is is this:
for( Lead l : trigger.new ) {
if(l.PostalCode != null){
String zipStringx = l.PostalCode.substring(0,5);
postalCodes.add( zipStringx );
// postalCodes.add( l.PostalCode );
} else{}
}
Are you suggesting change that to this:
for( Lead l : trigger.new ) {
if(l.PostalCode != null && l.PostalCode.length()>=4){
String zipStringx = l.PostalCode.substring(0,5);
postalCodes.add( zipStringx );
// postalCodes.add( l.PostalCode );
} else{}
}
Why would I use 4 and not length() >= 5?
Hi RossG,
Please try below code :-
/*Begin Processing United States leads*/
if(Trigger.isInsert || Trigger.isUpdate)
{
if(HelperClass.firstRun2){
HelperClass.firstRun2=false;
for(Lead l1: trigger.new)
{
if((UserInfo.getUserId() == Label.MarketoIntegrationUser || UserInfo.getUserId() == Label.WebsiteIntegrationUser) &&
(l1.RecordTypeId == leadRecordTypeId) && (l1.Country == 'United States' ||
l1.Country == 'United States of America' ||
l1.Country == 'USA' ||
l1.Country == 'U.S.' ||
l1.Country == 'U.S.A'
) )
{
Set<String> postalCodes = new Set<String>();
for( Lead l : trigger.new )
{
if(l.PostalCode != null && l.PostalCode.length() >=5 )
{
String zipStringx = l.PostalCode.substring(0,5);
postalCodes.add( zipStringx );
// postalCodes.add( l.PostalCode );
} else{}
}
Map<String, Zip_State_Table__c > validZips = new Map<String, Zip_State_Table__c >();
for( Zip_State_Table__c obj : [SELECT Id, Name,ZipCode__c, Territory__c FROM Zip_State_Table__c WHERE ZipCode__c IN :postalCodes] )
{
validZips.put( obj.ZipCode__c, obj );
}
for( Lead l : trigger.new ) {
if( l.PostalCode != null ) {
if( validZips.containsKey(l.PostalCode.substring(0,5)) )
{
l.State = validZips.get(l.PostalCode.substring(0,5)).Name;
l.RegionTerritoryHidden__c = validZips.get(l.PostalCode.substring(0,5)).Territory__c;
} else {}
}
}
}
}
}
}
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Thanks
Amit Chaudhary