DocMergy allows you to pull data from associated HubSpot objects dynamically directly in your template
To dynamically get data from associated HubSpot objects, you must create a Merge function. A Merge function with the below arguments can be used:
get_associated_objects(from_object_type, from_object_id, to_object_type, order_by)
- First argument [from_object_type]
- This is the source object of the association
- Most of the time this is going to be the object type of the workflow that triggered the Docmergy Document (ex: contact)
- Second argument [from_object_id]
- The HubSpot ID of the object of the type of the first argument
- Tip: you can get the current object ID by using object.hs_object_id
- Third argument [to_object_type]
- The object type you want to get the objects from
- ex: deal
- Fourth argument [order_by]
- This argument defines the order in which the associated objects are returned
- If not filled the default will be by object creation date in ascending order(oldest first)
- ex: createdate will order by the date the objects create date in HubSpot, by default ordering is in ASCENDING order.
- you can overwrite the ordering by specifying the field and ordering in brackets
- ex: ('createdate', 'DESCENDING') would look like the following in a complete template:
-
{{ get_associated_objects("contact",object.hs_object_id,"company", ("createdate", "DESCENDING"))|first|attr('name') }}
This function returns a list of Objects that can be used directly or in a loop
Example of getting the name of the associated company with a contact
In this scenario, we assume that only one company is associated with a contact or that if there are multiple, the first one is the relevant one.
This template is triggered from a contact workflow
{{ get_associated_objects("contact",object.hs_object_id,"company")|first|attr('name') }}
- Argument is the source object: contact in our case
- Argument is the source object id: object.hs_object_id
- Argument is the related object type: company
This function returns a list of objects. Applying the |first filter we can get the first object then using the |attr file with name as an argument is the property we want to display
Example of table displaying all line items on a deal
In this scenario, we assume that multiple line items are associated with a deal
The above table would be in the Word/Doc/Presentation template
Any row above the {% for ... %} won't be touched by DocMergy and is usually your table header
{% for line_item in get_associated_objects("deal",object.hs_object_id,"line_items") %}
The second row is the definition of the loop that will iterate on what the get_associated_objects returns. We define a variable called line_item (this can be anything you want) that will hold the current record in the loop. This row will be removed when the document is rendered, it is usually more readable to merge all columns of this row but it is not required.
The last row represents the current object that we are iterating on. In the same way we access properties on "object." we can now use "line_item." or any name that you have used above in the for loop.
- Cell is the SKU of the object
- The description of the object
- This is a more advanced syntax using python "fstrings"
- The quantity of the line item