how to populate name fields in the subject line of an notification email.

humblecommitted
Kilo Guru

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

1 ACCEPTED SOLUTION

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.



ScriptLog.png


View solution in original post

11 REPLIES 11

Example of how to query


GlideRecord - ServiceNow Wiki



Table to query is sc_req_item


Use sys ID parameter to query and current.sys_id to get request's sys ID


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);


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.


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.



ScriptLog.png


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!