Re: Issues Retrieving Association Labels for (deal to deal) Associations via HubSpot API

v3rnalequinox
Participant

Hey everyone,

I’m running into an issue with the HubSpot API when trying to retrieve association labels for (deal to deal) associations.

For context, I have a Deal that is associated with two other Deals using a custom Association Label called Renewal (Deal to Deal + 1 to Many + USER_DEFINED). When I query the HubSpot API, I’m not getting the Association Label Name or ID in the response. The results are always empty regardless of following V3 or V4 documentation. 

I’m using a private app and believe I have the correct scopes as I am not seeing any scope related issues in the response like I have histrocially. I am able to access just about every other part of the API with no issues.

Has anyone else run into this issue or found a workaround? Anything specific with scopes here? Appreciate any insights,

Thanks in advance!

0 Upvotes
1 Accepted solution
BBaber
Solution
Contributor

I was getting the same log when I tested on my end. I believe its because the batchApi endpoint only gives generic info and not detailed information. 

Did this and was able to get the label and typeId. Let me know if this works for you. 

 

try {
  console.log("***** Fetching deals associated with deal ID *****", dealId);
  const associationsResult = await hubspotClient.crm.associations.v4.basicApi.getPage(
    "DEAL",
    dealId,
    "DEAL",
    undefined,
    500
  );
  console.log("Associated deals API response received");

  const allAssociations = associationsResult.results;
  console.log("Number of associated deals found:", allAssociations.length);

  if (allAssociations.length > 0) {
    console.log("Found associated deals, checking for any association labels...");
    console.log("Raw associations data:", JSON.stringify(allAssociations));
    for (const association of allAssociations) {
      const otherDealId = association.toObjectId;
      console.log("Checking associated deal ID:", otherDealId);
      if (association.associationTypes && association.associationTypes.length > 0) {
        for (const assocType of association.associationTypes) {
          console.log("Association details for deal " + otherDealId + ": Label: " + assocType.label + ", Type ID: " + assocType.typeId);
        }
      } else {
        console.log("No detailed association types available for deal " + otherDealId + ".");
      }
    }
  }
} catch (error) {
  console.error("Error fetching associations:", error);
}

 


Edit:
Upon looking further the issue is the way you were looking at the response body. 
The otherDealId = association.id should be otherDealId = association.toObjectId

Also, not including getPage, didn't return the full response but only what you were seeing before. 

const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({ accessToken: process.env.TOKEN });

exports.main = async (event, callback) => {
  try {
    const dealId = event.object.objectId;
    console.log("***** Fetching deals associated with deal ID *****", dealId);
    
    const BatchInputPublicFetchAssociationsBatchRequest = { inputs: [{ id: dealId }] };
    const fromObjectType = "Deal";
    const toObjectType = "Deal";
    
    const associationsResult = await hubspotClient.crm.associations.v4.batchApi.getPage(
      fromObjectType,
      toObjectType,
      BatchInputPublicFetchAssociationsBatchRequest
    );
    console.log("Associated deals API response received");
    
    const allAssociations = associationsResult.results.flatMap(
      (result) => result.to || []
    );
    console.log("Number of associated deals found:", allAssociations.length);
    
    if (allAssociations.length > 0) {
      console.log("Found associated deals, checking for any association labels...");
      console.log("Raw associations data:", JSON.stringify(allAssociations, null, 2));
      for (const association of allAssociations) {
        const otherDealId = association.toObjectId;
        console.log("Checking associated deal ID:", otherDealId);
        // continue processing using otherDealId...
      }
    }
    callback({ outputFields: { success: true } });
  } catch (error) {
    console.error("Error fetching associations:", error);
    callback({ outputFields: { errorMessage: error.message, success: false } });
  }
};



View solution in original post

7 Replies 7
CChunn24
Participant

@BBaber  What a huge help. I still need to fold into the call, but this absolutely provided the labels I was looking for! Thanks!

0 Upvotes
BBaber
Contributor

@v3rnalequinox  which endpoint are you using and can you send a screenshot of the error you are seeing?

You should be able to see them by using the basic list endpoint
ObjectType - Deal
ObjectId - recordId of the deal that is associated to multiple deals
toObjectType - Deal

cURL request should look like this:

curl --request GET \
--url 'https://api.hubapi.com/crm/v4/objects/Deal/34355736403/associations/Deal?limit=500' \
--header 'authorization: Bearer YOUR_ACCESS_TOKEN'

 
I would love to hear your use case! I've spent the last month detangling the Associations API 😀

CChunn24
Participant

Hello @BBaber  I'm running into something similar! I'm trying to find a specific association label for deal to deal, and using the API to see if that label is present. What I'm getting back is the type deal to deal, but not the label I'm looking for. Honestly, I'd be satisified with the internal Association ID. 

the call is

for (const association of allAssociations) {
const otherDealId = association.id;
console.log("Checking associated deal ID:", otherDealId);
console.log("Raw association object:", JSON.stringify(association));

 

But I'm getting back

INFO ***** Fetching deals associated with deal ID ***** xxxx00

INFO Associated deals API response received

INFO Number of associated deals found: 1

INFO Found associated deals, checking for any association labels...

 INFO Raw associations data: [{"id":"xxx8849","type":"deal_to_deal"}]

 

I'm trying to call the association.id but no dice. 

Thoughts?

0 Upvotes
BBaber
Contributor
can you share a snippet of the call you are making that finds the associations? This part only shows me how you are parsing the information captured in the call.
CChunn24
Participant

Hello! 
This is the part for the association label. 

try {
console.log("***** Fetching deals associated with deal ID *****", dealId);
const associationsResult =
await hubspotClient.crm.associations.batchApi.read("deals", "deals", {
inputs: [{ id: dealId }],
});

console.log("Associated deals API response received");

const allAssociations = associationsResult.results.flatMap(
(association) => association.to || []
);

console.log("Number of associated deals found:", allAssociations.length);

if (allAssociations.length > 0) {
console.log(
"Found associated deals, checking for any association labels..."
);
console.log("Raw associations data:", JSON.stringify(allAssociations));

for (const association of allAssociations) {
const otherDealId = association.id;

clearly trying to grab the info via console logs since it isn't connecting it for me.

0 Upvotes
BBaber
Solution
Contributor

I was getting the same log when I tested on my end. I believe its because the batchApi endpoint only gives generic info and not detailed information. 

Did this and was able to get the label and typeId. Let me know if this works for you. 

 

try {
  console.log("***** Fetching deals associated with deal ID *****", dealId);
  const associationsResult = await hubspotClient.crm.associations.v4.basicApi.getPage(
    "DEAL",
    dealId,
    "DEAL",
    undefined,
    500
  );
  console.log("Associated deals API response received");

  const allAssociations = associationsResult.results;
  console.log("Number of associated deals found:", allAssociations.length);

  if (allAssociations.length > 0) {
    console.log("Found associated deals, checking for any association labels...");
    console.log("Raw associations data:", JSON.stringify(allAssociations));
    for (const association of allAssociations) {
      const otherDealId = association.toObjectId;
      console.log("Checking associated deal ID:", otherDealId);
      if (association.associationTypes && association.associationTypes.length > 0) {
        for (const assocType of association.associationTypes) {
          console.log("Association details for deal " + otherDealId + ": Label: " + assocType.label + ", Type ID: " + assocType.typeId);
        }
      } else {
        console.log("No detailed association types available for deal " + otherDealId + ".");
      }
    }
  }
} catch (error) {
  console.error("Error fetching associations:", error);
}

 


Edit:
Upon looking further the issue is the way you were looking at the response body. 
The otherDealId = association.id should be otherDealId = association.toObjectId

Also, not including getPage, didn't return the full response but only what you were seeing before. 

const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({ accessToken: process.env.TOKEN });

exports.main = async (event, callback) => {
  try {
    const dealId = event.object.objectId;
    console.log("***** Fetching deals associated with deal ID *****", dealId);
    
    const BatchInputPublicFetchAssociationsBatchRequest = { inputs: [{ id: dealId }] };
    const fromObjectType = "Deal";
    const toObjectType = "Deal";
    
    const associationsResult = await hubspotClient.crm.associations.v4.batchApi.getPage(
      fromObjectType,
      toObjectType,
      BatchInputPublicFetchAssociationsBatchRequest
    );
    console.log("Associated deals API response received");
    
    const allAssociations = associationsResult.results.flatMap(
      (result) => result.to || []
    );
    console.log("Number of associated deals found:", allAssociations.length);
    
    if (allAssociations.length > 0) {
      console.log("Found associated deals, checking for any association labels...");
      console.log("Raw associations data:", JSON.stringify(allAssociations, null, 2));
      for (const association of allAssociations) {
        const otherDealId = association.toObjectId;
        console.log("Checking associated deal ID:", otherDealId);
        // continue processing using otherDealId...
      }
    }
    callback({ outputFields: { success: true } });
  } catch (error) {
    console.error("Error fetching associations:", error);
    callback({ outputFields: { errorMessage: error.message, success: false } });
  }
};



v3rnalequinox
Participant

Cannot thank you enough @BBaber - Your solution nailed it, and we are off! You rock