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

sahusanjeep95
Giga Expert

 

If your Notification is on RITM table then you can create a below email script and can able to print the variable in notification body. use ${mail_script:"name_of_email_script"} in the message body.

getVariables();


function getVariables () {
	
	template.print('<tr><th>SAP Request Summary</th><td>');

	var varown = new GlideRecord('sc_item_option_mtom');  
	varown.addQuery("request_item", current.sys_id);  
	varown.orderBy('sc_item_option.order');
	varown.query();  
	while (varown.next()){  
		
  var question = new GlideappQuestion.getQuestion(varown.sc_item_option.item_option_new);  
		question.setValue(varown.sc_item_option.value);
		
     var label = question.getLabel();
		if(label == null || label == undefined || label == "")
			continue;
		
     var display_value = question.getDisplayValue();
	if(display_value == null || display_value == undefined || display_value == "")
			continue;
		
		template.print(question.getLabel() + ": " + question.getDisplayValue() + "<br/>"); 	 
	}
	template.print('</td></tr>');
}

 

Please mark answer as correct if this works for you.

Thanks,

Sanjeep

 

 

The notification is on Request Table. Also, its only not working with few catalog items. Other catalog items are showing the required information.

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

Thank You @Ankur Bawiskar for your help. It is working now.