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

Happy to hear that! Glad it works now - sometimes it's the easy things (been there )



Cheers


Frank


humblecommitted
Kilo Guru

Hello Everyone!


So here is an update that I have found to work very well!


So to put the users full name into the subject line of the email
notification I did the following:


  1. I located the email notification that was firing off for the particular event.   I did this by going to the event log located at system logs>events
  2. I noticed the email that was firing off in this example was using the event name
    called "approval inserted"
    1.jpg
  3. I then searched for the "approval inserted" event to see where the table was in
    servicenow. It was attached to the Business Rule
    "Approval Events (Task)".
    2.jpg
  4. I went into here and looked at the source code.
    3.jpg
  5. I copied and pasted this code since it contained the variable that was firing
    off, "approval.inserted". I commented out the original code just in case I need
    to reference it again.   I then pasted the
    other code and changed the parm2 from "gs.getUserName()" to "gs.getUserDisplayName()"
    in order to get the full name of the user instead of their username.


/*if
      (current.state.changes() && current.state=='requested') {


    var event =
      "approval.inserted";


    if (isRequest)


          event = "request.approval.inserted";


    else if (isSCTask)


          event =
      "sc_task.approval.inserted";



    gs.eventQueue(event, current,
      gs.getUserID(), gs.getUserName());


    updateTask(current,
      current.approver.getDisplayValue() + " requested to approve
      task");


}*/


if
      (current.state.changes() && current.state=='requested') {


    var event =
      "approval.inserted";


    if (isRequest)


          event =
      "request.approval.inserted";


    else if (isSCTask)


          event =
      "sc_task.approval.inserted";



    gs.eventQueue(event, current,
      gs.getUserID(), gs.getUserDisplayName());


    updateTask(current,
      current.approver.getDisplayValue() + " requested to approve
      task");


}



6.         I then went into the email template that fired off from the associated event and
email notification.   In this example it
was email template "change.itil.approve.role"


7.         Under "change.itil.approve.role" subject line
added the ${event.parm2} from the previous step.


4.jpg


5.jpg


Oh and I populated the subject with who ever was doing the request would be in the subject line too.   heres how i did that.



I also had the assigned manager to the onboarding user emailed but since the manager of the person could have someone submit the request on their behalf, i had to add the following code to the answer segment>in advance view>in the work flow.


to get an email notification to be sent out to the manager as well as the requestor here is what I did,


Since it was a special case, because the manager name field on the ess request catalog item table, was a reference and I gave it the variable name "Manager", thats why it is marked as "answer = current.Manager"


7.jpg


6.jpg


This is what the front end user will see.


8.jpg


9.jpg10.jpg



Thanks for all your help community, you rock!


Happy thanks giving!