crm.associations.v4.basicApi.create Node.Js VALIDATION_ERROR

Howdy, peeps!
I’m newer to the HubSpot dev world and trying to figure out where I’m going awry with my create association API. This is the script that’s in my workflow. It finds a match between two properties from different custom objects and it is supposed to associate the two together. My custom object settings have both objects associated, I found the property association type ID and am using “USER_DEFINED” for association category. It parses and matches the data just fine preor to the association phase.

Note:
• I’m currently running through bash for extensive testing before I place it back into it’s HubSpot workflow.
• sampleOrderObject, textilesObject,keys,home folder, and cookies were removed for security and yadda yadda. I can vouch that the sampleOrderObject, textilesObject ID numbers are correct wihtin the script.

Script:

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

// Initialize HubSpot API client
const hubspotClient = new hubspot.Client({ accessToken: process.env.MY_HUBSPOT_TOKEN });

// Check if running in development mode
const isTest = process.env.NODE_ENV === 'development';

async function updateAssociationMatch(event) {
 try {
 console.log('Starting updateAssociationMatch function');

 const sampleOrderObject = "[SAMPLE_ORDER_OBJECT]"; // Design Sample Order
 const textilesObject = "[SAMPLE_ORDER_OBJECT]"; // Design Sample Textiles

 const objectId = isTest ? process.env.TEST_OBJECT_ID : event.object.objectId;
 console.log('Sample Order Object ID:', objectId);

 if (!objectId) {
 throw new Error('objectId is null or undefined');
 }

 // Get properties from the current Design Sample Order record
 const enrolledRecordResponse = await hubspotClient.crm.objects.basicApi.getById(
 sampleOrderObject,
 objectId,
 ['association_match', 'textile_order']
 );

 const associationMatchRaw = isTest
 ? process.env.TEST_ASSOCIATION_MATCH
 : enrolledRecordResponse.properties['association_match'];

 const textileOrderValues = enrolledRecordResponse.properties['textile_order'] || '';
 const textileOrderOptions = textileOrderValues.split(';').map(val => val.trim());

 const textileNames = associationMatchRaw
 ? associationMatchRaw.split(',').map(name => name.trim()).filter(name => name.length > 0)
 : [];

 console.log(`Textile names extracted: ${textileNames.join(', ')}`);
 console.log(`Current textile_order options: ${textileOrderOptions.join(', ')}`);

 for (const textileName of textileNames) {
 const cleanedTextileName = textileName.trim();

 if (textileOrderOptions.includes(cleanedTextileName)) {
 console.log(`Matched textile_order value: ${cleanedTextileName}`);

 // Find the matching textile record by association_match
 const searchResponse = await hubspotClient.crm.objects.searchApi.doSearch(textilesObject, {
 properties: ['id', 'association_match'],
 limit: 1,
 filters: [
 {
 propertyName: 'association_match',
 operator: 'EQ',
 value: cleanedTextileName
 }
 ]
 });

 const match = searchResponse.results[0];

 if (match && match.id) {
 const textileId = match.id;
 console.log(`Associating with textile record ID: ${textileId}`);

 try {
 console.log(`Creating association from Sample Order ID ${objectId} to Textile ID ${textileId}`);

 await hubspotClient.crm.associations.v4.basicApi.create(
 sampleOrderObject,
 objectId,
 textilesObject,
 textileId,
 [
 {
 associationCategory: "USER_DEFINED",
 associationTypeId: 152 // Replace with your actual type ID
 }
 ]
 );

 console.log(`Successfully associated sample order ${objectId} with textile ${textileId}`);
 } catch (assocError) {
 console.error(`Association failed for textile ID ${textileId}`, assocError);
 }
 } else {
 console.warn(`No textile record found for: ${cleanedTextileName}`);
 }
 } else {
 console.log(`No match found in textile_order for: "${cleanedTextileName}"`);
 }
 }
 } catch (error) {
 console.error('Error updating association_match:', error);
 }
}

exports.main = updateAssociationMatch;

Last error log:

Running in local test mode...
 Starting updateAssociationMatch function
 Sample Order Object ID: 26440519494
 Textile names extracted: Elan 3111-001 Black, Brisa 3802-044 Indigo, Cadence 3506-902 Bay
 Current textile_order options: Elan 3111-001 Black, Brisa 3802-044 Indigo, Cadence 3506-902 Bay
 Matched textile_order value: Elan 3111-001 Black
 Associating with textile record ID: 25347121531
Creating association from Sample Order ID 26440519494 to Textile ID 25347121531
 Association failed for textile ID 25347121531 ApiException [Error]: HTTP-Code: 400
Message: An error occurred.
Body: {"status":"error","message":"One or more associations are invalid","correlationId":"232abacf-2a28-41d6-9413-b441f3d85863","context":{"INVALID_OBJECT_IDS":["[TEXTILE OBJECT ID]=26440519494 is not valid"],"objectId":["26440519494"],"objectType":["[TEXTILE OBJECT ID]"]},"category":"VALIDATION_ERROR"}
Headers: {"access-control-allow-credentials":"false","cf-cache-status":"DYNAMIC","cf-ray":"935224555d9482ff-IAD","connection":"keep-alive","content-length":"280","content-type":"application/json;charset=utf-8","date":"Thu, 24 Apr 2025 02:24:01 GMT","nel":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","report-to":"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=YVZgq8n1KdKqWBtY1sInphiJsgR2OUCkonvucWNdIIX4PoTEOiILJw7ON0Elsfq89XmvkoHU2pTLtTfUaPSd%2FQVocSsIwBtHD2oDUriud98nYQ8IZ9P7inSEVmXNtuba\"}],\"group\":\"cf-nel\",\"max_age\":604800}","server":"cloudflare","strict-transport-security":"max-age=31536000; includeSubDomains; preload","vary":"origin, Accept-Encoding","x-content-type-options":"nosniff","x-hubspot-correlation-id":"232abacf-2a28-41d6-9413-b441f3d85863","x-hubspot-ratelimit-daily":"1000000","x-hubspot-ratelimit-daily-remaining":"998516","x-hubspot-ratelimit-interval-milliseconds":"10000","x-hubspot-ratelimit-max":"190","x-hubspot-ratelimit-remaining":"188","x-hubspot-ratelimit-secondly":"19","x-hubspot-ratelimit-secondly-remaining":"18"}
 at BasicApiResponseProcessor.<anonymous> (/Users/[USER NAME]/hubspot-test/node_modules/@hubspot/api-client/lib/codegen/crm/associations/v4/apis/BasicApi.js:199:23)
 at Generator.next (<anonymous>)
 at fulfilled (/Users/[USER NAME]/hubspot-test/node_modules/@hubspot/api-client/lib/codegen/crm/associations/v4/apis/BasicApi.js:5:58)
 at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
 code: 400,
 body: {
 status: 'error',
 message: 'One or more associations are invalid',
 correlationId: '232abacf-2a28-41d6-9413-b441f3d85863',
 context: {
 INVALID_OBJECT_IDS: [Array],
 objectId: [Array],
 objectType: [Array]
 },
 category: 'VALIDATION_ERROR'
 },
 headers: {
 'access-control-allow-credentials': 'false',
 'cf-cache-status': 'DYNAMIC',
 'cf-ray': '935224555d9482ff-IAD',
 connection: 'keep-alive',
 'content-length': '280',
 'content-type': 'application/json;charset=utf-8',
 date: 'Thu, 24 Apr 2025 02:24:01 GMT',
 nel: '{"success_fraction":0.01,"report_to":"cf-nel","max_age":604800}',
 'report-to': '{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=YVZgq8n1KdKqWBtY1sInphiJsgR2OUCkonvucWNdIIX4PoTEOiILJw7ON0Elsfq89XmvkoHU2pTLtTfUaPSd%2FQVocSsIwBtHD2oDUriud98nYQ8IZ9P7inSEVmXNtuba"}],"group":"cf-nel","max_age":604800}',
 server: 'cloudflare',
 'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
 vary: 'origin, Accept-Encoding',
 'x-content-type-options': 'nosniff',
 'x-hubspot-correlation-id': '232abacf-2a28-41d6-9413-b441f3d85863',
 'x-hubspot-ratelimit-daily': '1000000',
 'x-hubspot-ratelimit-daily-remaining': '998516',
 'x-hubspot-ratelimit-interval-milliseconds': '10000',
 'x-hubspot-ratelimit-max': '190',
 'x-hubspot-ratelimit-remaining': '188',
 'x-hubspot-ratelimit-secondly': '19',
 'x-hubspot-ratelimit-secondly-remaining': '18'
 }
}
 Matched textile_order value: Brisa 3802-044 Indigo
 Associating with textile record ID: 25347121531
Creating association from Sample Order ID 26440519494 to Textile ID 25347121531
 Association failed for textile ID 25347121531 ApiException [Error]: HTTP-Code: 400
Message: An error occurred.
Body: {"status":"error","message":"One or more associations are invalid","correlationId":"c7970f42-01ba-4e3e-8703-951438312ab6","context":{"INVALID_OBJECT_IDS":["[TEXTILE OBJECT ID]=26440519494 is not valid"],"objectId":["26440519494"],"objectType":["[TEXTILE OBJECT ID]"]},"category":"VALIDATION_ERROR"}
Headers: {"access-control-allow-credentials":"false","cf-cache-status":"DYNAMIC","cf-ray":"935224579f9782ff-IAD","connection":"keep-alive","content-length":"280","content-type":"application/json;charset=utf-8","date":"Thu, 24 Apr 2025 02:24:01 GMT","nel":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","report-to":"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=6ktl3EaGYzcENtKdpgL2RJKhmG038i7Z1KoMU%2BDAtAlm6x7uJOLJQAeY2VonCjPiPtYARg2j%2FY%2B2uRUgLNBfhNWHdUOqFrVAiknwvpREr2DBuLOBgxHPxcvI3D3yt5Qt\"}],\"group\":\"cf-nel\",\"max_age\":604800}","server":"cloudflare","strict-transport-security":"max-age=31536000; includeSubDomains; preload","vary":"origin, Accept-Encoding","x-content-type-options":"nosniff","x-hubspot-correlation-id":"c7970f42-01ba-4e3e-8703-951438312ab6","x-hubspot-ratelimit-daily":"1000000","x-hubspot-ratelimit-daily-remaining":"998514","x-hubspot-ratelimit-interval-milliseconds":"10000","x-hubspot-ratelimit-max":"190","x-hubspot-ratelimit-remaining":"187","x-hubspot-ratelimit-secondly":"19","x-hubspot-ratelimit-secondly-remaining":"18"}
 at BasicApiResponseProcessor.<anonymous> (/Users/[USER NAME]/hubspot-test/node_modules/@hubspot/api-client/lib/codegen/crm/associations/v4/apis/BasicApi.js:199:23)
 at Generator.next (<anonymous>)
 at fulfilled (/Users/[USER NAME]/hubspot-test/node_modules/@hubspot/api-client/lib/codegen/crm/associations/v4/apis/BasicApi.js:5:58)
 at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
 code: 400,
 body: {
 status: 'error',
 message: 'One or more associations are invalid',
 correlationId: 'c7970f42-01ba-4e3e-8703-951438312ab6',
 context: {
 INVALID_OBJECT_IDS: [Array],
 objectId: [Array],
 objectType: [Array]
 },
 category: 'VALIDATION_ERROR'
 },
 headers: {
 'access-control-allow-credentials': 'false',
 'cf-cache-status': 'DYNAMIC',
 'cf-ray': '935224579f9782ff-IAD',
 connection: 'keep-alive',
 'content-length': '280',
 'content-type': 'application/json;charset=utf-8',
 date: 'Thu, 24 Apr 2025 02:24:01 GMT',
 nel: '{"success_fraction":0.01,"report_to":"cf-nel","max_age":604800}',
 'report-to': '{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=6ktl3EaGYzcENtKdpgL2RJKhmG038i7Z1KoMU%2BDAtAlm6x7uJOLJQAeY2VonCjPiPtYARg2j%2FY%2B2uRUgLNBfhNWHdUOqFrVAiknwvpREr2DBuLOBgxHPxcvI3D3yt5Qt"}],"group":"cf-nel","max_age":604800}',
 server: 'cloudflare',
 'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
 vary: 'origin, Accept-Encoding',
 'x-content-type-options': 'nosniff',
 'x-hubspot-correlation-id': 'c7970f42-01ba-4e3e-8703-951438312ab6',
 'x-hubspot-ratelimit-daily': '1000000',
 'x-hubspot-ratelimit-daily-remaining': '998514',
 'x-hubspot-ratelimit-interval-milliseconds': '10000',
 'x-hubspot-ratelimit-max': '190',
 'x-hubspot-ratelimit-remaining': '187',
 'x-hubspot-ratelimit-secondly': '19',
 'x-hubspot-ratelimit-secondly-remaining': '18'
 }
}
 Matched textile_order value: Cadence 3506-902 Bay
 Associating with textile record ID: 25347121531
Creating association from Sample Order ID 26440519494 to Textile ID 25347121531
 Association failed for textile ID 25347121531 ApiException [Error]: HTTP-Code: 400
Message: An error occurred.
Body: {"status":"error","message":"One or more associations are invalid","correlationId":"4e3908da-0099-4773-8de3-e86b86f9929c","context":{"INVALID_OBJECT_IDS":["[TEXTILE OBJECT ID]=26440519494 is not valid"],"objectId":["26440519494"],"objectType":["[TEXTILE OBJECT ID]"]},"category":"VALIDATION_ERROR"}
Headers: {"access-control-allow-credentials":"false","cf-cache-status":"DYNAMIC","cf-ray":"93522459592782ff-IAD","connection":"keep-alive","content-length":"280","content-type":"application/json;charset=utf-8","date":"Thu, 24 Apr 2025 02:24:01 GMT","nel":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","report-to":"{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=xM6aYNhVsR5PPo62jDPu7CxjjBVMCKXr5pAfACelhbs9tLrfxgqhjBvB0RlzSr%2FaOKQupJh%2BDxfYJXawO6b4bYdADEPztSmeETTDSFptvS89kyBeZwJqRgBN69fWWd8p\"}],\"group\":\"cf-nel\",\"max_age\":604800}","server":"cloudflare","strict-transport-security":"max-age=31536000; includeSubDomains; preload","vary":"origin, Accept-Encoding","x-content-type-options":"nosniff","x-hubspot-correlation-id":"4e3908da-0099-4773-8de3-e86b86f9929c","x-hubspot-ratelimit-daily":"1000000","x-hubspot-ratelimit-daily-remaining":"998512","x-hubspot-ratelimit-interval-milliseconds":"10000","x-hubspot-ratelimit-max":"190","x-hubspot-ratelimit-remaining":"186","x-hubspot-ratelimit-secondly":"19","x-hubspot-ratelimit-secondly-remaining":"18"}
 at BasicApiResponseProcessor.<anonymous> (/Users/[USER NAME]/hubspot-test/node_modules/@hubspot/api-client/lib/codegen/crm/associations/v4/apis/BasicApi.js:199:23)
 at Generator.next (<anonymous>)
 at fulfilled (/Users/[USER NAME]/hubspot-test/node_modules/@hubspot/api-client/lib/codegen/crm/associations/v4/apis/BasicApi.js:5:58)
 at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
 code: 400,
 body: {
 status: 'error',
 message: 'One or more associations are invalid',
 correlationId: '4e3908da-0099-4773-8de3-e86b86f9929c',
 context: {
 INVALID_OBJECT_IDS: [Array],
 objectId: [Array],
 objectType: [Array]
 },
 category: 'VALIDATION_ERROR'
 },
 headers: {
 'access-control-allow-credentials': 'false',
 'cf-cache-status': 'DYNAMIC',
 'cf-ray': '93522459592782ff-IAD',
 connection: 'keep-alive',
 'content-length': '280',
 'content-type': 'application/json;charset=utf-8',
 date: 'Thu, 24 Apr 2025 02:24:01 GMT',
 nel: '{"success_fraction":0.01,"report_to":"cf-nel","max_age":604800}',
 'report-to': '{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=xM6aYNhVsR5PPo62jDPu7CxjjBVMCKXr5pAfACelhbs9tLrfxgqhjBvB0RlzSr%2FaOKQupJh%2BDxfYJXawO6b4bYdADEPztSmeETTDSFptvS89kyBeZwJqRgBN69fWWd8p"}],"group":"cf-nel","max_age":604800}',
 server: 'cloudflare',
 'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
 vary: 'origin, Accept-Encoding',
 'x-content-type-options': 'nosniff',
 'x-hubspot-correlation-id': '4e3908da-0099-4773-8de3-e86b86f9929c',
 'x-hubspot-ratelimit-daily': '1000000',
 'x-hubspot-ratelimit-daily-remaining': '998512',
 'x-hubspot-ratelimit-interval-milliseconds': '10000',
 'x-hubspot-ratelimit-max': '190',
 'x-hubspot-ratelimit-remaining': '186',
 'x-hubspot-ratelimit-secondly': '19',
 'x-hubspot-ratelimit-secondly-remaining': '18'
 }
}

I think the main information to consider is :

“context”:{“INVALID_OBJECT_IDS”:[“[TEXTILE OBJECT ID]=26440519494 is not valid”]. I know the custom object IDs are assigned correcty so I’m lost on next steps to resolve this validaiton issue.

Any hints, tips, or suggestions are greatly appreciated. Thanks!

My GUESS is that you’re using the wrong associationTypeId. There are two of these for each association and they are DIRECTIONAL.

Sample Object → Associates to → Textile Object

Textile Object → Associates to → Sample Object

They will have different IDs. You might be using the wrong one.

So a quick example:

Contact → Company

Company → Contact:

You can see the Association ID are different. If you view the Associations in Hubspot and view the API info for the association, you’ll get the above screen for ONE DIRECTION. Click the reverse:

And then view the API info and see the other direction. See which one you’re using and try the other one.

You were correct! My main issue was trying to associate 2 custom objects without association lables. It was significantly easier to track and manage IDs and direciton once I used labels to define their conneciton within HubSpot’s UI.
Thank you!