CMS Development

JHolshuijsen
Member

Creating batch of associations gives Invalid Input JSON error

Trying to create a batch of associations with the following function:

 

await hubspot.crm.associations.batchApi.create('deal', 'contact', {inputs: contact_associations_links});

 

gives the following error:

"Invalid input JSON on line 1, col

umn 58: Cannot build PublicAssociation, some of required attributes are not set [from]"

When printing the value of the contact_association_links list of objects, I get this: 

 

[
  {
    from: { id: '8052979611' },
    to: { id: '161251' },
    type: 'deal_to_contact'
  }
]

 

This seems to correspond with what is said in the documentation of the API regarding creation of a batch of associations. One of the reasons I can think of this happens is beause the "from" key is set in the following way: 

 

contact_associations_links = contact_associations_links.map(function(input){ input.from  = {id: new_deal_id}; return input;})

 

and the "to" and "type" keys are set like this:

  contact_associations_links.push({"from": {id:''}, "to": {id:contact_id}, "type": 'deal_to_contact'})

However, I have also tried setting the "from" key in the following ways, which gives the same error:

 for(var i = 0; i<contact_associations_links.length; ++i){
        contact_associations_links[i].from = {id:new_deal_id}
      }

 for(var i = 0; i<contact_associations_links.length; ++i){
        contact_associations_links[i]["from"] = {id:new_deal_id}
      }

contact_associations_links = contact_associations_links.map(function(input){ input["from"]  = {id: new_deal_id}; return input;})

I'm really not sure how to solve this issue

0 Upvotes
3 Replies 3
louischausse
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

Creating batch of associations gives Invalid Input JSON error

Do you have an ID for every from line in your JSON?

Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link
JHolshuijsen
Member

Creating batch of associations gives Invalid Input JSON error

I've been trying to get to the root of the problem. I created the following code: 

const Hubspot = require('@hubspot/api-client');
        const hubspot = new Hubspot.Client({ apiKey: 'API-KEY'});

        var contact_associations_ = [{"from": {id: '8178324514'}, "to": {id:'550601'}, "type": 'deal_to_contact'}]     
        var inputs = {inputs: contact_associations_}
        if(!contact_associations_.filter(input => !input.from.id)) {
            throw new Error(`from.id missing in input: ${JSON.stringify(input)}`);
        } 
        var response = await hubspot.crm.associations.batchApi.create('deal', 'contact', {inputs: contact_associations_});
   

 The strange thing is that this code works fine and creates the association in Hubspot when I execute it directly from Visual Studio Code. However, running it from Pipedream or Localhost even, it gives the attributes not set error. 

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Creating batch of associations gives Invalid Input JSON error

@louischausse , any chance you can lend a hand here?

0 Upvotes