Need to query all custom objects associated to a contact and pull a single property from each one to add to a contact.
So we have custom objects called “Buildings” and a given contact can have multiple “Buildings” Associated. I need to know if i can use a workflow to query all associated “Buildings”, not just most recently updated, and pull a single property, “Building ID” to be able to add it to a property on the contact record.
If this cannot be done via regular workflow does anyone know by chance if a custom coded api workflow can achieve this?
Hey @JErickson60, welcome to our Community!
To query all custom objects (like “Buildings”) associated with a contact and pull a single property (such as “Building ID”) to add to a contact’s record, you can’t do this directly with HubSpot’s native workflows.
However, you can use custom coded workflow actions. Here’s a brief overview:
- Create a custom coded action in your workflow.
- Use HubSpot’s API to get all custom objects associated with the contact and extract the “Building ID” from each.
- Update the contact record with the collected “Building IDs”.
To our top experts, @DanielSanchez and @Anton do you have any recommendations for @JErickson60 matter?
Thank you,
Pam
Hey @JErickson60
To start @PamCotton 's ideas are the right direction, to paint a little more context:
You can use the graphql API to get all buildings w/ the building_id property associate to a contact in a workflow:
query MyQuery {
CRM {
contact(uniqueIdentifier: "hs_object_id", uniqueIdentifierValue: "44139400962") {
associations {
p_buildings_collection__contact_to_buildings {
items {
building_id
}
}
}
}
}
}
This query is requesting all buildings w/ their building_ids associated to contact with a record ID of “44139400962”.
The response will look somethign like:
{
"data": {
"CRM": {
"contact": {
"associations": {
"p_buildings_collection__contact_to_buildings": {
"items": [
{
"building_id": "6565"
},
{
"building_id": "34232"
}
]
}
}
}
}
}
}
You can then loop throught the buildings to output a list of the bulding IDs:
const buildingIds = response.data.CRM.Contact.Associations.p_buildings_collection__contact_to_buildings.items.map(obj => obj.building_id);
Finnaly this can then be output from the custom code action and applied to the contact record property.
Now if you only want a single building_id you’ll need additional logic to determine which building_id is the correct ID.
Hope this gets you going!
If I can clarify or help further in any way please don’t hesitate to reach out!
hey there @JErickson60 you can do this with a custom code action via the API:
1. use this endpoint to retrieve all of the associated Buildings of a the enrolled Contact (by Record ID) Accounts Dashboard | HubSpot.
2. use this endpoint to read the Building ID property value from the specified Building by including its record ID. Endpoint: /crm/v3/objects/{objectType}/{objectId}
Docs: Accounts Dashboard | HubSpot
3. use this endpoint to update (append the Building ID) the contact’s property for storing the associated Building ID(s).
Endpoint: PATCH/crm/v3/objects/contacts/{contactId}
Docs: Accounts Dashboard | HubSpot
Here’s documentation on how to use Custom Code Actions in HubSpot: