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?