⚙ Operations Hub

JJensen_Aurora
Participant

How to delete company with custom code workflow

SOLVE

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!

 

1 Accepted solution
louischausse
Solution
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

How to delete company with custom code workflow

SOLVE

Hi @JJensen_Aurora 

Here's how to do it using NodeJS

1 - Create a private app with the scope "crm.objects.companies.write": https://developers.hubspot.com/docs/api/private-apps
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: https://developers.hubspot.com/docs/api/workflows/custom-code-actions#:~:text=To%20add%20a%20new,fut....
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:

louischausse_1-1699372327041.png

 



5 - Select record if in the inputs:

louischausse_0-1699372043357.png

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 🧙‍

Louis Chaussé
The Wizard of Hubs

CEO

Auxilio

lchausse@auxilio.io
auxilio.io
Schedule a call

View solution in original post

4 Replies 4
louischausse
Solution
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

How to delete company with custom code workflow

SOLVE

Hi @JJensen_Aurora 

Here's how to do it using NodeJS

1 - Create a private app with the scope "crm.objects.companies.write": https://developers.hubspot.com/docs/api/private-apps
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: https://developers.hubspot.com/docs/api/workflows/custom-code-actions#:~:text=To%20add%20a%20new,fut....
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:

louischausse_1-1699372327041.png

 



5 - Select record if in the inputs:

louischausse_0-1699372043357.png

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 🧙‍

Louis Chaussé
The Wizard of Hubs

CEO

Auxilio

lchausse@auxilio.io
auxilio.io
Schedule a call
JJensen_Aurora
Participant

How to delete company with custom code workflow

SOLVE

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!

louischausse
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

How to delete company with custom code workflow

SOLVE

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.

Louis Chaussé
The Wizard of Hubs

CEO

Auxilio

lchausse@auxilio.io
auxilio.io
Schedule a call
BérangèreL
Community Manager
Community Manager

How to delete company with custom code workflow

SOLVE

Hi @JJensen_Aurora,

Congratulations on acquiring the Operations Hub 🎉 and thank you for reaching out to the Community!

I'd like to ask some of our Community Members who used custom code: Hi @jjones41@Sawdog and @asgJim did you end up using custom code? If yes, could you please share some insights with @JJensen_Aurora, please?

I'd like to also invite some of our top experts on this subject: Hello @louischausse@Teun and @ChrisoKlepke do you have suggestions to help @JJensen_Aurora, please?

If anybody else has anything to add and/or share, please feel free to join in the conversation 🙂

Thank you very much 🌟 and have a wonderful day!

Best,
Bérangère


Saviez-vous que la Communauté est disponible en Français ?
Rejoignez les discussions francophones en changeant votre langue dans les paramètres !

Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings!