Chapter 3: Configuring Custom Objects - ToAFinish/addressverification GitHub Wiki

Configuring Custom Objects

Expanding Address Verification to Custom Areas

I

n this section you will learn to configure custom objects built within Salesforce CRM or any edition on the Salesforce platform. As with the steps for Standard Objects, we need to switch to the “Setup” area.

Creating Mandatory Fields

The following fields must be created for each object where you want to verify an address. If there is more than one address to verify, then one of these fields must be created for each address.

Sample Field Name Field Type Field Description
Street Text(255) Will typically already be created
City Text(128) Will typically already be created
State Text(20) Will typically already be created
Zip Code Text(20) Will typically already be created
Country Text(64) Will typically already be created
Address Verified Checkbox Used to determine if the address needs to be verified
Address Last Verified Date/Time Used to determine the last time the address was verified
Address Return Code Text(40) Used for a return value from the address verification. Return values are: Confirmed, Not Confirmed, Confirmed - By Dropping Secondary Info, Confirmed - Missing Secondary Info, or Not Submitted

Creating Optional Fields

Several other fields are not required, but recommended to be created for each address to be verified.

Sample Field Name Field Type Field Description
Street 2 Text(128) Sometimes used to include the second line of the address. If this is not present, then the second line of the address can be included in the “Street” field.
Address Status Formula (Text) Use the following formula (substituting XXX with the API name of the "address return code" field name): If ( XXX = "Confirmed", IMAGE("/img/msg_icons/confirm16.png", "Confirmed" ,16,16) & " Confirmed", If ( XXX = "Confirmed - By Dropping Secondary Info", IMAGE("/img/msg_icons/warning16.png", "Confirmed - By Dropping Secondary Info" ,16,16) & " Confirmed - By Dropping Secondary Info", If ( XXX = "Confirmed - Missing Secondary Info", IMAGE("/img/msg_icons/warning16.png", "Confirmed - Missing Secondary Info" ,16,16) & " Confirmed - Missing SecondaryInfo",If ( XXX = "Not Submitted", IMAGE("/img/msg_icons/error16.png", "Not Submitted" ,16,16) & " Not Submitted",If ( XXX = "Not Confirmed", IMAGE("/img/msg_icons/error16.png", "Not Confirmed" ,16,16) & " Not Confirmed", IMAGE("/img/msg_icons/info16.png", "Note Yet Verified" ,16,16) & " Not Yet Verified" ) ) ) ) )
Address Barcode Text(32) Shows the barcode returned on verification
Address Barcode Image Formula (Text) Use the following formula (substituting XXX with the API name of the "address barcode" field name): if ( not ( isblank ( XXX ) ), IMAGE("https://www.barcodesinc.com/generator/image.php?code=" + XXX + "&style=196&type=C128B&width=206&height=50&xres=1&font=3", "Address Barcode" ), "" )
Address County Text(64) Shows the county name returned on verification
Address County FIPs Code Text(32) Shows the FIPs code for the county returned on verification
Address Latitude Number (13, 5) Used for geocoding the Latitude of the address
Address Longitude Number (13, 5) Used for geocoding the longitude of the address.
Address Unit Text(64) Shows the Apartment, Suite or Unit included in the address on verification
Urbanization Text(64) Shows the urbanization information (for Puerto Rico) returned on address verification
Active? Checkbox Shows if the Post Office thinks this is an active address
Carrier Route Text(4) Post Office carrier route code
Congressional District Text(2) Code for the Congressional District in which the address is located.
Daylight Savings? Checkbox Shows if the address is in an area that uses daylight savings time.
Sample Field Name Field Type Field Description
Do Not Verify Checkbox Use this field to indicate that you do not want the address to be verified in the future. Used mostly for automated processes.
Footnotes Long Text Area Notes on the details of the verification
RDI Text(32) Shows the Residential Delivery Indicator from the Post Office.
Record Type Text(1) Shows the returned accuracy of the verification.
Record Type Description Formula (Text) If(Record_Type__c = "F", "Firm", If(Record_Type__c = "G", "General Delivery",If(Record_Type__c = "H", "High-Rise", If(Record_Type__c = "P", "P.O. Box", If(Record_Type__c = "R", "RuralRoute", If(Record_Type__c = "S", "Street", "" ) ) ) ) ) )
Time Zone Text(48) Shows the name of the time zone in which the address is located.
Vacant? Checkbox Shows if the Post Office believes this address to be vacant.
Clean Street Text(255) Used to copy a “clean” version of the street field. This is used in the feature that tries to auto-update previously verified addresses.
Verification Used Picklist Choices are: SmartyStreets, Google Maps, UPS
UTC Offset Number (16,2) Used to indicate how many hours different from GMT/UTC time
Current Time Formula (Text) For the code for this field, please see code on the next page. Note that you will need to set up the UTC Offset and the Daylight Savings? fields as described above.

Non-SmartyStreets Data

These fields are populated by sources outside of SmartyStreets and are not guaranteed to be 100% accurate. If these fields are critical for you, please contact us at [email protected] to discuss sources and how often they are updated.

Sample Field Name Field Type Field Description
Island Text (200) This is for the name of the Island on which an address is located. Only the Hawaiian and both sets of Spanish islands are supported.
Area Codes Text(32) Phone area codes supported in this zip code
Urban Area Code Text (10) Code for the primary Urban Area for the zip code, sourced from the 2010 Census.
Urban Area Name Text (255) Primary Urban Area Name/Description for the Urban Area Code for the zip code. Must be used together with the Urban Area Code, as it is populated from the code.

Current Time Formula

To use this formula, please replace “HOUROFFSET” with the API name of the field containing the numeric offset from UTC official time, and “DAYLIGHTSAVINGS” with the checkbox field containing a TRUE or FALSE value for daylight savings. This formula will only work for time in the US.

IF(ISBLANK(HOUROFFSET), "",
  IF (AND( DAYLIGHTSAVINGS, 
        TODAY() >= 
        CASE(MOD(DATE(YEAR(TODAY()),3,8) - DATE(1900,1,7),7), 
        0, DATE(YEAR(TODAY()),3,8), 
        1, DATE(YEAR(TODAY()),3,8)+6, 
        2, DATE(YEAR(TODAY()),3,8)+5, 
        3, DATE(YEAR(TODAY()),3,8)+4, 
        4, DATE(YEAR(TODAY()),3,8)+3, 
        5, DATE(YEAR(TODAY()),3,8)+2, 
        DATE(YEAR(TODAY()),3,8)+1), 
        TODAY() <
        CASE(MOD(DATE(YEAR(TODAY()),11,1) - DATE(1900,1,7),7), 
        0, DATE(YEAR(TODAY()),11,1), 
        1, DATE(YEAR(TODAY()),11,1)+6, 
        2, DATE(YEAR(TODAY()),11,1)+5, 
        3, DATE(YEAR(TODAY()),11,1)+4, 
        4, DATE(YEAR(TODAY()),11,1)+3, 
        5, DATE(YEAR(TODAY()),11,1)+2, 
        DATE(YEAR(TODAY()),11,1)+1)),
    /* CALCULATE FOR IT BEING CURRENTLY DST */
    MID( TEXT( NOW()+ (HOUROFFSET +1) /24 ), 6, 2 ) & "/" &
    MID( TEXT( NOW()+ (HOUROFFSET +1) /24 ), 9, 2 ) & "/" &
    LEFT( TEXT( NOW()+ (HOUROFFSET +1) /24 ), 4 ) & " " &
    TEXT(
      IF(
        OR(
          VALUE( MID( TEXT( NOW()+ (HOUROFFSET +1) /24 ), 12, 2 ) ) = 0,
          VALUE( MID( TEXT( NOW()+ (HOUROFFSET +1) /24 ), 12, 2 ) ) = 12), 12,
            VALUE( MID( TEXT( NOW()+ (HOUROFFSET +1) /24 ), 12, 2 ) ) -
            IF( VALUE( MID( TEXT( NOW()+ (HOUROFFSET +1) /24 ), 12, 2 ) ) < 12, 0, 12 )
      )
    )
    &":"& MID( TEXT( NOW()+ (HOUROFFSET +1) /24 ), 15, 2 ) &" "&
    IF( VALUE( MID( TEXT( NOW()+ (HOUROFFSET +1) /24 ), 12, 2 ) ) < 12, "AM", "PM"
    ),
    /* CALCULATE FOR IT NOT BEING CURRENTLY DST */
    MID( TEXT( NOW()+ HOUROFFSET /24 ), 6, 2 ) & "/" &
    MID( TEXT( NOW()+ HOUROFFSET /24 ), 9, 2 ) & "/" &
    LEFT( TEXT( NOW()+ HOUROFFSET /24 ), 4 ) & " " &
    TEXT(
      IF(
        OR(
          VALUE( MID( TEXT( NOW()+ HOUROFFSET /24 ), 12, 2 ) ) = 0,
          VALUE( MID( TEXT( NOW()+ HOUROFFSET /24 ), 12, 2 ) ) = 12), 12,
            VALUE( MID( TEXT( NOW()+ HOUROFFSET /24 ), 12, 2 ) ) -
            IF( VALUE( MID( TEXT( NOW()+ HOUROFFSET /24 ), 12, 2 ) ) < 12, 0, 12 )
      )
    )
    &":"& MID( TEXT( NOW()+ HOUROFFSET /24 ), 15, 2 ) &" "&
    IF( VALUE( MID( TEXT( NOW()+ HOUROFFSET /24 ), 12, 2 ) ) < 12, "AM", "PM"
    )
  )
)

Creating International Fields

If you are going to be verifying international addresses, there are a few fields you should create if you want to have helpful data.

Sample Field Name Field Type Field Description
Address Precision Text (255) How precise is the address? From best to worst: ,DeliveryPoint — rooftop level,Premise ,property or building level,Thoroughfare — street level,Locality — city or town level,AdministrativeArea — state or province level,None — unknown address
Administrative Area Text (255) The most common administrative division within a country (e.g. Region)
Geocode Precision Text (255) How precise is the geocoding of the address? From best to worst: ,DeliveryPoint — rooftop level,Premise — property or building level,Thoroughfare — street level,Locality — city or town level
AdministrativeArea — state or province level,None — unknown address
Organization Text (255) The name of the recipient, firm, or company at this address
Premise Text (64) Alphanumeric code pertaining to an individual location
Premise Extra Text (64) Extra information about the premise that is not necessarily authoritative but might still be useful
Sub Building Number Text (64) The alphanumeric component of the sub_building
Sub Building Type Text (64) The leading sub-building type of the sub_building
Super Administrative Area Text (255) The largest administrative division within a country
Thoroughfare Name Text (255) The name component of the thoroughfare

Creating Custom Address Records

Every custom address to be verified must first be set up in the configuration page. Once in the setup page, click the “Manage Addresses” tab and then click the “New Address” button.

image

Enter a name for the new address, which begins with the object name. Next, select the object, make the address Active, and then fill in at least the Required Fields and press the “Save” button. This will add the current address as a valid address to verify in your org.

image

  • Note: If you want to add an address from an object that does not show up in the Object dropdown, you will need to first add this object using the Add Supported Object button on the prior page.

Lightning Setup

Creating the Custom “Verify” Button

Please note that the following section will be very technical and perhaps difficult to follow if you do not yet have experience in Salesforce development. We provide code samples, but they must be tweaked and modified to fit your own custom object.

GitHub.com Address Verification Sample Code Repository
https://github.com/ToAFinish/addressverification

All of the JavaScript code referenced in the pages below can be found in the "lightning" folder in our GitHub repository:

  1. Go to Setup -> Custom Code -> Apex Classes and press the "New" button.
    • Note: this must be done in a sandbox or dev org
  1. In a new tab, go to our GitHub repository, in the Lightning folder, and open the "custom_apex_controller" entry. Paste the code (not the line numbers) into the box in Salesforce. You will need to replace all of the references to the custom Office__c object with the object and field names of your custom object. Once this is done, Save. If it saves, then you are most likely safe to continue.

image

  1. Now open the Developer Console

  2. Click File -> New -> Lightning Component

  3. Give the component a name and optional description. Click Submit.

  4. Replace the default text in the new component with the code in the "custom_lightning_component" entry in our GitHub repository.

  5. Now click on the Controller item to create a new controller for this bundle.

  6. Replace the default text in the new controller with the code in the "custom_lightning_controller" entry in the Lightning folder in our GitHub repository. Again, updating the areas referencing the sample Office__c custom object with your own custom object and fields.

    • Note: To ensure that you have located all areas in the code that need attention, do a search for "__c", which indicates a custom object or field. Replace the name of the field in the code with the name in your custom object.

image

  1. Go back to Setup -> Object Manager -> Creates -> Custom Object .

  2. Scroll down to the "Buttons, Links, and Actions" section and click on the "New Action" button.

  3. Select "Lightning Component" as the Action Type, and then select the component that you just created. Give it a name like "Verify Address" and save.

  4. Now edit the Page Layout for your custom object. If you have multiple layouts, you will need to do this several times. Go to the " Salesforce Mobile and Lightning Experience Actions " menu at the top and find the Action you just created. Drag it down into the "Salesforce 1 and Lightning Actions" section.

    • Note: if the "Salesforce Mobile and Lightning Experience Actions" section is closed, you might need to open it for updates.

image

  1. Now switch to Lightning and you can test the button. You can also test it on Salesforce Mobile.

Classic Setup

Creating the Custom “Verify” Button

A custom button must be created on the object to launch the address verification, and then added to the layout.

image

First, create a simple Visualforce page by going to Setup  App Setup  Develop  Visualforce pages, and press the “New” button. Then, paste the code below into the page area:

<apex:page standardController="Object_Name__c" sidebar="false">
    <smartystreets:VerifyAddresses />
</apex:page>

Replace the highlighted “Object_Name__c” with the API name of the custom or standard object.

Next, go to the object you want to verify, and find the “Buttons, Links and Actions” section and then create a new button. Set it as a “Detail Page Button” and have the behavior “Display in Existing Window without sidebar or header” and under the Content Source, select “Visualforce Page” and then select the page you created in the steps above.

Now, you will need to add this button to the layout, and you are good to go!

A word about JavaScript buttons. We still provide the code for JavaScript buttons in our repository. These can be used in Classic Salesforce, but are no longer recommended by Salesforce. If you are currently using Classic, it is OK to stay with them as they provide a somewhat faster experience, but our recommendation is to move to either a Lightning Component or a Visualforce page as described above.

Setup for All Scenarios

Un-verifying Records On Change

Now that we have a button to verify the record, we need to automate one very important feature: we need to make sure that addresses are only verified when we need them to. We have two options: Trigger (must be coded in a Sandbox first) or Workflow rule.

Option 1: Flagging Addresses Un-Verified via Workflow Rule

An easy way to mark a record as needing verification is via a workflow rule. This option might be preferable for the learned point-and-click administrator, or someone who is good at formulas and simply prefers configuration via clicks vs. code.

  • *Note: You could also use the newer Process Builder for this task, but we are not currently recommending it as the errors when these processes don’t work are harder to manage. However, it appears that Processes have more of a future than Workflow Rules, so if you are already using them, please add this to your feature list.
  1. To build it with Workflow Rules, **go to Setup -> Process Automation -> Workflow **

image

Rules and click on the “New Rule” button to get started.

  1. On the next screen select the Object where your custom address is, and then press the Next button.

  2. Name the rule and change the Evaluation Criteria to “created, and every time it’s edited.” Then change the** Rule Criteria** to **“formula evaluates to true” **and now you need to create a formula using the isChanged() function to determine if any of the address fields were changed. See the example below.

image

  1. After making sure that your formula is correct, press the Save & Next button. Then, from the Add Workflow Options dropdown, select Field Update.

image

  1. Enter a Name and Unique Name for the field update, and then select the Field to Update. This must be the checkbox field indicating if an address is verified, that is configured in the Setup page for this address. Next, select “False” in the Checkbox Options section, and Save.

  2. On the next page click Done and then back at the main rule page, click the Activate button. You are now ready to test this workflow rule by going to a custom record that is already verified and changing one of the address fields. When you save, the “Verified” flag field should be set to False.

Option 2: Flagging Addresses Un-Verified via Trigger

The custom trigger is very simple in what it does, though a bit harder to set up because it requires creating the trigger in a sandbox and then deploying to production.

Basically, the Address Verify button will only work if the “Address Verified” field is unchecked. That is done in order to minimize the number of API calls you make. Every time the Verify Address button is pressed and there is successful address verification, we check the box, stating that the address has been verified and does not need to be verified again.

This trigger unchecks the verified flag whenever the address is updated again. This is important to do so that the address does not say it is verified, when in fact, changes have been made to it, and the final version has never been verified.

image

When you press the “New” button, a blank textbox will come up. Paste the Apex code that you can find in the “triggers” folder on our GitHub.com repository, linked to below. The code from a file called resetAddressFlag should be pasted entirely inside the available text area and then modified to match the custom object that contains the address you want to verify.
For example, in the example code you will pull from GitHub.com, you will want to make the following changes:

  1. All instances of Office__c will need to be replaced with the API Name of the custom object.

  2. Four field names will possibly need to be updated, depending on the names of those fields within the custom object: Street__c, Zip__c, City__c, and State__c.

GitHub.com Address Verification Sample Code Repository
https://github.com/ToAFinish/addressverification

When finished, press the Save button, and the trigger is now ready to run every time a record is updated.

  • • Note: The next chapter explains how to verify in a list or automatically.

Turning on Field History Tracking

Turning on the field history is very similar to what is done for standard objects. The exception being that custom addresses usually have individual fields for Street, City, State, Zip and Country. For effective tracking, all of these should be turned on.

Modifying the Layout

Modifying the layout for a custom object is exactly the same as what is done for a standard object.

⚠️ **GitHub.com Fallback** ⚠️