Getting a value in a catalog client script

oharel
Kilo Sage

Hi there,

In a catalog item, I have a variable reference field called requested_for. It is auto-populated by "javascript:gs.getUserID();" when the form loads; however, the user can change it when requesting an item for someone else.

I would like to set another variable - department - as read only with '111' in it, in case the requested_for source contains the words "MyCompany".

I am unable to do it with the below script:

function onLoad() {

    //Type appropriate comment here, and begin script below

    var userID = g_form.getValue('requested_for');//gets the user ID

  alert('requested for is ' + userID);

  var ga = new GlideAjax('getUserSource'); //this is the script include

  ga.addParam('sysparm_name', 'getSource'); //this is the function within the script include

  ga.addParam('sysparm_usr_source', userID);

  ga.getXML(getResponse);

  function getResponse(response) {

  var values = response.responseXML.documentElement.getAttribute('answer');

  if(values == 'yes') {

  g_form.setValue('Department_Number', '111');

  g_form.setReadOnly('Department_Number', true);

  } else if(values == 'no') {

  g_form.setReadOnly('Department_Number', false);

  }

  }

}

The alert to tell me who the requested_for returns "requested for is " (no name, sys_id or anything).

However, it is working when the first line is

var userID = g_user.userID;

instead of var userID = g_form.getValue('requested_for');

But in that case, it does not check the requested_for field...

(Also, gs.log in the script include shows that it is not getting the requested_for value.)

ideas?

harel

1 ACCEPTED SOLUTION

oharel
Kilo Sage

Guys, thanks for the help and for not giving up!



So HI solved this.


The problem was that I named both the variable set and the variable in it "requested_for ". This (quote from HI) "causes the issue here. Using the same name in both the variable set and variable causing the element search to fail and find the variable set as the element rather than the variable itself."


I would never have thought that the system refers to the actual name in this regard.


I will mark my answer as correct, so other people can benefit from it and from your answers


View solution in original post

15 REPLIES 15

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Looks like you're running this on load, but I think you'd want to load it onchange of that requested for variable, right? Also, could you post the script include you're calling? Seems like maybe the form is loading and this script is loading before there's a value in that requested for variable.


Hi Brad,



I also applied the same script to onChange script for that field. Once the field is changed, it still gives an "undefined" alert. When the onChange script is active (but not the onLoad one), I don't get any alert, though, when the page loads, so maybe I will need both?


The script include is as follows (client callable):


var getUserSource = Class.create();


getUserSource.prototype = Object.extendsObject(AbstractAjaxProcessor,{


     


  getSource : function() {



  var userSrc = this.getParameter('sysparm_usr_source');


  var userSource = new GlideRecord('sys_user');



  userSource.addQuery('sys_id', userSrc);


  userSource.addQuery('source','CONTAINS','MyCompany');


  userSource.query();



  if(userSource.next()) {


  gs.log('HBS, script include, yes, getUserSource, requested for is ' + userSrc); //troubleshooting


  return 'yes';


  }


  gs.log('HBS, script includege, no, UserSource, requested for is ' + userSrc); //troubleshooting


  return 'no';



  },



  type: 'getUserSource'


});



Thanks,


Harel


Brad Tilton
ServiceNow Employee
ServiceNow Employee

To have it run when the page loads make sure you're not returning false when isLoading is true. I would add some logging to your client script right after you grab the value of the user variable to make sure that's grabbing a value.


Hi Brad,



yes, I tried that because I suspected it is not collecting anything. When I add


alert(userID);


right below the getReference statement, I get "undefined" in the alert.


That's why I think I think the code I am using to get it is wrong somehow.


I could do with the g_user.userID statement, and add an onChange script, if the requested_for is changed, but that does not seem ti collect the data as well.


Ooh... forgot to mention: the requested_for variable is a variable set. Does it matter?