⚙ Operations Hub

iingallslawtige
参加者

Associate Deal to Company

解決

Hello,

 

I am working on using HubSpot Custom Code Workflow Actions to associate the deal in the workflow with a specific company (I already know the company ID).

 

For the property included in the code, I have the Deal ID as hs_object_id

 

Here is the code I have written: 

 

const hubspot = require('@hubspot/api-client')

exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
accessToken: process.env.redacted
});

async function associateDealWithCompany(dealId, companyId) {
try {
const association = {
"inputs": [
{
"from": {
"id": event.inputFields['hs_object_id']
},
"to": {
"id": 16319160389
},
"type": "deal_to_company"
}
]
};
await hubspotClient.crm.associations.batchApi.create('deals', 'companies', association);
console.log(`Deal ${dealId} has been associated with company ${companyId}`);
} catch (err) {
console.error(err);
}
}
}

 

The hs_execution_state status response is success.

 

Please help me - thanks!

0 いいね!
1件の承認済みベストアンサー
louischausse
解決策
キーアドバイザー | Platinum Partner
キーアドバイザー | Platinum Partner

Associate Deal to Company

解決

Hi @iingallslawtige 

I saw some inconsistencies lately using hubspot node js client with the association API.

I prefer to use axios.

Here's how it goes:

const axios = require('axios');

exports.main = async (event) => {
 
  const dealId = event.inputFields['hs_object_id'];
  const token = process.env.secret
  const companyId = "16319160389" 

  // define associate() function

  async function associate(fromObjectType,fromObjectId,toObjectType,toObjectId) {

    console.log(`trying to associate ${fromObjectType} of id ${fromObjectId} with ${toObjectType} of id ${toObjectId}...`)

    const options = {
      method: 'PUT',
      url: `https://api.hubapi.com/crm/v4/objects/${fromObjectType}/${fromObjectId}/associations/default/${toObjectType}/${toObjectId}`,
      headers: {
        'content-type': 'application/json',
        'accept': 'application/json',
        'authorization': `Bearer ${token}`
      }
    };

    try {
      const response = await axios(options);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  //exexute associate() function
      var fromObjectType = "deals"
      var fromObjectId = dealId
      var toObjectType = companies
      var toObjectId = companyId
      associate(fromObjectType,fromObjectId,toObjectType,toObjectId)
        .then(association => {
        console.log(association.status);
        console.log(association.results[0]);
      })
        .catch(error => {
        console.error(error);
      });
}
Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link

元の投稿で解決策を見る

14件の返信
CraigObee
参加者

Associate Deal to Company

解決

I'm looking to get code to work to associate deals we're creating via API/Web forms to the correct companys (company IDs will be known as a deal property value). I've looked at multiple other community posts but non of the codes are quite right and erroring when trying to run them. any help would be appreciated! 

0 いいね!
louischausse
キーアドバイザー | Platinum Partner
キーアドバイザー | Platinum Partner

Associate Deal to Company

解決

@CraigObee 

CAn you share your existing core and the error you get?

Also can you share where the code is hosted? Is it in your app or in a custom coded action in a HubSpot workflow?

Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link
0 いいね!
CraigObee
参加者

Associate Deal to Company

解決

Hi Louis, my colleague managed to get it working. it was using custom code on a deal workflow where the company ID value was present in a deal property.

We're now working on doing this for tickets. applying this to deal and ticket workflows will address an annoying issue we have which I raised in a separate post. https://community.hubspot.com/t5/HubSpot-Ideas/Associate-only-companies-associated-to-the-existing-D...

louischausse
キーアドバイザー | Platinum Partner
キーアドバイザー | Platinum Partner

Associate Deal to Company

解決

Share your code and errors if you need help

Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link
0 いいね!
DStaat
参加者

Associate Deal to Company

解決

Hey iingallslawtige,

We have an app called Associ8 that would solve your problem. No code required! It creates a workflow action that associates objects together based on your search parameters (the company ID and deal ID in your case). Check it out here.

Let me know if you have any questions or need help setting up. I'd be happy to hop on a call with you!

Best,

-David Staat

0 いいね!
louischausse
キーアドバイザー | Platinum Partner
キーアドバイザー | Platinum Partner

Associate Deal to Company

解決

Hi @iingallslawtige 

Do you have an error in the console? If yes please copy/paste it here.
Do you want to apply an association label or not?

Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link
iingallslawtige
参加者

Associate Deal to Company

解決

Hi Louis, thanks for the message!

 

Based on Version 8.9

 

Not looking for an association label (unless required). Just looking to associate the deal with a company.

 

Current Code:

 

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

exports.main = (event, callback) => {
  const hubspotClient = new hubspot.Client({
    accessToken: process.env.secret
  });
 hubspotClient.crm.objects.associationsApi.create("DEAL",event.inputFields['hs_object_id'],"COMPANY",16319160389,"deal_to_company",5)
.catch(err => {
    console.error(err);
});
}

 

 

 

Error Message:

2023-07-12T14:11:54.028Z	ERROR	TypeError: Cannot read properties of undefined (reading 'makeRequestContext')
    at AssociationsApiRequestFactory.<anonymous> (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/apis/AssociationsApi.js:87:55)
    at Generator.next (<anonymous>)
    at /opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/apis/AssociationsApi.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/apis/AssociationsApi.js:4:12)
    at AssociationsApiRequestFactory.create (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/apis/AssociationsApi.js:64:16)
    at ObservableAssociationsApi.create (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/types/ObservableAPI.js:29:59)
    at PromiseAssociationsApi.create (/opt/nodejs/node_modules/@hubspot/api-client/lib/codegen/crm/objects/types/PromiseAPI.js:14:33)
    at Object.exports.main (/var/task/file.js:7:44)
    at Runtime.exports.hubspot_handler [as handler] (/var/task/hubspotHandler.js:6:21)

Memory: 72/128 MB
Runtime: 854.28 ms

 

0 いいね!
louischausse
解決策
キーアドバイザー | Platinum Partner
キーアドバイザー | Platinum Partner

Associate Deal to Company

解決

Hi @iingallslawtige 

I saw some inconsistencies lately using hubspot node js client with the association API.

I prefer to use axios.

Here's how it goes:

const axios = require('axios');

exports.main = async (event) => {
 
  const dealId = event.inputFields['hs_object_id'];
  const token = process.env.secret
  const companyId = "16319160389" 

  // define associate() function

  async function associate(fromObjectType,fromObjectId,toObjectType,toObjectId) {

    console.log(`trying to associate ${fromObjectType} of id ${fromObjectId} with ${toObjectType} of id ${toObjectId}...`)

    const options = {
      method: 'PUT',
      url: `https://api.hubapi.com/crm/v4/objects/${fromObjectType}/${fromObjectId}/associations/default/${toObjectType}/${toObjectId}`,
      headers: {
        'content-type': 'application/json',
        'accept': 'application/json',
        'authorization': `Bearer ${token}`
      }
    };

    try {
      const response = await axios(options);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  //exexute associate() function
      var fromObjectType = "deals"
      var fromObjectId = dealId
      var toObjectType = companies
      var toObjectId = companyId
      associate(fromObjectType,fromObjectId,toObjectType,toObjectId)
        .then(association => {
        console.log(association.status);
        console.log(association.results[0]);
      })
        .catch(error => {
        console.error(error);
      });
}
Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link
MSaurat
投稿者

Associate Deal to Company

解決

Hi Louis, 

Apologies, I'm not a developer and I can't really understand. Are you able to create a code (whether in Python or Node.jx as these are the only ones avaiable in Custom Code) to associate a deal with the primary company the contact is from. I've looked around for different codes and tried and none of them worked.

Would be great thank you!

 

M

0 いいね!
louischausse
キーアドバイザー | Platinum Partner
キーアドバイザー | Platinum Partner

Associate Deal to Company

解決

Hi @MSaurat , 

Have you tried the no code solutions to associate record in workflows? It's new in beta and may fit your need: https://knowledge.hubspot.com/workflows/manage-crm-record-associations-with-workflows

To activate the beta you need to click on Avatar (top right corner) > Product updates and search for "New Workflow Actions for Associations"

If it doesn't work for your use case, contact me through this form (https://www.auxilio.io/en/contact) and I'll send you a quote to develop a custom automation for you.


Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link
0 いいね!
MSaurat
投稿者

Associate Deal to Company

解決

Hey Louis, 

 

Yes I've tried and tested but nothing in the way of associating a deal wirh a contact primary company. If you have some tips let me know. 

 

Thanks

0 いいね!
louischausse
キーアドバイザー | Platinum Partner
キーアドバイザー | Platinum Partner

Associate Deal to Company

解決

It depends of your process. What is the trigger? When the deal hits a specific stage?

Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link
0 いいね!
iingallslawtige
参加者

Associate Deal to Company

解決

Amazing, that worked perfectly. Thank you.

BérangèreL
コミュニティーマネージャー
コミュニティーマネージャー

Associate Deal to Company

解決

Hi @iingallslawtige,

Thanks for reaching out to the Community!

I found on this similar post "Creating An Association In Custom Code Workflow" several options that might work for you.

I also wanted to invite a couple of subject matter experts to this conversation @louischausse@Teun and @HFisher7: do you have any tips to help @iingallslawtige to associate a deal to a specific company via a custom coded workflow action please?

Thanks and have a lovely day!
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!