How to send the notification with all the populated data in the requested item

Poorna Chandu
Tera Contributor

Hello Everyone,

I have a requirement where I need to send the notification via mail script with all the data in the requested item. Can anyone help me in achieving this.

9 REPLIES 9

Hi,

you can use this script and print all variables

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

	// Add your code here
	template.print('Variable Summary: <br/>');
	var variables = curent.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

@Poorna Chandu 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello Poorna,

I would request you to please try below script in email notification scripts to fetch al variable of catalog item:

template.print("<font style='font-size: 10pt; font-family: arial;'>");
template.print("Summary of options:<br />");

var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.sys_id);
item.query();
if(item.next())

{

template.print("<p style='font-size: 10pt; font-family: arial;'>");
        //template.print(item.number + " :  " + item.cat_item.getDisplayValue() + "<br />");
        template.print("<p style='font-size: 10pt; font-family: arial;'>Options:<br />");
        template.print("<br /></p>");
        var keys = [];
        var set = new GlideappVariablePoolQuestionSet();
        set.setRequestID(item.sys_id);
        set.load();
        var vs = set.getFlatQuestions();
        for (var i=0; i < vs.size(); i++) {
                template.space(2);
                template.print("<font style='font-size: 10pt; font-family: arial;'>");
                template.print(vs.get(i).getLabel() + ' ' +  '=' + ' ' + vs.get(i).getDisplayValue() );
                template.print("</font><br/>");
            }
}

 

Please mark it as right if it helps you.

BR,
Nayan

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Mohith Devatte
Tera Sage
Tera Sage

hello @Poorna Chandu ,

try this script in email script it worked for me 

var variableList = current.variables.getElements();
for(var i=0; i<variableList.length; ++i)
{
var variable = variableList[i];
if(variable.getLabel()!='')
{
template.print('<strong>'+variable.getLabel()+ ': </strong>'+variable.getDisplayValue()+'<br>');
}
}

PLEASE MARK MY ANSWER CORRECT IT IT HELPS YOU

ShubhamGarg
Kilo Sage

Hi Poorna,

After defining your email script, call it in your Email Notification > 'What it will contain' section using

${mail_script:email_script_name}

Mark this as Correct/Helpful if it helps.

Regards,

Shubham