How to create macro expressions to shorten form emails with many null fields

Marcus Humberg asked on January 11, 2018 23:00

I have a form that is quite lengthy - it contains a subsection where a user can choose from 24 different business types, each with 3-8 distinct questions asked, so roughly 100 total entries, but only an average of 4 answered questions out of those 100. The form itself is designed well for the users - they only see their 3-8 questions.

However, when I go to the email notification tab in the form editor to design the form submission email to my team, if I include all of the form labels and values in the auto-generated table, the response team has to wade through all of the NULL answers just to get the data they need. I am being asked to find a way to truncate that email so it only sends the user submitted data without the NULL fields.

How do I write a macro within the email notification so that if the user's response is NULL or not entered, the field is excluded in the generated email?

Logically, I'm thinking an argument something like this:

IF $$value:UserSubmission$$ = NULL (or some other empty form value, e.g., "select an option"), THEN do not display $$value:UserSubmission$$ in the email notification.

I'm not well enough versed in Kentico's macro syntax to know how this should be written. I searched the forum and didn't immediately see any similar questions.

Correct Answer

Trevor Fayas answered on January 12, 2018 19:23

Marcus,

Don't mind the #, that's Kentico adding a security hash, you may be able to replace it with @ but either way, it's not vital.

Next thing is i would use the IsNullOrWhiteSpace because the value may be an empty string, which isn't null. Try exactly as it's shown below.

{% if(!string.IsNullOrWhiteSpace(EnterYourEmail)) { %}
       The Email is: {% EnterYourEmail %} 
{% } |(identity)GlobalAdministrator%}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on January 12, 2018 00:28 (last edited on December 10, 2019 02:31)

The form values are also passed as Macros to the Email Engine. So you should be able to do:

{% if(UserSubmission != null || UserSubmission != "Some value") { %}
    The User Submission content here
{% } |(identity)GlobalAdministrator%}
0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on January 12, 2018 18:53 (last edited on December 10, 2019 02:31)

Macros should work like Trevor said https://docs.kentico.com/k10/managing-website-content/forms/using-macros-with-forms

In your e-mail notification you can use custom layout and do something like

{% string.IsNullOrEmpty(FormField1)?  "" : "FormField1 is :" FormField1|(identity)GlobalAdministrator%}
1 votesVote for this answer Mark as a Correct answer

Marcus Humberg answered on January 12, 2018 19:01 (last edited on December 10, 2019 02:31)

I'm trying to enter this as you have suggested, but I'm not getting anything returned in the email, and Kentico is insiting on entering a "#" into the code that I cannot eliminate - it's being auto-added even when I enter it in the source code editor.

First I tried:

{% if(EnterYourEmail != null) { %}{% } |(identity)GlobalAdministrator%} with the same result.

0 votesVote for this answer Mark as a Correct answer

Marcus Humberg answered on January 12, 2018 19:41

Thanks Trevor! That one worked. I'll keep testing this on my big form. The only thing I'm seeing now is it looks like it adds an empty line in the email if the field is blank. My test form is only two questions long so I'll add a few more lines and keep whacking at it.

0 votesVote for this answer Mark as a Correct answer

Marcus Humberg answered on January 12, 2018 20:12

Further testing is showing that yes the fields that are not entered are not showing in the email, but Kentico is putting a blank line for every entry that is being blanked out by the macro. Is there any way to avoid this? Everywhere I've labeled [empty line] is an actual blank line sent in the emailed form.

Entry 1: (everything)

The Email is: marcus.humberg@junk
The Name is: Marcus Humberg
The Letter is: A
The Number is: 1
The Button is: Option 2
The Box is: True
The Options are: Option 2|Option 3

Entry 2: (partial form)

[empty line]
The Name is: Marcus Humberg
The Letter is: A
[empty line]
The Button is: Option 2
The Box is: False
The Options are: Option 2|Option 3

Entry 3: (partial form)

The Email is: marcus.humberg@junk
[empty line]
The Letter is: A
[empty line]
[empty line]
The Box is: True
[empty line]

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on January 12, 2018 20:40 (last edited on December 10, 2019 02:31)

I'm guessing you are doing Text vs. HTML. In which case, you can try to condense the macro into a single line using a Ternary operator like Peter put:

{% string.IsNullOrEmpty(EnterYourEmail) ?  "" : "Enter your email value is"+EnterYourEmail |(identity)GlobalAdministrator%}

Ternary statements pretty much are: The true/false condition ? True result : false results

0 votesVote for this answer Mark as a Correct answer

Marcus Humberg answered on January 12, 2018 21:14 (last edited on December 10, 2019 02:31)

Okay so I had to insert CSS in the email and make each line a div, and used the CSS to hide the empty divs. I'm using square brackets so the HTML doesn't parse. Thank you so much for all of your help!

[head]
[style type="text/css"].div:empty {display: none;}
[/style]
[/head]
[body]
[div]{% if(!string.IsNullOrWhiteSpace(EnterYourEmail)) { %} The Email is: {% EnterYourEmail %} {% } |(identity)GlobalAdministrator%}[/div]
[/body]

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.