Feb 17, 202111:37 AM - edited May 10, 20212:51 PM
HubSpot Employee
Code to standardize state/region
This code takes two-letter abbreviations from the State/Region property and outputs the full name. Works for all U.S. states and territories and Canadian providences. If you want it to work in other places, you'll have to expand the list of abbreviations yourself 😛
This doesn't actually update the record. To do that, you'll need to add a workflow action that copies the output value from the code into the State/Region contact property. (Or add an additional API call to the code to update the property.)
TO MAKE THIS WORK FOR COMPANIES: Change contacts to companies in hubspotClient.crm.contacts.basicApi.getById.
TO MAKE THIS WORK THE OTHER WAY AROUND: Would take a few extra steps. This code takes the value from State/Region, uses .toUpperCase to make it ALL CAPS, and then searches the list of abbreviations (which are also ALL CAPS) to look for matches. There isn't a standard method like .toUpperCase that puts things in Title Case, and the .has method for maps is case sensitive. Luckily, the Data Format action (also included in Operations Hub) does all kinds of things with capitalization, so you could do it that way. I think maybe the easiest thing to do is to reverse the list of abbreviations and make the state names ALL UPPERCASE like the abbreviation are. Then use the code basically like it is. The output of this code could be pulled into a Data Format action to fix the capitalization, and then copy the output of the Data Format action into the property.
OTHER NOTES: This works great if all your State/Region values are two-letter abbreviations or properly spelled/capitalized full names. If you have people using old fashioned abbreviations (Ill. for Illinois, Mass. for Massachusetts, Minn. for Minnesota) or if you have typoes (Texsa for Texas), this won't help with that. In a future world, when custom-coded actions can be used in branching logic, it would be worthwhile to have a code that takes State/Region and, if the value isn't found in the array below, it creates a task and assigns it to the contact owner to have them fix the problem.
This is great, @KyleJepson ! All about that data cleanliness 👏
I'm using this custom code to update Companies (I did changecontacts to companies in hubspotClient.crm.contacts.basicApi.getById), but am having trouble with the output. The code successfully executes but continues to fail when the state string value is meant to copy over. Any thoughts?
Man. I've never seen this before. Without being able to dig into the workflow itself and poke around, I'm probably not going to be able to fix it. At this point, your best bet is probably to reach out to our Support team and see if they can get to the bottom of it. Sorry not to be more help!