The idea of this is to provides a "framework" to work locally on your Custom Coded Action and execute it in the same context as HubSpot.
You can write your Custom Coded Action locally, as we mocked the specific functions and limitations that exist with Operations Hub pro.
As an example : the project will prevent you to add librairies that are not allowed to be imported on the HubSpot Custom Coded Action. list of allowed librairies here
Clone the project and install the dependencies by running :
npm install
Then create a .env file at the root of the project with your privateAppToken
privateAppToken = "sdfsd-dsfsdf-wwxcwx-ffdsdfdsf-fsdffdsfs"
You have to initialize a new project by calling
npm run init <nameOfYourProject>
Like :
npm run init my-new-custom-coded-action
The template created contains a first file name cca.js, this is where you write your code.
const axios = require('axios');
const axiosConfig = {
headers: {
authorization: `Bearer ${process.env.privateAppToken}`
}
};
exports.main = async (event, callback) => {
/**
* @name getPortalInfo
* @desc Grab the portal id and various other infos
* @returns {promise}it returns an axios object
*/
const getPortalInfo = async () => {
const endpoint = `https://api.hubapi.com/integrations/v1/me`;
return axios.get(endpoint, axiosConfig);
}
const domainName = event.inputFields.domainName;
if (!domainName) throw new Error('domainName is not set, are you sure you put domainName in the "properties to include in code" ? ');
const portalInfos = await getPortalInfo();
if (!portalInfos.data) throw new Error(`We couldn't grab your portal infos`);
const { portalId, timeZone, currency } = portalInfos.data;
callback({
outputFields: {
portalId,
timeZone,
currency
}
});
}
The event.js file represent the properties you can include in code.
exports.events = {
// object: {
// objectId: 3401
// },
inputFields: {
domainName : "google.com"
}
}
In this example to access companyName, you have to use :
const domainName = event.inputFields.domainName;
Then execute the code by calling :
node run.js ./name-of-the-folder/file-name.js
or
npm run cca ./get-siret-from-domain-name/cca.js
Or for more confort it can be a good idea to create an alias in your bashrc / .zshrc
example :
cca(){
node /Users/userName/Documents/code/HubSpot-Operations-Hub-Custom-Coded-Action/run.js $1;
}
Then to invoke the Custom Coded Action you can just call
cca ./merge-companies-based-on-name/cca.js
The Custom Coded Action in the workflows doesn't let you install the libraires you want list of allowed librairies here.
If you really need to use a library you can. To do so you will need to compile your code in a single file with the following lib : @vercel/ncc
To make it a bit simpler I recommend you use the compiler I created
Run the tests by running mocha with :
npx mocha
How to run the tests for a given Custom Coded Action
npx mocha ./format-us-phone-number/cca.js
This will only work if the Custom Coded Action folder contains à test folder
Ideas to write a good Custom Coded Action. The code should be stateless as much a possible. If it's stateless it will be easy to test it.
This is a test for a Custom Coded Action which is stateless, the Custom Coded Action doesn't call the API. All the CRUD operatations happen outside of the Custom Coded Action.
describe('Integration no copy', () => {
const events = {
inputFields: {
firstName: "Peter",
lastName: "Obriens",
email: "Peter-OBriens@fake.com"
}
}
await cca.main(events, output => {
const { gender, firstName } = output.outputFields;
console.log( { gender, firstName })
output = output.outputFields;
return assert.deepEqual(output, { gender: 'M', firstName: 'Peter' })
});
});