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 %} 
{% } |(user)zbynekh|(hash)368858e782cff9ca9f47d7e671e9e7d4a1353491eb013a95ec0f82487be8027d%}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on January 12, 2018 00:28 (last edited on March 24, 2018 12:16)

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
{% } |(user)zbynekh|(hash)6baead3d1201365ba0390a9befd3d2e6a27b1e6412dcc7b8f95074d6ff1f6f6b%}
0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on January 12, 2018 18:53 (last edited on March 24, 2018 12:16)

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|(user)zbynekh|(hash)bfb5d325cb0d580446f0b5c8db6ae3140b6a08aca98b7a402f4e64f6df4f61d0%}
{% string.IsNullOrEmpty(FormField2)?  "" : "FormField2 is :" FormField2|(user)zbynekh|(hash)4145c87004cce0e1a0019e40a4d0f9d0cf3d7c8ce71ddcc5e944651a50cc1e5e%}
{% string.IsNullOrEmpty(FormField3)?  "" : "FormField3 is :" FormField3|(user)zbynekh|(hash)0055778fc95caf7ab2fe85a38335b2035356e6d0d85b3f906d902680894f7a72%}
1 votesVote for this answer Mark as a Correct answer

Marcus Humberg answered on January 12, 2018 19:01 (last edited on March 24, 2018 12:16)

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) { %}{% } |(user)zbynekh|(hash)f674fb01a54d69cba96336b752a45dacb4da3e1383163e18ecfec9e738a1131f%}

You can see the "#" that it keeps adding - and I'm not getting anything returned in the email whether the "EnterYourEmail" field is filled or not.

Then I tried:

{% if($$value:EnterYourEmail$$ != null) { %}{% } |(user)zbynekh|(hash)28b90824e814b3bb696e4ad39c9d5ff1ba6211ed46007f260c9916a130302d82%} 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 March 24, 2018 12:16)

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 |(user)zbynekh|(hash)58ca944d1cced3c4a2c95b02f50e4a6ce843cc676fd89bac1419b1bdf425e8e1%}

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 March 24, 2018 12:16)

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 %} {% } |(user)zbynekh|(hash)d6be779e45e786ae8d92ac72b36ecc996b2da7459bff758f4adedc4f287f7efa%}[/div]

[div]{% if(!string.IsNullOrWhiteSpace(EnterYourName)) { %} The Name is: {% EnterYourName %} {% } |(user)zbynekh|(hash)9235c767f9942d8600310bd5fc05962cb1e52ca4de3ecb9a4f752cb1e039f85c%}[/div]
[/body]

0 votesVote for this answer Mark as a Correct answer

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