Adding RITM Variables in Approval Mail Notification

Arun87
Giga Contributor

Hi,

I have one catalog item named as xyz which contains workflow name as yz. When user raised a request it will be go for the user's manager approval. In the approval email few fields are need to visible. 

Approval email is sending for sysapprover table. Could anyone please help on this.

1 ACCEPTED SOLUTION

Hi,

I didn't get.

Since user has filled 10 variables those 10 will come in email.

Why to display only 5 variables? are you saying you want to keep specific variables only and exclude some 5?

if yes then try this

ensure you give valid variable names in the if condition

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

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

    // Add your code here

    template.print('Summary of requested item: <br/>');

    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('sys_id', current.sysapproval);
    ritm.query();
    if(ritm.next()){
        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();
            var name = question.getName();

            if(name != 'variable1' && name != 'variable2' && name != 'variable3' && name != 'variable4' && name != 'variable5'){
            if(label != '' && value != ''){
                template.space(4);
                template.print('  ' + label + " = " + value + "<br/>");
            }
            }
        }
    }

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

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

Regards
Ankur

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

View solution in original post

14 REPLIES 14

sachin_namjoshi
Kilo Patron
Kilo Patron

Use below script to get requested item variables.

 

<mail_script>  


template.print("Summary of Requested item:\n");    


var gr = new GlideRecord("sc_req_item");  


gr.addQuery("sys_id", current.sysapproval);  


gr.query();  


while(gr.next()) {


template.print(gr.number + ":   " + gr.quantity + " X " + gr.cat_item.getDisplayValue() + "\n");  


template.print("       Options:\n");  


var varown = new GlideRecord('sc_item_option_mtom');  


varown.addQuery("request_item", current.sysapproval);


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() != "" && visible == true){  


template.space(4);  


template.print('         ' +   question.getLabel() + " = " + question.getDisplayValue() + "\n");  


    }  


    }  


    }  


</mail_script>    

 

Regards,

Sachin

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Arun,

Can you try this

Email script:

Name: my_info

Script:

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

	// Add your code here

	template.print('Summary of requested item: <br/>');

	var ritm = new GlideRecord('sc_req_item');
	ritm.addQuery('sys_id', current.sysapproval);
	ritm.query();
	if(ritm.next()){
		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);

Call mail script in notification body of Approval table notification in format

${mail_script:my_info}

Regards
Ankur

 

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

Hi Ankur,

I am able to get all the RITM details in email. But i want to get only the selected variables in the email. Could you please help on this.

Hi,

Thanks for informing that my script is working.

So you are saying you want only those variables which are having value and not to have the ones with empty values

then update as below

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

    // Add your code here

    template.print('Summary of requested item: <br/>');

    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('sys_id', current.sysapproval);
    ritm.query();
    if(ritm.next()){
        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 != '' && value != ''){
                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

Hi Ankur,

It is working for the fields which are filled with values. My requirement is I have 10 variables in  RITM but i want to display only 5 variables in the approval email. Could you please help me on this same.