Pull the 'Request for' location into a catalog variable

KristinaB
Giga Contributor

Hi all,

I'm trying to populate a variable on a catalog item based on the Request For widget on a catalog item.

I currently have a script that pulls the location of the person logged into ServiceNow into a variable but that may be a service desk person and the request may be for a person in a different location. I want it to pull the location of this person.

  • I've added the Request For widget to the catalog home page
    RequestFor.png
  • I have a variable set called location and it pulls the location of the person submitting the request (the person logged into ServiceNow). The default value for this variable set is javascript: gs.getUser().getRecord().getValue('location')

Any ideas on how to update this script to pull the location of the person listed in the request for rather than the person who is logged in? Thanks so much.

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee
ServiceNow Employee

Kristina,



You need to create a script include like this:


Name : getLocation


Script:


function getLocation()


{


  var cart = new GlideRecord('sc_cart');


  cart.addQuery('user',gs.getUserID());


  cart.query();


  if(cart.next())


  {


  return cart.requested_for.location;


  }


  return '';


}



Now, have the default value as javascript:getLocation()


View solution in original post

11 REPLIES 11

I am working on it in my developer instance and hopefully I can get a more specific example to you if no one else can.


Ok, I solved this with two scripts: a script include and a catalog client script.



First, the client script:



Capture.PNG



And the script include:



Capture.PNG



It would be faster to use asynchronous AJAX.   I just mocked it up as synchronous.



The key to this is that Client Callable box on the Script Include.   That MUST be checked.   If you have any questions as to what I did here, please ask and I will be glad to explain it.


Thank you Mike. I can see this one being helpful for a different scenario


manikorada
ServiceNow Employee
ServiceNow Employee

Kristina,



You need to create a script include like this:


Name : getLocation


Script:


function getLocation()


{


  var cart = new GlideRecord('sc_cart');


  cart.addQuery('user',gs.getUserID());


  cart.query();


  if(cart.next())


  {


  return cart.requested_for.location;


  }


  return '';


}



Now, have the default value as javascript:getLocation()


Mani, this is a nice feature, but I guess this wont always work because when someone from helpdesk / servicedesk is opening a request for someone else, they will change the requestor on the "Request for" field, but the location will remain the same as the user opening it since it ´s a default value.



What she really needs, IMHO, is a catalog client script of type onChange, and with the onchange triggering with the change of the request for field. Whatever this field is populated automatically or changed by the current user opening on someone ´s behalf, it will populate with the location of the user pointed in the Request for field.



This data must be collected via a glideajax call to a script include that will provide the location of the user record based on the request for sys_id.



Let me know if you are running into a roadblock on this and I can provide some coding samples.



More on GlideAjax:



GlideAjax - ServiceNow Wiki



Cheers and happy coding,



Felipe