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

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