Working with Child Relationships

Salesforce supports master-detail relationships and we’ve given you the ability to use those relationships in WebMerge Mappings.

To use Child Relationships, you will need to create a loop in your document that supports iterating over a list of data. To do so:


{foreach from=$Contacts item=_contact}


The "_contact" part of this loop is the local variable that you'll use inside the loop.  To print out a field value, you use {$_contact.FIELD}, where FIELD is the Salesforce API Field Name for the sub field on that object (ie Name, Favorite_Color__c, etc).

*If you're not sure what the field names are called, turn on Debug Mode for your Document and after you run a test merge, you'll be able to see the field names.  You'll need to complete the next step first though!

In your Salesforce WebMerge Mapping you will now have the ability to map a Salesforce field or relationship to "Contacts". When working with child relationships, we’re going to ignore the _contact field in this situation because it is the iterator. Let’s instead look at Contacts and in the Salesforce Field section, find the relationship you want to map to the list.



In this example, we will use Child Relationship: Contacts. This will allow you to create a WebMerge generated document that iterates over all the child contacts attached to a Salesforce record and output their Name and Phone Number.

Create a Table

If you'd like to create a table in your document, you can do that use a "tablerow" tag instead of the "foreach" loop like this:

Name Phone Number
{tablerow from=$Contacts item=_contact}{$_contact.Name} {$_contact.Phone}{/tablerow}


Working with Grandchildren (Nested Loops)

If you need to loop through child records within your child relationships, Salesforce will send over a list of records indexed by the Id of your main child record.  You can do something like this in your document:

{foreach from=$Contacts item=_contact} 
{foreach from=$Dependents[$_contact.Id] item=_dep}
- {$_dep.Name}

You'll then need to update your Salesforce Mapping and map the "Dependents" field to "Child Relationship: Contacts.Dependents"


Example - Opportunity Line Items + Pricebook

If you'd like to create a table in your document of line items, you can do that use a "tablerow" tag instead of the "foreach" loop like this:

Name Product Code Unit Price Total Price
{tablerow from=$LineItems item=_row}{$_row.Name} ({$_row.Product2Id.Name}) {$_row.PricebookEntryId.ProductCode} {$_row.UnitPrice|number_format:2} {$_row.TotalPrice|number_format:2}{/tablerow}


Have more questions? Submit a request


  • Avatar
    Jane Isaac

    I do see that rows with the label Child Relationship: Log Time but none of the fields of the custom object are appearing in the list.

  • Avatar
    Jane Isaac

    In addition, when I am in the Field Mapping screen, the 5 fields from the table do not even appear on the left side of the screen as fields to be mapped.

  • Avatar
    Jeremy Clarke

    Hi Jane,

    The child relationship fields do not show up because you only need to map the 1 loop field from your table and that will send over all the fields from your child object, then in your table you use the API Field Names to print those fields. Please see the example table above :)

  • Avatar
    Vinod Kumar

    Hi Jeremy,

    1) Let's say i have two Objects A (Master) and Object B (Child) and on object B there is a field for Email. Now what i want is when Object A's status is completed then send out certificates to those email id's (Multiple mail id's) with respective Name (multiple names) and Date of Object B and I am uploading .pdf document template which has only Name and Date as input text not Email so how will it be done?

    2) I saw your post regarding iterating over child items but i want the certificates to be sent to each individual with their name on it and to their email id so how will it be done?

    Edited by Vinod Kumar
  • Avatar
    Jeremy Clarke

    Hi Vinod,

    1) You can create a new merge field in your email delivery by typing in something like {$Email}. That field doesn't need to be in your document

    2) You'll need to use Data Routing loop over the child records. So in your data route, you'll create a rule for your document, then you'll check the "Repeat this rule" box and enter something like {json_encode($Children)}.

    This will loop through each child record and merge a document. You'll need to make sure the merge fields you use in your template for the child fields, match the API Field Name from SF.

Please sign in to leave a comment.
Powered by Zendesk