- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 12:41 AM
Hello community! I am new here and would like to say hello and would like to thank you all in advanced with all the help and tips you provided to the community. I haved used majority of it to get me this far in my custom workflow for my projects onboarding process. Lets get right into it.
So my question is how to grab the first and last name of a request and pass that through to populate it in the subject line of the request for new user onboarding. Here is what I have so far.
====================================
I have created an onboarding ESS catalog item with the following variables:
*Single Line Text
> question = 'Enter the new user's first name'
> name = 'first_name'
*Single Line Text
> question = 'Enter the new user's last name'
> name = 'last_name'
====================================
I entered the following into the OOB request.itil.approve.role Email template:
var vs = set.getFlatQuestions();
for (var i=0; i < vs.size(); i++) {
if(vs.get(i).getLabel() != '') {
template.space(4);
template.print(' ' + vs.get(i).getLabel() + " = " + vs.get(i).getDisplayValue() + "\n");
}
///////////// added lines here
if(vs.get(i).getLabel() == 'First Name') {
var firstName = vs.get(i).getDisplayValue();
}
if(vs.get(i).getLabel() == 'Last Name') {
var lastName = vs.get(i).getDisplayValue();
}
if(vs.get(i).getLabel() == 'first_name') {
var firstName = vs.get(i).getDisplayValue();
}
if(vs.get(i).getLabel() == 'last_name') {
var lastName = vs.get(i).getDisplayValue();
}
if(vs.get(i).getLabel() == 'Enter the new user\'s first name') {
var firstName = vs.get(i).getDisplayValue();
}
if(vs.get(i).getLabel() == 'Enter the new user\'s first name') {
var lastName = vs.get(i).getDisplayValue();
}
//////////
====================================
I then attached the script ${mail_script:approval_information} to the "Request Opened on Behalf" email notification message body. The script is the following:
var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", current.sysapproval);
gr.query();
while(gr.next()) {
template.print(" Options:\n");
for (key in gr.variables) {
var v = gr.variables[key];
if(v.getGlideObject().getQuestion().getLabel() != '' && v.getDisplayValue() != '') {
template.space(4);
template.print(' ' + v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");
}
}
if(v.getGlideObject().getQuestion().getLabel() == 'Enter the new user\'s first name') {
var firstName = v.getDisplayValue();
}
if(v.getGlideObject().getQuestion().getLabel() == 'Enter the new user\'s last name') { //is this suppose to be this or first_name ?
var lastName = v.getDisplayValue();
}
template.print('This is the users name '+firstName+ ' '+lastName);
email.setSubject("Approval Request for:" + current.variables.firstName + ' '+ lastName);
}
===========================================
The email that gets generated is the following:
Subject:
Approval Request for:undefined undefined
body:
Options:
This
is the users name undefined undefined Options:
Enter the new user's last name = smash
SSH = false
Enter the user's company = DHSIE
Enter the new user's first name = hulk
Weblogic = false
NON-PROD VPN = false
Enter the user's email address = orlando.galindo.org@gmail.com
Project Laptop = false
Servicenow username (system generated) = hsmash
VPN = false
I have followed these discussions:
Re: Re: Adding cat item variables to email notification
Add request item variables to approval email comments
and gotten the first name and last name to print in the body, but for the locations that say undefined, I am not sure why I am getting this error.
I apologize in advanced if this is confusing but if you need me to clarify anything in more detail please let me know.
Sincerely,
Humble Commited Student
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 11:21 AM
Hm, strange enough - works fine for me.
Can you double check against which table your notification runs?
But aside from that I saw that you've got your two checks (for the label of the first name and the last name) outside of your "for" loop - at this point these labels are definitely undefined. Try putting them inside your for loop.
for (key in gr.variables) {
var v = gr.variables[key];
if(v.getGlideObject().getQuestion().getLabel() != '' && v.getDisplayValue() != '') {
template.space(4);
template.print(' ' + v.getGlideObject().getQuestion().getLabel() + " = " + v.getDisplayValue() + "\n");
}
if(v.getGlideObject().getQuestion().getLabel() == 'Enter the new user\'s first name') {
var firstName = v.getDisplayValue();
}
if(v.getGlideObject().getQuestion().getLabel() == 'Enter the new user\'s last name') { //is this suppose to be this or first_name ?
var lastName = v.getDisplayValue();
}
}
I tested it with some sample item of mine and just used two other label checks and it worked perfectly.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 01:20 AM
If the notification is on requested item table
email.setSubject("Approval Request for:" + current.variables.firstName + ' '+ current.variables.lastName);
If the notification is defined on approval table
email.setSubject("Approval Request for:" + current.sysapproval.variable_pool.firstName + ' '+ current.sysapproval.variable_pool.lastName);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 01:47 AM
Hello Kalai,
I believe they are on the Request table [sc_request];
what would you recommend?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 01:49 AM
If that is the case, you would need to use the sys ID of the request, use it to query sc_req_item table and use the gliderecord object to access the variables.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2015 01:56 AM
Hello Kalai,
I apologize, may I request an example of how to script this? Many thanks.