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

Wait, are you using getReference() or getValue() to get the value of you requested_for variable? You should be using getValue(). It shouldn't matter whether the variable is in a variable set or attached to the item.


Hi Brad,



I tried both, actually, with the same results. I even tried adding a timer to wait for 3 seconds before invoking the script include, in case the client script needs to take time to wait for the form to fill:


setTimeout(function(){


script here


}, 3000);



So I received the alert after 3 seconds, but still with an "undefined" in it.


Brad Tilton
ServiceNow Employee
ServiceNow Employee

If you're not getting the value then I would guess the issue is with the variable name. I just want to confirm that the name of the variable is requested_for and it is present on the catalog item you're running this on.


Yes, it is requested_for and it is present there.


I added another field - a text field this time, again with a default value of javascript:gs.getUserID() and queried on it. It is working as expected. That's why I think it might be an issue with the field being a reference field.


test.JPG



[edit]: OK, so a quick update:


I changed the above text field 'test' to a reference field and it is working. When the user has MyCompany in its source, the department number changes.


It is not working on the requested_for field, however.


Normally, I would just go ahead and insert a new field, but I really can't, because it is a variable set which is already implemented in other catalog items...


Bharat23
Kilo Guru

Instead of g_form.setValue('Department_Number', '111'); try using it this way


setValue(fieldName, value, displayValue) and provide the displayValue also