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
sarahdevsarahdev 

Parsing Street Addresses?

I need to know what syntax to use to parse the street number out of the standard street address field in a trigger.  I only need up to the space in my custom field.

 

Any help would be greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
a.Billing_Street_Name__c = a.BillingStreet.split(' ',2)[1]; // Everything after the numbers.

All Answers

sfdcfoxsfdcfox

For non PO box addresses (you'd have to check that separately):

 

String HouseNumber = record.BillingStreet.split(' ',2)[0]; // Split by "space", take the first result.

 

sarahdevsarahdev

Thanks!, would this be right:

Trigger AddressMapNonStandardFields on Account(before insert,before update){
For(Account a:Trigger.new){
    if (a.AccountSource == 'Data.com' && Trigger.isBefore && Trigger.isInsert)
        // Will only run this is a data.com record and if it is a NEW record
   a.Billing_Street_Number__c= a.BillingStreet.split(' ',2)[0]; // Split by "space", take the first result.;
   a.Billing_Street_Name__c=a.BillingStreet;
   a.Billing_City__c=a.BillingCity;
   a.Billing_State_ABB__c=a.BillingState;
   a.Billing_Zip_Code__c=a.BillingPostalCode;
}
}

sfdcfoxsfdcfox
a.Billing_Street_Name__c = a.BillingStreet.split(' ',2)[1]; // Everything after the numbers.
This was selected as the best answer
sarahdevsarahdev

When I put this in the developer console, the line is highlighted in pink but with no problems.  When I try to manally insert an Account, it does not parse the number out of the street address, it doesn't copy anything into the Billing_Street_number__C field.

Any suggestions?

sarahdevsarahdev

I finally got this working, thanks so much for all your help.  It was a error on my end not the person that posted the solution.  Here it is.

trigger
AddressMapNonStandardFields on Account(before insert) {

For(Account a: Trigger.new) {

if (a.AccountSource == 'Data.com' && Trigger.isInsert)

// Will only run this is a data.com
record and if it is a NEW record
//Map the standard salesforce billing address fields to my custom fields
{ a.Billing_Street_Number__c = a.BillingStreet.split(' ',2)[0];

a.Billing_Street_Name__c = a.BillingStreet.split(' ',2)[1];

a.Billing_City__c = a.BillingCity;

a.Billing_State_ABB__c = a.BillingState;

a.Billing_Zip_Code__c = a.BillingPostalCode;

}

}

}

gslagle1.3916088682906562E12gslagle1.3916088682906562E12
I needed a solution to pull out the street names only (removing the street number), and I know exactly zero apex code. 

Warning: it's pretty ugly, but this actually worked for me:


IF(
   NOT(ISNUMBER(MID(BillingStreet,1,1))),
   BillingStreet,
   IF(
      NOT(ISNUMBER(MID(BillingStreet,2,1))),
      RIGHT(BillingStreet, LEN(BillingStreet)-1),
      IF(
         NOT(ISNUMBER(MID(BillingStreet,3,1))),
         RIGHT(BillingStreet, LEN(BillingStreet)-2),
         IF(
            NOT(ISNUMBER(MID(BillingStreet,4,1))),
            RIGHT(BillingStreet, LEN(BillingStreet)-3),
            IF(
               NOT(ISNUMBER(MID(BillingStreet,5,1))),
               RIGHT(BillingStreet, LEN(BillingStreet)-4),
               IF(
                  NOT(ISNUMBER(MID(BillingStreet,5,1))),
                  RIGHT(BillingStreet, LEN(BillingStreet)-4),
                  BillingStreet
                 )
               )
            )
         )
      )
   )
Brian Hayes 12Brian Hayes 12
I just stumbled across this. This is not the general (ideal) way to parse a U.S. postal street address. See Publication 28, Postal Addressing Standards, https://pe.usps.com/text/pub28/welcome.htm, for the different delivery address line formats.  This posting is informative: How to parse freeform street/postal address out of text, and into components - Stack Overflow (https://stackoverflow.com/questions/11160192/how-to-parse-freeform-street-postal-address-out-of-text-and-into-components)