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}
    {$_contact.Name}
    {$_contact.Phone}
{/foreach}

 

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.

 

salesforce_child_relationship.png

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} 
{$_contact.Name}
{foreach from=$Dependents[$_contact.Id] item=_dep}
- {$_dep.Name}
{/foreach}
{/foreach}


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

20 Comments

  • Avatar
    Robby Johnson

    so what would the Webmerge Mappings in SF look like for the child relationship fields above? thanks

  • Avatar
    Jeremy Clarke

    Hi Robby,

    In Salesforce, you could see a field for "Contacts" and then you would map that field to "Child Relationship: Contacts"

  • Avatar
    Shawn Thornley

    Is there any way to make this work with Lookup relationships? For example having {$Contacts.0.Manager__r.Name} or similar on a fully custom object. I have tried and it doesn't appear to output anything. {$Contacts.0.Name} however does work no problem.

  • Avatar
    Jeremy Clarke

    Hi Shawn,

    Please turn on Debug Mode (https://support.webmerge.me/hc/en-us/articles/206526126-Debug-Mode) and you'll be able to see the data we're receiving from SF and the correct subfields to use for the referenced objects.

    If you have any troubles, please create a support ticket and we'll help you from there.

    Thanks!

  • Avatar
    Steve Jones

    Is it possible to go down multiple layers? E.g. Account -> Opportunities -> OpportunityLineItems.

  • Avatar
    Jeremy Clarke

    Hi Steve,

    Yes! There should be an option in your mapping settings called "Child Relationship: Opportunities.OpportunityLineItems" --- this will send over a list of the line items grouped by opp.

    Here's an example loop

    {foreach from=$LineItems item=_items key=_oppId}
    {foreach from=$_items item=_item}
    {$_item.Name} - {$_item.Total}
    {/foreach}
    {/foreach}

  • Avatar
    Matthew Souther

    I'd like to display a section of my page only if there are child records meeting certain criteria. For example, I might have an invoice that includes one table for goods (OpportunityLineItems) and another table with different columns for services (also OpportunityLineItems). If a given invoice doesn't include any services, rather than displaying my services table with just a header row, I want to hide the whole table. What kind of syntax would that require?

  • Avatar
    Jeremy Clarke

    Hi Matthew,

    The easiest route is to split your line items array into 2 separate arrays then you can use logic on those arrays to show/hide the tables. You can add the code to your template like this:

    {foreach from=$lines item=_line}{if $_line.Type__c == "Goods"}{$_goods[] = $_line}{elseif $_line.Type__c == "Services"}{$_services[] = $_line}{/if}{/foreach}

    Then you can add logic around your tables:

    {if !empty($_goods)}
    GOODS TABLE
    {/if}

    {if !empty($_services)}
    SERVICES TABLE
    {/if}

  • Avatar
    Matthew Souther

    Brilliant! Thanks. :-)

  • Avatar
    Andrew Toews

    Is there a syntax to specify a filter on which child records to include in the merge? I am using a custom object called Attribution__c that is a child of Opportunities and I only want to include Attribution records where Attribution__c.Attribution_Role__c = "Co-Borrower".

    Edited by Andrew Toews
  • Avatar
    Andrew Toews

    Another question...I am creating a merge document template from the Opportunity object and have no trouble referencing fields from the parent Account. Now I would also like to reference data from all the child contacts associated to that parent account. Can this be done without users being required to establish Opportunity Contact Roles?

  • Avatar
    Jeremy Clarke

    Hi Andrew,

    For your first question, you can use an "if" statement inside your loop to skip the roles you don't want like this:

    {foreach from=$Attributions item=_attribution}{if $_attribution.Attribution_Role__c != "Co-Borrower"}{continue}{/if}
    Name: {$_attribution.Name}
    {/foreach}

    (if you're using a {tablerow}, then you'll use {tableif $attribution.Attribution_Role__c != "Co-Borrower"} instead)

    ------
    For your 2nd question, you'll setup a loop inside your document for the contacts, then in your Mapping, you should see something like "Account: Contacts" in the Salesforce field dropdown and you'll match that up with your merge field.

    If you have any more questions, please create a ticket and we'll help you from there!

    Thanks!

  • Avatar
    Kanchan Singh

    I have something like this , and I need exactly array index value instead of key .

    {foreach from=$data.application.CUSTOMER item=_row key=index}

    Name {$_index}) : {$_row.CUSTOMER.NAME} 

     
    Edited by Kanchan Singh
  • Avatar
    Jeremy Clarke

    Hi Kanchan,

    Since you have "key=index", you'll need to use {$index} like this:

    Name {$index}) : {$_row.CUSTOMER.NAME}

  • Avatar
    Sugandha Arora

    Hii Jeremy!
    I have mapped a child (custom) object from accounts. In the child object, I have a look up field to another custom object. I need the value in the lookup field. Can you tell me what's the syntax?
    Thank you!

  • Avatar
    Jeremy Clarke

    Hi Sugandha,

    Sometimes we can pull in that related object, so please turn on Debug Mode (https://support.webmerge.me/hc/en-us/articles/206526126-Debug-Mode) and look for that field and it will tell you what merge field to use.

    If you don't see it, you'll need to create formula field on your child object that references the field on the other custom object.

    Thanks!

  • Avatar
    Stephanie Byng

    What would this look like if you are using a fillable PDF to sync directly to salesforce upon clicking a submit button? Where would I put this loop information?

  • Avatar
    Jeremy Clarke

    Hi Stephanie,

    There isn't a way to "loop" inside a fillable PDF, but you can use the Field Map (https://support.webmerge.me/hc/en-us/articles/206526086-Field-Map) to set the value of each field you have in the PDF using the static field name for each child relationship like this:

    Product1_Name:
    {$products.0.Field_Name__c}

    Product2_Name:
    {$products.1.Field_Name__c}

    Product3_Name:
    {$products.2.Field_Name__c}

    You can find these field names by turning on Debug Mode (https://support.webmerge.me/hc/en-us/articles/206526126-Debug-Mode) and running a test merge. Then click on the field names in the View Data popup and you'll see the merge field codes to use in the Field Map.

    Thanks!

  • Avatar
    Stephanie Byng

    Thanks for the response Jeremy. I'm not sure where I'd put that mapping. I have a fillable PDF that I want to sync to salesforce. I want to create one master object record, and 10+ child records under that master. When I go into the salesforce mappings, none of my child object fields show up in the list. Would I set the value you mention in your comment on the webmerge side of things?

  • Avatar
    Jeremy Clarke

    Hi Stephanie,

    Yes, the code I sent you will go into the Field Map which is inside WebMerge. Then inside Salesforce in your WebMerge Mapping page, you'll choose the "Child Relationship" from the dropdown next to "products" (or whatever you call the merge field within your Field Map).

    You will only see 1 field show up in your WebMerge Mapping. This will send over all the child objects, then inside your Field Map, that's where you reference the individual fields.

    Thanks!

Please sign in to leave a comment.
Powered by Zendesk