# Abbreviating State/Region with Custom Code Help

**URL:** <https://community.hubspot.com/t/abbreviating-state-region-with-custom-code-help/124845>\
**Category:** Data Hub\
**Created:** [January 27, 2025, 5:46pm UTC](https://community.hubspot.com/t/abbreviating-state-region-with-custom-code-help/124845 "2025-01-27T17:46:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CHopkins1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/chopkins1/32/39503_2.png) [@CHopkins1](https://community.hubspot.com/u/CHopkins1)\
**Post date:** [January 27, 2025, 5:46pm UTC](https://community.hubspot.com/t/abbreviating-state-region-with-custom-code-help/124845/1 "2025-01-27T17:46:11Z")

</div>

I am trying to abbreviate all of the written out State/Regions for companies with the custom code function (Texas → TX) then have the output update the State/Region property.

My private app is set for reading company data, but the logs keep saying “Error: State property not found in response.”  
Here is what I have so far with JSON:

const hubspot = require(‘@hubspot/api-client’);

let abbreviations = new Map();  
abbreviations.set(‘ALABAMA’, ‘AL’);  
abbreviations.set(‘ALASKA’, ‘AK’);  
abbreviations.set(‘ARIZONA’, ‘AZ’);  
abbreviations.set(‘ARKANSAS’, ‘AR’);  
abbreviations.set(‘CALIFORNIA’, ‘CA’);  
abbreviations.set(‘COLORADO’, ‘CO’);  
abbreviations.set(‘CONNECTICUT’, ‘CT’);  
abbreviations.set(‘DELAWARE’, ‘DE’);  
abbreviations.set(‘DISTRICT OF COLUMBIA’, ‘DC’);  
abbreviations.set(‘FLORIDA’, ‘FL’);  
abbreviations.set(‘GEORGIA’, ‘GA’);  
abbreviations.set(‘HAWAII’, ‘HI’);  
abbreviations.set(‘IDAHO’, ‘ID’);  
abbreviations.set(‘ILLINOIS’, ‘IL’);  
abbreviations.set(‘INDIANA’, ‘IN’);  
abbreviations.set(‘IOWA’, ‘IA’);  
abbreviations.set(‘KANSAS’, ‘KS’);  
abbreviations.set(‘KENTUCKY’, ‘KY’);  
abbreviations.set(‘LOUISIANA’, ‘LA’);  
abbreviations.set(‘MAINE’, ‘ME’);  
abbreviations.set(‘MARYLAND’, ‘MD’);  
abbreviations.set(‘MASSACHUSETTS’, ‘MA’);  
abbreviations.set(‘MICHIGAN’, ‘MI’);  
abbreviations.set(‘MINNESOTA’, ‘MN’);  
abbreviations.set(‘MISSISSIPPI’, ‘MS’);  
abbreviations.set(‘MISSOURI’, ‘MO’);  
abbreviations.set(‘MONTANA’, ‘MT’);  
abbreviations.set(‘NEBRASKA’, ‘NE’);  
abbreviations.set(‘NEVADA’, ‘NV’);  
abbreviations.set(‘NEW HAMPSHIRE’, ‘NH’);  
abbreviations.set(‘NEW JERSEY’, ‘NJ’);  
abbreviations.set(‘NEW MEXICO’, ‘NM’);  
abbreviations.set(‘NEW YORK’, ‘NY’);  
abbreviations.set(‘NORTH CAROLINA’, ‘NC’);  
abbreviations.set(‘NORTH DAKOTA’, ‘ND’);  
abbreviations.set(‘OHIO’, ‘OH’);  
abbreviations.set(‘OKLAHOMA’, ‘OK’);  
abbreviations.set(‘OREGON’, ‘OR’);  
abbreviations.set(‘PENNSYLVANIA’, ‘PA’);  
abbreviations.set(‘RHODE ISLAND’, ‘RI’);  
abbreviations.set(‘SOUTH CAROLINA’, ‘SC’);  
abbreviations.set(‘SOUTH DAKOTA’, ‘SD’);  
abbreviations.set(‘TENNESSEE’, ‘TN’);  
abbreviations.set(‘TEXAS’, ‘TX’);  
abbreviations.set(‘UTAH’, ‘UT’);  
abbreviations.set(‘VERMONT’, ‘VT’);  
abbreviations.set(‘VIRGINIA’, ‘VA’);  
abbreviations.set(‘WASHINGTON’, ‘WA’);  
abbreviations.set(‘WEST VIRGINIA’, ‘WV’);  
abbreviations.set(‘WISCONSIN’, ‘WI’);  
abbreviations.set(‘WYOMING’, ‘WY’);

exports.main = (event, callback) =\> {  
const hubspotClient = new hubspot.Client({  
accessToken: process.env.normalizing\_data  
});

hubspotClient.crm.companies.basicApi.getById(event.object.objectId, [“state”])  
.then(results =\> {  
// Directly access the state from properties  
const state = results.body && results.body.properties && results.body.properties.state;

if (state) {  
let test = state.toUpperCase();  
let output;  
if (abbreviations.has(test)) {  
output = abbreviations.get(test);  
} else {  
output = state;  
}

callback({  
outputFields: {  
state\_abbreviation: output  
}  
});  
} else {  
console.error(‘Error: State property not found in response.’);  
callback({  
outputFields: {  
state\_abbreviation: “Error: State property missing”  
}  
});  
}  
})  
.catch(err =\> {  
console.error(‘Error fetching company data:’, err);  
callback({  
outputFields: {  
state\_abbreviation: “Error: Unable to retrieve data”  
}  
});  
});  
};  
Can anyone help?

---

<div class="post-metadata">

**Author:** ![kennedyp](https://avatars.discourse-cdn.com/v4/letter/k/49beb7/32.png) [@kennedyp](https://community.hubspot.com/u/kennedyp)\
**Post date:** [January 27, 2025, 10:42pm UTC](https://community.hubspot.com/t/abbreviating-state-region-with-custom-code-help/124845/2 "2025-01-27T22:42:52Z")

</div>

Hi @CHopkins1! Welcome to the Community-- happy to have you here 😊

Thank you for including your code and response here. I always recommend verifying the internal property name for the property you are accessing, verifying that your app has scopes for the respective scopes of any data that you’re trying to access and that the company you are testing with has a value for that field.

If you haven’t seen it already, I also highly recommend @KyleJepson’s post: [Code to standardize state/region](https://community.hubspot.com/t5/9881-Operations-Hub/Code-to-standardize-state-region/m-p/410068) that walks through this code process in both directions.

_Best,_

Kennedy

---

<div class="post-metadata">

**Author:** ![CHopkins1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/chopkins1/32/39503_2.png) [@CHopkins1](https://community.hubspot.com/u/CHopkins1)\
**Post date:** [January 28, 2025, 2:52am UTC](https://community.hubspot.com/t/abbreviating-state-region-with-custom-code-help/124845/3 "2025-01-28T02:52:34Z")

</div>

Kennedy - I’ve verified all of the items you listed, all look correct. I found that post even before posting and if you read the comments you can see they are coming across a similar error, so this doesn’t solve my issue. Thanks for your time though!

---

<div class="post-metadata">

**Author:** ![kennedyp](https://avatars.discourse-cdn.com/v4/letter/k/49beb7/32.png) [@kennedyp](https://community.hubspot.com/u/kennedyp)\
**Post date:** [February 10, 2025, 5:24pm UTC](https://community.hubspot.com/t/abbreviating-state-region-with-custom-code-help/124845/4 "2025-02-10T17:24:18Z")

</div>

I want to invite some subject matter experts to see if they have any insight.

Hey @louischausse, @danmoyle, @Bortami, @KyleJepson do you have any troubleshooting suggestions?

---

<div class="post-metadata">

**Author:** ![CHopkins1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/chopkins1/32/39503_2.png) [@CHopkins1](https://community.hubspot.com/u/CHopkins1)\
**Post date:** [February 10, 2025, 5:35pm UTC](https://community.hubspot.com/t/abbreviating-state-region-with-custom-code-help/124845/5 "2025-02-10T17:35:15Z")

</div>

Hi Kennedy, I was able to figure it out with other sources! Thanks for your help on this

---

<div class="post-metadata">

**Author:** ![kennedyp](https://avatars.discourse-cdn.com/v4/letter/k/49beb7/32.png) [@kennedyp](https://community.hubspot.com/u/kennedyp)\
**Post date:** [February 11, 2025, 12:13am UTC](https://community.hubspot.com/t/abbreviating-state-region-with-custom-code-help/124845/6 "2025-02-11T00:13:40Z")

</div>

So glad to hear! If you can share your findings, I am sure you’d be a big help to others in the community as well 😊

---

<div class="post-metadata">

**Author:** ![CHopkins1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/chopkins1/32/39503_2.png) [@CHopkins1](https://community.hubspot.com/u/CHopkins1)\
**Post date:** [February 11, 2025, 8:20pm UTC](https://community.hubspot.com/t/abbreviating-state-region-with-custom-code-help/124845/7 "2025-02-11T20:20:09Z")

</div>

Sure, no problem! I ended just adding the library, Axios, and was able to fix it that way.

Code starts here:

/\*  
\*  
\* Edit Secret Name here  
\*/  
const SECRET\_NAME = “normalizing\_data”

const axios = require(‘axios’);

const axiosConfig = {  
headers: {  
authorization: `Bearer ${process.env[SECRET_NAME]}`  
}  
};

// Define a mapping of state names to abbreviations  
const stateAbbreviations = {  
‘ALABAMA’: ‘AL’, ‘ALASKA’: ‘AK’, ‘ARIZONA’: ‘AZ’, ‘ARKANSAS’: ‘AR’, ‘CALIFORNIA’: ‘CA’,  
‘COLORADO’: ‘CO’, ‘CONNECTICUT’: ‘CT’, ‘DELAWARE’: ‘DE’, ‘FLORIDA’: ‘FL’, ‘GEORGIA’: ‘GA’,  
‘HAWAII’: ‘HI’, ‘IDAHO’: ‘ID’, ‘ILLINOIS’: ‘IL’, ‘INDIANA’: ‘IN’, ‘IOWA’: ‘IA’,  
‘KANSAS’: ‘KS’, ‘KENTUCKY’: ‘KY’, ‘LOUISIANA’: ‘LA’, ‘MAINE’: ‘ME’, ‘MARYLAND’: ‘MD’,  
‘MASSACHUSETTS’: ‘MA’, ‘MICHIGAN’: ‘MI’, ‘MINNESOTA’: ‘MN’, ‘MISSISSIPPI’: ‘MS’, ‘MISSOURI’: ‘MO’,  
‘MONTANA’: ‘MT’, ‘NEBRASKA’: ‘NE’, ‘NEVADA’: ‘NV’, ‘NEW HAMPSHIRE’: ‘NH’, ‘NEW JERSEY’: ‘NJ’,  
‘NEW MEXICO’: ‘NM’, ‘NEW YORK’: ‘NY’, ‘NORTH CAROLINA’: ‘NC’, ‘NORTH DAKOTA’: ‘ND’, ‘OHIO’: ‘OH’,  
‘OKLAHOMA’: ‘OK’, ‘OREGON’: ‘OR’, ‘PENNSYLVANIA’: ‘PA’, ‘RHODE ISLAND’: ‘RI’, ‘SOUTH CAROLINA’: ‘SC’,  
‘SOUTH DAKOTA’: ‘SD’, ‘TENNESSEE’: ‘TN’, ‘TEXAS’: ‘TX’, ‘UTAH’: ‘UT’, ‘VERMONT’: ‘VT’,  
‘VIRGINIA’: ‘VA’, ‘WASHINGTON’: ‘WA’, ‘WEST VIRGINIA’: ‘WV’, ‘WISCONSIN’: ‘WI’, ‘WYOMING’: ‘WY’  
};

exports.main = async (event, callback) =\> {  
try {  
// Verify HubSpot API token by making a test request  
const authResponse = await axios.get(‘[https://api.hubapi.com/crm/v3/objects/companies](https://api.hubapi.com/crm/v3/objects/companies)’, axiosConfig);

if (authResponse.status !== 200) {  
console.error(‘Failed to authenticate with HubSpot API’);  
return callback({ outputFields: { abbreviated\_state: ‘’ } });  
}

// Get the state property from the company event  
const state = event.inputFields[‘state’];

if (!state) {  
// If state is null or undefined, output an empty abbreviated\_state  
return callback({ outputFields: { abbreviated\_state: ‘’ } });  
}

// Convert the state to uppercase for comparison  
const upperState = state.toUpperCase().trim();

// Check if the state is already an abbreviation  
if (Object.values(stateAbbreviations).includes(upperState)) {  
// If it’s already abbreviated, output it as is  
return callback({ outputFields: { abbreviated\_state: upperState } });  
}

// Find the abbreviation for the state name  
const abbreviation = stateAbbreviations[upperState] || ‘’;

// Output the abbreviation or an empty string if not found  
callback({ outputFields: { abbreviated\_state: abbreviation } });  
} catch (error) {  
console.error(‘Error processing state or authenticating:’, error);  
callback({ outputFields: { abbreviated\_state: ‘’ } });  
}  
};
