Is there a way to show the variable and variable set both in the body of the email notification?

Koyel Guha
Tera Contributor

Hi All, 

I am having an issue with the email notification. Whenever I am trying to create a request, it doesn't show the REQ details in Additional information for few of the catalog items.

For some of the catalog items it does give the additional information of the REQ. On the other hand for few of the items it doesn't.

Here is the email script which is attached with the notification . 

template.print("<p></p>Requested items:\n");

var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sys_id);
gr.query();
while (gr.next()) {
var stage = gr.stage.getDisplayValue();
if (JSUtil.nil(stage))
stage = gr.stage.getChoiceValue();
template.print(gr.number + ": " + gr.cat_item.getDisplayValue() + ", Stage: " + stage + "\n");

template.print(" \n ");

template.print(" More details:\n");

for (key in gr.variables) {

var v = gr.variables[key];

if (v.getGlideObject().getQuestion().getLabel() != '') {

if (v.getDisplayValue() != '') {

template.space(4);

template.print(' ' + v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");
}

}

}


}

 

Please help me with this.

 

KInd Regards,

Koyel Guha

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Koyel Guha 

this should work fine to print all the variables be it within or outside variable set

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

	// Add your code here

	template.print("<p></p>Requested items:\n");

	var ritm = new GlideRecord('sc_req_item');
	ritm.get('request', current.sys_id);

	var variables = ritm.variables.getElements(); 
	for (var i=0;i<variables.length;i++) { 
		var question = variables[i].getQuestion();
		var label = question.getLabel();
		var value = question.getDisplayValue();
		if(label != ''){
			template.space(4);
			template.print('  ' + label + " = " + value + "<br/>");
		}
	} 

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

Regards
Ankur

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

View solution in original post

9 REPLIES 9

@Koyel Guha 

Glad to help.

Please remember to mark response helpful as well.

Regards
Ankur

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

Hello @Ankur Bawiskar ,

I have one question on the above code :

This code is giving all the variables associated with the catalog item. However, it also shows the variables with no value. I only need the variables to be displayed which has the value in it.

find_real_file.png     

Please check the below example which needs to be shown: 

find_real_file.png

Can you please tell how to restrict the variables which are not filled.

 

Thanks in Advance.

Kind Regards,

Koyel

Hi,

currently not possible.

The email script is server side and won't know which variables were visible or hidden on the Client side UI

Regards
Ankur

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

Hi,

I actually do that using EVG_SSI_EmailFormatter ... Here is my code. You would have to setup the formatter first.  You can grab the update for the formatter here, https://jeffbenedictblog.wordpress.com/2014/07/16/servicenow-email-layouts-new-version-1-1/

 

var thef = new EVG_SSI_EmailFormatter('CatalogDefault');
var requestURL = gs.getProperty('glide.servlet.uri') + "nav_to.do?uri=" + GlideStringUtil.urlEncode(gs.generateURL(current.getTableName(), current.sys_id));
//var sSubject = current.number + ": "  + current.short_description + " ("  + current.state.getDisplayValue()+ ") " ;
var sSubject = current.number;

var bodyContent = "";

/* Content */
//Top table information

bodyContent = "<table style=\"width:600px;border:1px solid " + thef.BORDER_COLOR + ";padding:4px;\"><tbody>";
bodyContent += "<tr><td valign=\"top\" width=\"125\"><strong>Task:</strong></td><td valign=\"top\">" + current.number + "</td></tr>";
bodyContent += "<tr><td valign=\"top\" width=\"125\"><strong>Summary:</strong></td><td valign=\"top\">" + current.short_description + "</td></tr>";
bodyContent += "<tr><td valign=\"top\"><strong>Status: </strong></td><td valign=\"top\">" + current.state.getDisplayValue() + "</td></tr>";
bodyContent += "<tr><td valign=\"top\"><strong>Assignment Group: </strong></td><td valign=\"top\">" + current.assignment_group.getDisplayValue() + "</td></tr>";
bodyContent += "</tbody></table>";

//Details information
var sDescription = current.description.getDisplayValue();
bodyContent += "<strong>Request Details</strong>";

/* Build Updates into Email Body */
var notes = current.comments.getJournalEntry(-1); //gets all journal entries as a string where each entry is delimited by '\n\n'
var na = notes.split("\n\n");
var notesHTML = "";
if (na.length > 0) {
    notesHTML += "<b>" + current.number + " Comments:</b>";
}

for (var k = 0; k < na.length; k++) {
    var aNoteParts = na[k].split('\n');
    for (var x = 0; x < aNoteParts.length; x++) {
        var noteEntry = JSUtil.escapeText(aNoteParts[x]);
        if (noteEntry.indexOf('(Additional comments)') > -1) {
            notesHTML += "<div style=\"color: #1c3664;font-weight: bold;\">" + noteEntry + "</div>";
        } else {
            notesHTML += "<div>" + noteEntry + "</div>";
        }
    }
    notesHTML += "<div style=\"height: 15px;\">&nbsp;</div>";
}

/* Build Table of Variable Information to put in the Body */
var sTableRows = "";
var rowInfo = {};
var iCounter = 0;
//	bodyContent += "<table style=\"WIDTH: 100%;border:1px solid "+thef.BORDER_COLOR+";padding:4px;\"><tbody>";

rowInfo.LABEL1 = "Status:  ";
rowInfo.VALUE1 = current.state.getDisplayValue();
sTableRows += thef.getSection('table-row', rowInfo);
rowInfo = {};
rowInfo.LABEL1 = "Approval Status:  ";
rowInfo.VALUE1 = current.approval.getDisplayValue();
sTableRows += thef.getSection('table-row', rowInfo);
rowInfo = {};
rowInfo.LABEL1 = "Assignment Group:  ";
rowInfo.VALUE1 = current.assignment_group.getDisplayValue();
sTableRows += thef.getSection('table-row', rowInfo);
rowInfo = {};
rowInfo.LABEL1 = "Assignment To:  ";
rowInfo.VALUE1 = current.assigned_to.getDisplayValue();
sTableRows += thef.getSection('table-row', rowInfo);
rowInfo = {};
rowInfo.LABEL1 = "Opened By:  ";
rowInfo.VALUE1 = current.opened_by.u_displayname;
sTableRows += thef.getSection('table-row', rowInfo);
rowInfo = {};
rowInfo.LABEL1 = "Opened On:  ";
rowInfo.VALUE1 = current.opened_at.getDisplayValue();
sTableRows += thef.getSection('table-row', rowInfo);
rowInfo = {};


var varown = new GlideRecord('sc_item_option_mtom');
varown.addQuery('request_item', current.request_item);
varown.orderBy('sc_item_option.order');
varown.query();
while (varown.next()) {
    //	var visible = varown.sc_item_option.item_option_new.visible_summary;
    var question = GlideappAbstractChoiceListQuestion.getQuestion(varown.sc_item_option.item_option_new);

    question.setValue(varown.sc_item_option.value);


    if (question.getLabel() != "" && question.getDisplayValue() != "" && question.getDisplayValue() != 'false' && question.getDisplayValue() != '-- None --') {


        rowInfo.LABEL1 = question.getLabel();
        rowInfo.VALUE1 = question.getDisplayValue();
        sTableRows += thef.getSection('table-row', rowInfo);
        rowInfo = {};
    }
}

rowInfo.LABEL1 = "Closed";
rowInfo.VALUE1 = current.closed_at.getDisplayValue();
sTableRows += thef.getSection('table-row', rowInfo);
rowInfo = {};

rowInfo.LABEL1 = "Closed Note";
rowInfo.VALUE1 = current.close_notes.getDisplayValue();
sTableRows += thef.getSection('table-row', rowInfo);
rowInfo = {};

bodyContent += thef.getSection('table-body', {
    'HEADER': 'Details',
    'TABLE_BODY': sTableRows
});
bodyContent += "<br/>";


var sLatestComments = thef.formatArray(current.comments.getJournalEntry(1));

if (sLatestComments != "") {
    bodyContent += "<strong>Most Recent Comment</strong>";
    bodyContent += "<table style=\"WIDTH:600px;border:1px solid " + thef.BORDER_COLOR + ";padding:4px;\"><tbody><tr style=\"HEIGHT: 25pt;\"><td valign=\"top\">";
    bodyContent += "<p>" + sLatestComments + "</p>";
    bodyContent += "</td></tr></tbody></table><br/>";
}



/* Put it all together into a single HTML body */
var aContentParms = {
    'SUBJECT': sSubject,
    'TICKET_LINK': requestURL,
    'TICKET_LINK_TEXT': 'Click here to view the record',
    'BODY_CONTENT': bodyContent
};
var sHTML = thef.getSection('content', aContentParms);
sHTML += thef.getSection('footer');

email.setBody(sHTML);
//email.setSubject(sSubject);

 

Hope this helps.

June

Kevin
Tera Expert

Ankur,

I want to show only a couple of the RITM variables ion my email.

Could you help me with a script to call first_name, last_name, job_title?

 

Thank you!