web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Advanced Custom code If and else setting in a marketing email

(0) ShareShare
ReportReport
Posted on by 235

Hi, 

I would like to send an email to a group of customer. One group should get One email and the other the other

My email contains 2 block

The field I am trying to get is a 1:N relationship from contact. 

Kind regards

Camilla

I have the same question (0)
  • cloflyMao Profile Picture
    25,210 on at

    Hi Camilla,

    As per my understanding, you should first iterate over all related membership of contact person, conditional statement will be nested in each loop.

    e.g: I created a custom entity "Membership" and 3 demo records and changed my publisher prefix to cgi as yours. 

    pastedimage1590044637025v2.png

    Dynamic expression in marketing email:

    pastedimage1590044674858v3.png

    As doc stated, operator(eq) is not required for boolean field.

    pastedimage1590044916805v5.png

    So the conditional statement will be: {{#if  this.cgi_roadassistance }}, it is equivalent to true,

    and the whole expression is:

    Hi {{contact.firstname}}, 
    
    {{#each contact.cgi_membership_contact_new_contact}}
    
        {{#if this.cgi_roadassistance }}
        
            {{this.cgi_name}}, Road assitance is available.
        
        {{else}}
        
            {{this.cgi_name}}, Road assitance is not available.
        
        {{/if}}
    
    {{/each}}

    Preview result:

    pastedimage1590045048574v6.png

    To apply it to your requirement, you might need to edit HTML code directly to add image element.(e.g: <img src="xxxx" width="100" height="100">)

    Just add the element inside {{#if}}{{/if}} body.

    Regards,

    Clofly

  • Camilla Larsson Profile Picture
    235 on at

    Hi Clofly,

    Thank you for your reply.

    But I do not want to show fields on the body from the field roadassistance.

    I want to send one email with 2 different contens.

    Kind Regards

    Camilla

  • cloflyMao Profile Picture
    25,210 on at

    Hi Camilla,

    I just show fields of related entity(roadassistance) with demo purpose.

    You could remove field expression from my demo code to send email with 2 different contents likes below:

    {{#each contact.cgi_membership_contact_new_contact}}

       {{#if this.cgi_roadassistance }}

           content A

       {{else}}

           content B

      {{/if}}

    {{/each}}

    Firstly set text content by editor:

    pastedimage1590399195327v1.png

    Then replace text with custom HTML elements.

    pastedimage1590399345088v2.png

    You could format HTML source code by website below:

    https://webformatter.com/html 

    Regards,

    Clofly

  • Camilla Larsson Profile Picture
    235 on at

    Hi Clofly,

    Thanks for your reply.

    Can I also use this expression to send different modules in the email?

    So the emails shows different type of modules and text?

    KR

    Camilla

  • cloflyMao Profile Picture
    25,210 on at

    Hi Camilla,

    What the meaning of "modules"?

    As per my understanding, you could use this expression to show two different blocks,

    because it is based on two options field.

    You could display other text or modules by changing to another two options field of related entity.

    Regards,

    Clofly

  • Camilla Larsson Profile Picture
    235 on at

    Hi again.

    I am getting an error when I am trying to set up this expression.

    "Dynamic content contains a for-each loop that uses a single-value relationship as a multi-value relationship:

    Camilla

  • Verified answer
    cloflyMao Profile Picture
    25,210 on at

    Hi Camilla,

    Could you confirm that relationship between Contact and Membership is 1:N?

    Because the error indicates that we applied "for each" loop to a N:1 relationship.(and I reproduced your error in my environment.)

    Please check whether Membership is actually a lookup field of Contact entity,

    if yes, we just need to use expression below without foreach because its N:1 relationship(Contact <-> Membership):

    {{#if  contact.contact_cgi_membership_cgi_currentmembershipid.cgi_roadassistance }}   

            Content A

         {{else}}

            Content B

    {{/if}}     

    Regards,

    Clofly

  • Camilla Larsson Profile Picture
    235 on at

    Thank you so much Clofly for answering me.

    This expression had no errors :D So apperantly this was a N:1.

    But there are no data showing in the preview? Veihjelp is not what I have add.. But under it it should show something. 

  • cloflyMao Profile Picture
    25,210 on at

    It is based on value of cgi_roadassistance field.

    Could you share your dynamic expression?

    {{#if contact.xxx}} expression is equal to true.

    e.g: If cgi_roadassistance of related membership is yes, then your expression would be

    {{#if  contact.contact_cgi_membership_cgi_currentmembershipid.cgi_roadassistance }}  

           Veihjelp 1

        {{else}}

          Veihjelp 2

    {{/if}}    

    Regards,

    Clofly

  • Camilla Larsson Profile Picture
    235 on at

    The dynamic expression is Yes/No..

    But now you lost me again.. This is not easy.. How should I write this?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 93 Super User 2025 Season 2

#2
TAHER_El_Mehdi Profile Picture

TAHER_El_Mehdi 30

#3
Satyam Prakash Profile Picture

Satyam Prakash 24

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans