Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

Foreach loop not working in CodeBlocks

Posted on by 115

We have a requirement where we need to list all Preference Lists that the contact receiving the email has been subscribed to, so we can dynamically show or hide different content blocks related to those preferences within the email.

As suggested here https://docs.microsoft.com/en-us/dynamics365/marketing/dynamic-email-content and here https://community.dynamics.com/365/marketing/b/dynamics-365-for-marketing-community-blogs/posts/add-dynamic-content-to-marketing-emails-and-show-event-sessions, the only way to do that is to insert a Code Block element in the marketing email and provide with these code lines:

{{#each contact.listmember_list_contact}}
{{#if (eq this.listid '"   blockListId   "')}}"
contentBlockHTML
"{{/if}}
{{/each}}

However this is not working at all. Even if we simplified to this: 

    {{#each contact.listmember_list_contact}}
  • ID: {{this.listid}}
  • {{/each}}

Nothing appears, only blank space. 

What are we doing wrong?

Best regards

  • RE: Foreach loop not working in CodeBlocks

    Hi Roy - unfortunately nothing at this point. My team is beginning to look into this further so hopefully an update/solution is on the way.

    J

  • Dsouza_28 Profile Picture
    Dsouza_28 387 on at
    RE: Foreach loop not working in CodeBlocks

    Hello James DC - Marketing ,

    I am currently exploring dynamics email content in the similar lines as you mentioned. Where you able to figure out a solution?

    I'd appreciate your response.

  • RE: Foreach loop not working in CodeBlocks

    Hi there,

    So I am trying to solve this exact issue currently; building an email that displays content blocks based off subscription/marketing lists.

    For example, say a contact is in subscription lists 'Cars' and 'Boats', but not 'Bikes' - I'd like to display the content block for 'Cars' and the contact block for 'Boats'.

    What I have so far is using:

    {{#if (eq contact.listmember_list_contact Cars)}}

    ...not sure how to link in the contact block for Cars...

    {{else if (eq contact.listmember_list_contact Boats)}}

    ...not sure how to link in the contact block for Boats...

    {{else}}

    ...fallback content block

    {{/if}}

    Any help on this would be amazing.

    Thanks in advance!

  • Manuesev Profile Picture
    Manuesev 115 on at
    RE: Foreach loop not working in CodeBlocks

    Thanks for answer

    We are using the N:N relationship between Contact and Marketing List: 

    pastedimage1613724245066v1.png

  • Verified answer
    Nya Profile Picture
    Nya 29,056 on at
    RE: Foreach loop not working in CodeBlocks

    Hi, Manuel

    Does the Preference Lists you mentioned refer to the Marketing Lists?

    If so, it’s an N: N relationship between Marketing List and Contact while the example you referred to is an N: 1.

     8546.png

    Unfortunately, N: N relationship has not been supported in Code Block.

    For this type of relationship is not linked by the specified field, it is not possible to find the corresponding list by emailing the identified contact.

     

    You can find the description about N: N relationship in the documentation you mentioned:

    Add dynamic content to marketing emails (Dynamics 365 Marketing) | Microsoft Docs

     6710.png

    And here is a related issue:

    many to many relation - Dynamics 365 Marketing Forum Community Forum

     

    If this helped you, I'd appreciate it if you'd mark this as a Verified Answer, which may in turn help others as well.

    Best Regards,

    Nya

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans