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).

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}
Have more questions? Submit a request

14 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}

Please sign in to leave a comment.
Powered by Zendesk