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
ItsJustCode2ItsJustCode2 

How do I bulkify this??? Kudos offered to best advice

I seem to have problems around line 43 where I have my

zipMapState.get(l.postalcode.substring(0,5));

Statement.  I was wondering if there is a more efficient way of doing this??  Please help if you can I'd really appriciated it.

Thank you, Steve
trigger ZipCodeStateCityFix on Lead (before insert, before update) { 
    
    set<string> left5zips = new set<string>();
    
    String ZipVar;
     
    for (lead l: trigger.New) {
        
        ZipVar = l.postalcode;
        
        if(l.PostalCode != null)
        {
          if(l.postalcode.length()>= 5)
          {
               
            if ((l.State == null || l.City == null) && l.postalcode != null) {
                left5zips.add(l.postalcode.substring(0,5));
            }
          }
        }  
    }

    //query the zip_code object to get the zipcode (Name) and zone from the zip code object
    if(ZipVar != null)
    {
    if(ZipVar.length() >= 5)
    {
     
    map<string,string> zipMapState=new map<string,string>(); 
    map<string,string> zipMapCity=new map<string,string>();
    
    
    for(Zip_Code_Look_up__c z : [Select name, State__c, City__c 
    from Zip_Code_Look_up__c WHERE name IN :left5zips]) 
    {
            zipMapState.put (z.name, z.State__c); 
            zipMapCity.put (z.name, z.City__c);
    }

    for (lead l: trigger.new) {
        if((l.State == null || l.City == null) && l.postalcode != null) {
              
              String State = zipMapState.get(l.postalcode.substring(0,5));
              String City = zipMapCity.get(l.postalcode.substring(0,5));
              
              if (State != null) 
              {
                  l.State = State;
                  l.City = City;
                  l.Country = 'USA';
              }

        }
        if (l.State == null )
        {
            Return;        
        }
    }
    }}
    Return;
}

 
Best Answer chosen by ItsJustCode2
Vishal_GuptaVishal_Gupta
Hi Steve,

Change you line 43 and 44 with below ones :

String State = '';
String City = '';
if(l.postalcode.llength()= 5)
{
State = zipMapState.get(l.postalcode.substring(0,4));
City = zipMapCity.get(l.postalcode.substring(0,4));
}
else if(l.postalcode.llength()> 5)
{
State = zipMapState.get(l.postalcode.substring(0,5));
City = zipMapCity.get(l.postalcode.substring(0,5));
}

Hopefully It will work for you. Let me know if more information will require.

Thanks,
Vishal
 

All Answers

ItsJustCode2ItsJustCode2
This error went away when I reduced the update batch size to 1 record at a time.  This was the error message after it processed a few thousand records the last 200 had this erro:   Developer script exception from CMN.com : ZipCodeStateCityFix : ZipCodeStateCityFix: execution of BeforeUpdate caused by: System.StringException: Ending position out of bounds: 5 Trigger.ZipCodeStateCityFix: line 43, column 1
Vishal_GuptaVishal_Gupta
Hi Steve,

Change you line 43 and 44 with below ones :

String State = '';
String City = '';
if(l.postalcode.llength()= 5)
{
State = zipMapState.get(l.postalcode.substring(0,4));
City = zipMapCity.get(l.postalcode.substring(0,4));
}
else if(l.postalcode.llength()> 5)
{
State = zipMapState.get(l.postalcode.substring(0,5));
City = zipMapCity.get(l.postalcode.substring(0,5));
}

Hopefully It will work for you. Let me know if more information will require.

Thanks,
Vishal
 
This was selected as the best answer
Vishal_GuptaVishal_Gupta
Make one change in bold line:

String State = '';
String City = '';
if(l.postalcode.length()== 5)
{
State = zipMapState.get(l.postalcode.substring(0,4));
City = zipMapCity.get(l.postalcode.substring(0,4));
}
else if(l.postalcode.length()> 5)
{
State = zipMapState.get(l.postalcode.substring(0,5));
City = zipMapCity.get(l.postalcode.substring(0,5));
}

Thanks,
Vishal