Hello HubSpot developers,
I am seeking help with my code to troubleshoot an issue. I want to open the email pop-up in contacts. I am currently creating a custom CRM card and have used the command “hs project create” to generate a template.
I have an error of this:
Error opening email editor TypeError: Cannot read properties of undefined (reading ‘openEmailEditor’)
I cannot share my whole code but i will share the part which it has error on it
Here is my component with the button which I expect to be clicked and show the email editor pop-up in contacts.
import React, { useState, useEffect } from "react";
import { Button, hubspot } from "@hubspot/ui-extensions";
// Define the extension to be run within the Hubspot CRM
hubspot.extend(({ context, runServerlessFunction, actions }) => (
<Extension
context={context}
runServerless={runServerlessFunction}
sendAlert={actions.addAlert}
/>
));
const Extension = ({ context, runServerless, sendAlert }) => {
const [loginState, setLoginState] = useState({
token: null,
personId: null,
workEmail: null,
});
const handleSendEmail = async (email) => {
const { response } = await runServerless({
name: "openEmailEditor",
parameters: {
subject: email.scheduledEmail?.subject || "No Subject",
body: email.promptResponse?.text || "No Body Text",
to: [{ email: loginState.workEmail }],
},
});
};
return (
<Button
type="submit"
onClick={() => handleSendEmail(email)}
variant="destructive"
>
Send Email
</Button>
);
};
export default Extension;
then, I created a file openEmailEditor.js:
const hubspot = require("@hubspot/api-client");
exports.main = async (context = {}) => {
console.log(context)
const { subject, body, to } = context.parameters;
try {
await context.actions.openEmailEditor({
subject,
body,
to,
});
return {
statusCode: 200,
};
} catch (error) {
console.error("Error opening email editor", error);
return {
statusCode: error,
};
}
};
as i tried to console.log the context, i dont see any actions there, all i see is my parameters. Here is the example console.log output:
{
propertiesToSend: {},
parameters: {
subject: 'Doubling leads with Union',
body: 'Hi Brian,\n' +
'\n' +
"I wanted to share how our unique approach to sales, inspired by Union's Intent Based Sales, helped us double qualified leads in just a few months. By identifying and prioritizing target audiences, we've seen amazing results. I'm curious, how are you currently approaching lead gen and integrating your marketing, sales and service teams? With your AI-powered HubSpot platform, I bet there are some powerful opportunities to explore.\n" +
'\n' +
'Best,\n' +
'John',
to: [ [Object] ]
},
event: undefined,
secrets: {}
}
Here is my whole serverless.json file:
{
"appFunctions": {
"myFunc": {
"file": "example-function.js",
"secrets": []
},
"emailLogin": {
"file": "email-login.js",
"method": "POST",
"secrets": []
},
"fetchPlaybooks": {
"file": "fetch-playbooks.js",
"method": "GET",
"secrets": []
},
"generatePlaybook": {
"file": "generate-playbook.js",
"method": "POST",
"secrets": []
},
"fetchContact": {
"file": "fetch-contact.js",
"method": "GET",
"secrets": []
},
"fetchEmails": {
"file": "fetch-emails.js",
"method": "GET",
"secrets": []
},
"openEmailEditor": {
"file": "openEmailEditor.js",
"secrets": []
}
}
}