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

Hi Bharat,



The issue is not with populating the department field - I get that done easily when I send the g-user.userID value to the script include.


The problem is not being able to send the variable from the requested_for reference field to the script include.


By the way, if I choose any other variable from the form, I get valid results. So I think it definitely has to do with the reference field. Perhaps it being a reference field is the problem?


try to pass value as ......requested_for.requested_for


Hi Bharat,



Please explain: where should I pass the value? How?


thanks


harel


andrew_lawlor
Giga Expert

I think Brad is right in that there is an issue with Client Script execution order.



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'm assuming the script that populates the requested_for field in an onLoad Client Script attached to the variable set you mentioned (or as a default value on the variable itself?) To prove this, why don't you try adding this to the beginning of your onLoad Client Script:




g_form.setValue('requested_for', g_user.userID, g_user.getFullName());




Let me know what happens.


Hi Andrew,



Nothing happens, actually. If I remove the default value from the requested_for variable field,, even after adding the line to the onLoad script, the field comes up empty. If I don't remove the default value, I still get nothing passed on to the script include.


For reference, I inserted the line right at the beginning of the script.


I am pretty sure it is a variable set issue, because adding a "normal" reference field on the form, with the same javascript:gs.getUserID(); script as its default value, and testing on it, works nicely.