Notification email script - check user role and redirect to ess or default view

alexm3
Kilo Expert

Hello,

I have configured a notification that will be sent to the user opening a request on our Service Catalog.

The requirement is that if the user opening the request has no roles, he should be redirected to our ESS portal. If the user opening the request has roles(ITIL) he should be redirected to the normal view. I have the following code:

attachLink();

function attachLink() {

  //Check for requestor's role

  var userObj = gs.getUser().getUserByID(current.requested_for.user_name);

  if (userObj.hasRole('itil')) {

  template.print('<a href="/nav_to.do?uri=sc_req_item.do?sys_id='+current.sys_id+'">' + current.number + '</a>');

  }

  else {

  template.print('<a href="/ess/ess_ritm.do?uri=sc_req_item.do?sys_id='+current.sys_id+'">' + current.number + '</a>');

  }

  }

Unfortunately the code is not working, it's not checking if the user has ITIL role or not and all users are redirected to '<a href="/ess/ess_ritm.do?uri=sc_req_item.do?sys_id='+current.sys_id+'">' + current.number + '</a>');

What do I have to do to make the above code work?

I have limited experience with scripting therefore any ideas will be much appreciated!

Alex

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I would make sure you're getting the right requested for user. It looks like this notification may be firing from the sc_req_item table, but there is no requested_for field on that table. You may need to do current.request.requested_for;


View solution in original post

4 REPLIES 4

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I would make sure you're getting the right requested for user. It looks like this notification may be firing from the sc_req_item table, but there is no requested_for field on that table. You may need to do current.request.requested_for;


Brad,



That did the trick! Thank you very much for your fast response!



Alex


Hi Alexandru,



I'm going to resurrect this thread as I'm having to do something very similar and instead of the ESS portal I have Service Portal. My first question is, where do you add the below script?



attachLink();


function attachLink() {


  //Check for requestor's role


  var userObj = gs.getUser().getUserByID(current.request.requested_for);


  if (userObj.hasRole('itil')) {


  template.print('<a href="/nav_to.do?uri=sc_req_item.do?sys_id='+current.sys_id+'">' + current.number + '</a>');


  }


  else {


  template.print('<a href="/ess/ess_ritm.do?uri=sc_req_item.do?sys_id='+current.sys_id+'">' + current.number + '</a>');


  }


  }



If it's for a request notification, would you add this in the relevant notification/template or did you add it as a 'notification email script' and referenced in the notification where it says   'Click here to view request' ?



Thanks in advance.


VJ4
Giga Expert

You can ignore my question as I've managed to solve the issue.



Cheers!