Button onclick to copy the text and open email pop-up in contacts

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": []
 }
 }
}

Hi @JMacasero

The issue in your code lies in the way you’re attempting to access the actions object within the openEmailEditor.js file.

The problem is that the context object passed to your serverless function doesn’t directly contain the actions property, resulting in the error “Cannot read properties of undefined” when trying to access context.actions.openEmailEditor.

To resolve this, you need to access the actions object through the nested extensions property provided by HubSpot’s serverless function context, using context.extensions.actions.

Make sure you’ve imported the necessary modules, including const { actions } = require(“@hubspot/extensions”);, and double-check your serverless function configuration in serverless.json to confirm openEmailEditor is defined correctly.

By applying this fix, you should now be able to access the openEmailEditor function from the actions object and successfully launch the email editor pop-up when the button is clicked.

Hope this helps - Happy to help further!!
Thank you very much and have a great one!
Warm regards

still error .
i also tried to install it. npm install @hubspot/extensions, but package doesnt exist

Error: Cannot find module ‘@hubspot/extensions’

const {actions} = require("@hubspot/extensions")

exports.main = async (context = {}) => {
 const { subject, body, to } = context.parameters;
 try {
 console.log(body)
 // await context.actions.openEmailEditor({
 // subject,
 // body,
 // to,
 // });

 return {
 statusCode: 200,
 };
 } catch (error) {
 console.error("Error opening email editor", error);
 return {
 statusCode: error,
 };
 }
};

here is my app.functions package.json:

{
 "name": "hubspot-example-function",
 "version": "0.1.0",
 "author": "HubSpot",
 "license": "MIT",
 "dependencies": {
 "@hubspot/api-client": "^7.0.1",
 "@hubspot/ui-extensions": "^0.8.26",
 "axios": "^0.27.2"
 }
}

Hi @JMacasero

There appears to be a misunderstanding regarding the @hubspot/extensions package, which is likely intended for internal HubSpot development tools and not publicly available through npm.

Give a try by copying text and opening the email editor pop-up, consider the following approach:

  1. Implement a nested serverless function, which provides a clean separation between UI interactions and accessing CRM actions.
  2. This involves creating a new serverless function, openEmailInCRM.js, and importing necessary modules, including @hubspot/ui-extensions.
  3. Utilize the useServerlessFunction hook and update serverless.json to include the new function.

Lastly, call the nested function from openEmailEditor.js to achieve the desired outcome.

Sample useServerlessFunction Javascript&colon;

import React from ‘react’;

import { useServerlessFunction } from ‘@hubspot/ui-extensions’;

export default async function openEmailInCRM(context) {

const { subject, body, to } = context.parameters;

const { actions } = useServerlessFunction();

try {

**await actions.openEmailEditor({ subject, body, to });**

} catch (error) {

**console.error("Error opening email editor", error);**

}

}

Update serverless.json:

Add the new openEmailInCRM.js function to your serverless.json:

JSON

{

“appFunctions”: {

**// ... other functions**

**"openEmailInCRM": {**

  **"file": "openEmailInCRM.js",**

  **"secrets": \[]**

**}**

}

}

Call Nested Function from openEmailEditor.js:

In your openEmailEditor.js, call the openEmailInCRM.js function:

JavaScript

const hubspot = require(“@hubspot/api-client”);

exports.main = async (context = {}) => {

console.log(context);

const { subject, body, to } = context.parameters;

try {

**await context.runServerlessFunction({**

  **name: "openEmailInCRM",**

  **parameters: {**

    **subject,**

    **body,**

    **to,**

  **},**

**});**

**return {**

  **statusCode: 200,**

**};**

} catch (error) {

**console.error("Error opening email editor", error);**

**return {**

  **statusCode: error,**

**};**

}

};

Regards