⚙ Operations Hub

JamesonC
参加者

Programmable Automation | Inability to save when template literal added

解決

👋 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.

 

Screenshot 2024-09-20 at 8.52.36 AM.png

 

 

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'
            }
        });
    }
};

 

0 いいね!
1件の承認済みベストアンサー
Teun
解決策
名誉エキスパート | Elite Partner
名誉エキスパート | Elite Partner

Programmable Automation | Inability to save when template literal added

解決

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.



Learn more about HubSpot by following me on LinkedIn or YouTube

Did my answer solve your issue? Help the community by marking it as the solution.


元の投稿で解決策を見る

0 いいね!
5件の返信
Teun
名誉エキスパート | Elite Partner
名誉エキスパート | Elite Partner

Programmable Automation | Inability to save when template literal added

解決

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.



Learn more about HubSpot by following me on LinkedIn or YouTube

Did my answer solve your issue? Help the community by marking it as the solution.


0 いいね!
JamesonC
参加者

Programmable Automation | Inability to save when template literal added

解決

Hi @Teun

 

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

Teun
解決策
名誉エキスパート | Elite Partner
名誉エキスパート | Elite Partner

Programmable Automation | Inability to save when template literal added

解決

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.



Learn more about HubSpot by following me on LinkedIn or YouTube

Did my answer solve your issue? Help the community by marking it as the solution.


0 いいね!
JamesonC
参加者

Programmable Automation | Inability to save when template literal added

解決

Wow... hahah

 

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

Teun
名誉エキスパート | Elite Partner
名誉エキスパート | Elite Partner

Programmable Automation | Inability to save when template literal added

解決

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



Learn more about HubSpot by following me on LinkedIn or YouTube

Did my answer solve your issue? Help the community by marking it as the solution.