Adding catalog item variables to email notification

David36
Tera Contributor

Hello,

We recently started using Variable Sets and I would like to include all variables in the approval email notification.

Currently no variables are populating in the approval requests. I have been investigating this and find different solutions that dont quite fit my situation. I discovered that I can pull certain variables by dot walking the variable. The only example is the "Requested for " field where i was able to add  ${sysapproval.variables.legal_first_name} ${sysapproval.variables.legal_last_name}  to accurately display the right information.

I am new with scripting and have not found an effective means of adding all the variables of a request regardless of the variable set that is in use. We are on the London Version and as much detail on where to update the script would be very appreciated.

Below is the script that is currently in use request.itil.approve.role

Short Description: ${sysapproval.short_description}
Priority: ${sysapproval.priority}
Requested For: ${sysapproval.u_item_requested_for}
Requested By: ${sysapproval.opened_by}
Total Price: ${sysapproval.price}
<hr/>
<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 = Packages.com.glideapp.questionset.Question.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>
<mail_script>
var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", current.sysapproval);
gr.query();
while(gr.next()) {
var nicePrice = gr.price.toString();
if (nicePrice != '') {
nicePrice = parseFloat(nicePrice);
nicePrice = nicePrice.toFixed(2);
}
template.print(gr.number + ": " + gr.quantity + " X " + gr.cat_item.getDisplayValue() + " at $" + nicePrice + " each \n");
template.print(" Options:\n");
for (key in gr.variables) {
var v = gr.variables[key];
if(v.getGlideObject().getQuestion().getLabel() != '') {
template.space(4);
template.print(' ' + v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");
}
}
}
</mail_script>
${sysapproval.description}
<hr/>
${mailto:mailto.approval}
<hr/>
${mailto:mailto.rejection}
<hr/>
Click here to log into ServiceNow to view Approval Request: ${URI_REF}
Click here to log into ServiceNow to view ${sysapproval.sys_class_name}: ${sysapproval.URI_REF}

 

Approval requests are currently being sent out with no information as shown below.

find_real_file.png

4 REPLIES 4

SanjivMeher
Kilo Patron
Kilo Patron

I think instead of 'Packages.com.glideapp.questionset.Question', you should use GlideappQuestion in below line

 

var question = Packages.com.glideapp.questionset.Question.getQuestion(varown.sc_item_option.item_option_new); 
question.setValue(varown.sc_item_option.value);


Please mark this response as correct or helpful if it assisted you with your question.

Sanjiv,

This update was only able to add the quick summary and RITM but none of the other variables

Summary of Requested item:
RITM0018579: 1 X User Profile Change Request
Options:

Derek C
Tera Guru

We recently went through this exact thing. I found this community post very helpful.

https://community.servicenow.com/community?id=community_question&sys_id=a4540369dbd8dbc01dcaf3231f96...

Essentially what we found is that you can use a mail script to get the questions / values of the variables using GlideappVariablePoolQuestionSet(). Then call the mail script off of a notification on the approval table.

something like this -

var variableSet = GlideappVariablePoolQuestionSet();

variableSet.setRequestID(current.sysapproval.getValue().toString());

variableSet.load();

variableSet.getFlatQuestions();

for (var i=0; i< variableSet.size(); i++) {

var label = variableSet.get(i).getLabel();
var data = variableSet.get(i).getDisplayValue();


//only add variables that have a label and data
if(label != '' && data != '') {
variableData += '<strong>' + label + '</strong>' + '<br>' + data + '<br><br>' ;
}
}

template.print(variableData);

 

One suggestion is; if you think you'll ever re-use this code, consider making it in a script include. That's what we ended up doing so it can be easily used in other scripts. Hope this helps!

 

 

David36
Tera Contributor

Derek,

 

Where specifically on the template could I add the suggested script?

Also if I were to make this a script include what parameters should it be set to?