Skip to main content

Notifications

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

Retrieve the latest date from items in an each loop

(0) ShareShare
ReportReport
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,058 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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,817 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans