- 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 02:02 AM
Example of how to query
Table to query is sc_req_item
Use sys ID parameter to query and current.sys_id to get request's sys ID
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2015 09:30 AM
Hello Orlando,
given the fact you're using "current.sysapproval" in your notification it is most likely the case that this notification is defined against the Approval table.
In that case you could simply do the following since you're GlideRecord already gets the correct Requested Item:
email.setSubject("Approval Request for:" + gr.variables.firstName + ' '+ gr.variables.lastName);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2015 10:44 AM
Hey Frank,
Thanks for getting back to me. I just inputted that code and still getting the same errors. Any other tips and tracks anyone would advise would greatly be appreciated. I will keep everyone informed if I stumple upon a 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 11:29 AM
Hey Frank,
Thanks for getting back to me. I just inputted that code and I am hammering it out right now. I am now able to get it to populate to the subject line!!! Hurray!
But I am currently still getting the error within the email body.
So the problem was I had to change the:
email.setSubject("Approval Request for:" + gr.variables.firstName + ' '+ gr.variables.lastName);
to
email.setSubject("Approval Request for:" + gr.variables.first_name + ' '+ gr.variables.last_name);
The "first_name" and "last_name" was the variable names/labels i gave the single line text fields on the ess service catalog table.
I am sure there are some other tweaks i have to play around with to get it in the email body. I will post the complete solution once it has been attained.
Big thanks guys!