how to print/hide the fields in the email notification

SAS21
Tera Guru

Have a section in the notification template 'Resolution Section' as shown below. Under that it has 4 fields 

 

Resolution Section:

Describe action taken: ##u_action_taken##
Describe action to be taken : ##u_planned_action##
Date of Completion: ##u_date_of_completion##
Target Date : ##u_target_date## 

 

In the triggered email, have to print only those fields whichever has data under the Section. If all fields are blank dont display the 'Resolution Section' at all in  the triggered email notification. How do we achieve this via email script ?

 

Appreciate the help!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@SAS21 

email script is the only way.

Create email script and include in email body using this syntax

${mail_script:scriptName}

try this script -> ensure you give correct field names here

(function runMailScript(current, template, email, email_action, event) {

    var actionTaken = current.u_action_taken;
    var plannedAction = current.u_planned_action;
    var dateOfCompletion = current.u_date_of_completion;
    var targetDate = current.u_target_date;

    var resolutionSection = '';
    if (actionTaken || plannedAction || dateOfCompletion || targetDate) {
        resolutionSection += '<b>Resolution Section:</b><br>';
        if (actionTaken) {
            resolutionSection += 'Describe action taken: ' + actionTaken + '<br>';
        }
        if (plannedAction) {
            resolutionSection += 'Describe action to be taken: ' + plannedAction + '<br>';
        }
        if (dateOfCompletion) {
            resolutionSection += 'Date of Completion: ' + dateOfCompletion + '<br>';
        }
        if (targetDate) {
            resolutionSection += 'Target Date: ' + targetDate + '<br>';
        }
    }

    // Return the resolution section to be included in the email
    template.print(resolutionSection);

})(current, template, email, email_action, event);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ayushi12
Mega Sage

Hi @SAS21 
You can use email script to check the field condition and print the fields lable and value accordingly.You can try something like below:

 

 

if (current.fieldname) {
        // The Field value is not null
} else {
        // The Field value is null
}

 

 

If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

Thank You!

Runjay Patel
Giga Sage

Hi @SAS21 ,

 

You can use below script in email your email script.

    var action_taken = current.u_action_taken;
    var planned_action = current.u_planned_action;
    var date_of_completion = current.u_date_of_completion;
    var target_date = current.u_target_date;

    var output = "";

    if (!gs.nil(action_taken) || !gs.nil(planned_action) || !gs.nil(date_of_completion) || !gs.nil(target_date)) {
        output += "<b>Resolution Section:</b><br>";

        if (!gs.nil(action_taken)) {
            output += "<b>Describe action taken:</b> " + action_taken + "<br>";
        }
        if (!gs.nil(planned_action)) {
            output += "<b>Describe action to be taken:</b> " + planned_action + "<br>";
        }
        if (!gs.nil(date_of_completion)) {
            output += "<b>Date of Completion:</b> " + date_of_completion + "<br>";
        }
        if (!gs.nil(target_date)) {
            output += "<b>Target Date:</b> " + target_date + "<br>";
        }
    }
 template.print(output);
   

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

Ankur Bawiskar
Tera Patron
Tera Patron

@SAS21 

email script is the only way.

Create email script and include in email body using this syntax

${mail_script:scriptName}

try this script -> ensure you give correct field names here

(function runMailScript(current, template, email, email_action, event) {

    var actionTaken = current.u_action_taken;
    var plannedAction = current.u_planned_action;
    var dateOfCompletion = current.u_date_of_completion;
    var targetDate = current.u_target_date;

    var resolutionSection = '';
    if (actionTaken || plannedAction || dateOfCompletion || targetDate) {
        resolutionSection += '<b>Resolution Section:</b><br>';
        if (actionTaken) {
            resolutionSection += 'Describe action taken: ' + actionTaken + '<br>';
        }
        if (plannedAction) {
            resolutionSection += 'Describe action to be taken: ' + plannedAction + '<br>';
        }
        if (dateOfCompletion) {
            resolutionSection += 'Date of Completion: ' + dateOfCompletion + '<br>';
        }
        if (targetDate) {
            resolutionSection += 'Target Date: ' + targetDate + '<br>';
        }
    }

    // Return the resolution section to be included in the email
    template.print(resolutionSection);

})(current, template, email, email_action, event);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  Hi Ankur, thank you for the help. the above is working . But i am trying to use ##RESOLUTIONSECTION# in the notification template. Instead of printing in the email script i am trying to use this 

body=body

.replace(/##RESOLUTIONSECTION#/g, resolutionSection);  but its not working though. Am i missing something? 
Thank you