Abbreviating State/Region with Custom Code Help

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?

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

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 that walks through this code process in both directions.

Best,

Kennedy

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!

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?

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

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 :blush:

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’, 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: ‘’ } });
}
};