Batch create custom objects with associations

Hi!

I could make it work to associate 2 custom objects (Activations and License) via api using the url “https://api.hubapi.com/crm/v3/objects/activations/123/associations/license/456/activations_to_license”.

The next step was to batch create Activations and I could successfully achieve that. However, now I need to batch create Activations, but now with License associations, so I can create the Activations and do the association in the same api request to HubSpot.

Checking HubSpot doc, I found out that for batch create, the associations section is like this:

It says I need the “associationTypeId”, which make things a little tricky to me.

Does it mean that everytime that I will need to batch create Activations, I will need to first get the schema for the Activations so I can know the associationTypeId?? It doesn’t make sense to me to be honest…
Why not just using the association label just like it’s used in the PUT call to associate 2 custom objects?
Thanks!

Hey, @AndreMiranda :waving_hand: Thanks for reaching out!

You’re correct that the batch API requires the associationTypeId.

The associationTypeId is defined in the schema of the custom object, and it’s a unique identifier for the type of association. You don’t need to get the schema every time you want to create a batch of activations. Once you’ve obtained the associationTypeId for the Activations to Licenses association, you can hard-code this value into your application. You can get the ID here — Retrieve association types

I have a few questions to help the community assist you effectively:

  • Are you encountering any errors when attempting to batch create with associations?
  • If so, could you please share the error message with us?

Best,

Jaycee

Hi Jaycee Lewis,

I do experience an error while batch creating objects and associations at the same time. It works in the HubSpot developer tools, It doesn’t works in a custom coded action --> meaning: it does create the object, but it doesn’t create the associations.

Would be very much appriciated if you could help me with this…

Best,

Marieke

Hi @Jaycee_Lewis
Yeah, I ended up doing a GetSchema 1st and then I could use the associationTypeId in the batch creation. I didn’t want to hard-coded it since I am testing against a HubSpot sandbox and then this would go to Production when it’s done and probably the associationTypeId would be another one in our HubSpot production. For now, it worked, but if could be the case, we can end up hard-coded it :slightly_smiling_face:

Thanks!

Hi AndreMiranda,

Did you make the associations work? I experience issue with this code in a custom coded action in a workflow. Perhaps you also did this in a workflow?

Hi @MWWesterterp ,

Yes, I did a GetSchema for the custom object (C# code):

var response = await Client.GetAsync($"schemas/{objectType}");

After that, I did the batch create like this:

var schema = new HubSpotActivationSchema();
        var hubSpotSchemaResponse = await GetSchema(schema.Repository);

        var msg = new
        {
            inputs = requests
                .Select(request =>
                    new
                    {
                        properties = new
                        {
                            key = request.Key,
                            product = request.Product
                        },
                        associations = new[]
                        {
                            new
                            {
                                to = new
                                {
                                    id = request.HubSpotLicenseId
                                },
                                types = new[]
                                {
                                    new
                                    {
                                        associationCategory = HubSpotAssociationCategory.UserDefined,
                                        associationTypeId = hubSpotSchemaResponse.Associations.FirstOrDefault(a => a.Name == schema.AssociationToLicense)?.Id
                                    }
                                }
                            }
                        }
                    }).ToArray()
        };

        var response = await Client.PostJsonAsync($"objects/{schema.Repository}/batch/create", msg);

In one of the custom object, I have an association labeled as “activation_to_license” which is represented by the part schema.AssociationToLicense in the code above.

So, GetSchema will bring all schema for the object and I look for an association with the name “activation_to_license” and get its associationTypeId. By doing this, you can create an object and associate it with another object at the same time. But, of course, you will need to know the id of this other object, which is represented in the code above by request.HubSpotLicenseId (request is just a DTO arriving as argument to this method).

Hope this helps! :slightly_smiling_face:

Hi AndreMiranda, thanks for your help. I really appriciate that. I did indeed find out the associationTypeId. I have all the ID’s correct, It does work in the hubspot developer tools, it doesn’t work in a custom coded action, though. Do you have any thoughts on that?

Hi @MWWesterterp ,

Hmmm.. that’s weird. To be honest, I have already faced the opposite situation you are having… in some cases, I was trying to test something using the developer page and it wasn’t working… but I moved forward anyway and test it in my C# code worked and tbh I don’t know why. Unfortunately, in your case, I don’t know the reason the object is being created but the association is not happening.