Programmable Automation | Inability to save when template literal added

:waving_hand: Hi everyone,

I want to make an API call to OpenAI to categorize a HubSpot contact’s job title into 1 of 8 job functions.

For some reason, HubSpot’s custom code editor does not allow me to save the code when I add the template literal ${jobTitle} into my code.

If I remove the template literal, it saves. See error message and red box in the screenshot.

I believe my code is right, but am I missing something trivial?

Any help is greatly appreciated.

exports.main = async (event, callback) => {
 // OpenAI API endpoint and key
 const openaiUrl = 'https://api.openai.com/v1/chat/completions';
 const apiKey = process.env.OPENAI_API_KEY; // Use secret storage in HubSpot for API keys

 // Extract the jobTitle from the contact
 const jobTitle = event.inputFields['jobTitle']; 
 console.log(jobTitle);

 // Prepare the request data for OpenAI
 const data = {
 model: 'gpt-3.5-turbo', // or 'gpt-4' based on your preference
 messages: [
 {
 role: 'system',
 content: 'You are a helpful assistant.'
 },
 {
 role: 'user',
 content: `A HubSpot contact has the job title, '${jobTitle}' - pick 1 category they fall into based on these options: [Executive, Marketing, Sales, HR/Office/People, Billing, Distributor, Production / Operations, Merch Store].`
 }
 ],
 max_tokens: 10,
 temperature: 0
 };

 try {
 // Send the request to OpenAI using fetch
 const response = await fetch(openaiUrl, {
 method: 'POST',
 headers: {
 'Content-Type': 'application/json',
 'Authorization': `Bearer ${apiKey}`
 },
 body: JSON.stringify(data)
 });

 // Parse the JSON response correctly
 const responseData = await response.json(); // Define responseData here

 // Get the job function from the parsed response
 const job_function = responseData.choices[0].message.content.trim();

 // Return the job function as the output of the custom action
 callback({
 outputFields: {
 job_function: job_function
 }
 });

 } catch (error) {
 console.error('Error:', error);
 callback({
 outputFields: {
 job_function: 'Error'
 }
 });
 }
};

Hi @JamesonC ,

Could you try to remove the single quotes around ${jobTitle}?

I’ve used similar code quite a lot in custom code actions, and passing a variable to a string with backticks shouldn’t be an issue, so the only thing I can think of is the single quotes wrapping the ${jobTitle} in your prompt.
If that does solve your issue, you probably want to notify HubSpot support, because that does seem like a bug.

Hi @Teun,

Unfortunately, that didn’t work either… I guess I’ll move forward with reporting this as a potential bug.

I had a similar issue in the past, could you try renaming `jobTitle` to something else? I couldn’t save a code action and thought I was going crazy, but renaming the variable solved it.

Wow… hahah

That worked @Teun! Never would have thought of that. Thank you for the idea!

Hahaha that’s awesome. Still a bug, but happy you got it solved!