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

@SAS21 

Glad to know that my script worked.

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

@SAS21 

I didn't get why not to use email script.

what are you trying to do using this and where are you doing this?

body=body
.replace(/##RESOLUTIONSECTION#/g, resolutionSection);

Share the complete script

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

@SAS21 

Since you mentioned that my script worked, please mark my response as correct and close the thread.

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

Viraj Hudlikar
Giga Sage

Hello @SAS21 -

Email script something as below:

(function runEmailScript(current, template, email, email_action, email_config) {
    // Initialize an array to hold non-empty field content
    var resolutionContent = [];
    
    // Define the fields to check along with their labels
    var fields = [
        { label: 'Describe action taken', value: current.u_action_taken},
        { label: 'Describe action to be taken', value: current.u_planned_action},
        { label: 'Date of Completion', value: current.u_date_of_completion.getDisplayValue() },
        { label: 'Target Date', value: current.u_target_date.getDisplayValue() }
    ];
    
    // Iterate through each field to check for non-empty values
    fields.forEach(function(field) {
        if (field.value.trim() !== '') {
            resolutionContent.push(field.label + ': ' + field.value);
        }
    });
    
    // If there are non-empty fields, construct and print the Resolution Section
    if (resolutionContent.length > 0) {
        var sectionHeader = '<b>Resolution Section:</b>';
        var sectionBody = resolutionContent.join('<br/>');
        template.print(sectionHeader + '<br/>' + sectionBody + '<br/><br/>');
    }
})(current, template, email, email_action, email_config);

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hello @SAS21 

 

Did my reply help you with your concern raised? 

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

Thanks & Regards
Viraj Hudlikar.