Hi all,
My company just procured Ops Hub, and I’d like to use the Custom Code step to automatically delete companies where salesforcedeleted = TRUE.
The problem is I have no idea where to begin with the custom code step! Can someone please help me draft the snippet that would accomplish this task?
Thank you!
Hi @JJensen_Aurora
Here’s how to do it using NodeJS
1 - Create a private app with the scope “crm.objects.companies.write”: Legacy private apps - HubSpot docs
2 - Create a company-based workflow with the triggers to meet salesforcedeleted = TRUE and add a “custom coded action” where you want the company to be deleted
3 - Create a secret with the token of the private app you created at step 1: Workflows | Custom Code Actions - HubSpot docs.
In the code below the name of the secret is OPS_TOKEN but you can name it whatever you want, but if you do name it differently you will need to match it in the code.
4 - Select your secret:
5 - Select record if in the inputs:
6 - Replace the code with this code:
const hubspot = require('@hubspot/api-client');
exports.main = async (event) => {
const token = process.env.OPS_TOKEN
const hubspotClient = new hubspot.Client({"accessToken":token});
const companyId = event.inputFields['hs_object_id'];
try {
const apiResponse = await hubspotClient.crm.companies.basicApi.archive(companyId);
console.log(JSON.stringify(apiResponse, null, 2));
} catch (e) {
e.message === 'HTTP request failed'
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e)
}
}
7 - Save and Ta-dam!
Don’t forget to mark my reply as a solution if you are satisfied. If not, do not hesitate to ask me anything!
Sincerely yours,
The Wizard of Hubs
Wow amazing Louis thank you so much!!
Not only for the solution but also answering in a way that helps me understand the moving pieces here, for the next time I need to create a custom code action.
I did hit some rate-limiting errors with the initial enrollment of companies, but removing the try/catch clause based on this thread’s advice resolved that quite easily.
Thanks again!
Oh sorry. You’re right I didn’t take into account that you would have many records to enroll at first. I’m glad you figured it out.