Skip to main content

Notifications

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

Retrieve the latest date from items in an each loop

Posted on by 10

Hi! 

This issue regards HTML when designing a marketing email.

I am trying to get a startdate from an entity called "Membership" by using an #each loop. 

I prefer c# but sometimes these tasks appear as well. What I would've done in c# is basically like this (code not tested):


DateTime startDate = new DateTime();
DateTime endDate = new DateTime();

foreach(var membership in memberships)
{
    if(membership.startdate > startDate)
    {
        startDate = membership.startDate;
        endDate = membership.endDate;
    }
}


And then print out the variables.
Doing the same in html doesn't seem to be as easy.
Below code is insufficient, and I don't know how to use variables like this.

 

var startDate = ?;
var endDate = ?;

{{#each contact.membership_id}}
    {{#if (gt this.startDate startDate)}}
        startDate = this.startDate;
        endDate = this.endDate;
    {{/if}}
{{/each}}

Member from:{startDate}

Member to:{endDate}

Please help me understand if and how this is do-able.

Another thought I had was to make a list with all the startdates and enddates (a list<datetime, datetime>-ish) and then choose the max datetime among all entries. But I have no clue on how to do this either. 

Best regards,
Matilda

  • Elisa del valle Profile Picture
    Elisa del valle 10 on at
    RE: Retrieve the latest date from items in an each loop

    I have the same question,  could we use variables en [tag:each] loop

  • Matilda Olsson Profile Picture
    Matilda Olsson 10 on at
    RE: Retrieve the latest date from items in an each loop

    Yeah, we thought of solutions including a new field, a parameter to the workflow or customer journey, but we are unfortunately not allowed to do any such changes.

    I understood it the same way, that C# was not apply-able to the HTML. But there are no other ways in HTML to use variables or lists the way I want then? If not then I will just have to accept defeat.

    Thank you for the fast answer!

    Best regards,

    Matilda

  • Verified answer
    Nya Profile Picture
    Nya 29,056 on at
    RE: Retrieve the latest date from items in an each loop

    Hi Matilda,

    The C# code is not supported in the code block of Marketing Emails.

    It is recommended to create a custom field in your entity, which can record the latest date with something like workflow. Then insert the field as a dynamic content into the email, so that the latest date can be shown.

     

    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