How to show list collector value in select box

Samiksha2
Mega Sage

Hi,

I want to show the list collector value in select box. How to do that? Right now I have a script include which is fetching the value of a list field. I want to show that values in a select box in the catalog item.

 

Please help in this.

 

Thanks!!

1 ACCEPTED SOLUTION

Hi @Samiksha2 ,

Try changing your client script to the one below.

 

function onChange(control, oldValue, newValue, isLoading) {
  if (newValue == '') {
    return;
  }
  var ga = new GlideAjax('getUsername');
  ga.addParam('sysparm_name', 'getUser');
  ga.addParam('request_for', newValue);
  ga.getXMLAnswer(setProduct);

  function setProduct(response) {
    var resp = JSON.parse(response);
    for(var i=0; i < resp.length; i++){
      g_form.addOption('u_hardware', resp[i], resp[i]);
    }
  }
}

 

 

Note: Check if the Script Include is Client Callable and returning the values needed.

 

Thanks,

Anvesh

Thanks,
Anvesh

View solution in original post

3 REPLIES 3

AnveshKumar M
Tera Sage
Tera Sage

Hi @Samiksha2 ,

I understand that, you want the values of a list collector field values as options in select box drop down, is that correct?

If Yes, what is the table the list collector field is referring to? And, are you using just the SelectBox in catalog form or that one is also referring to same table as List collector?

 

Thanks,

Anvesh

Thanks,
Anvesh

Hi @AnveshKumar M,

Here the requirement is in u_hardware table there are three fields- Primary(reference) and Backup(List) field and name(String). In the catalog Item- there are three varibles- Requested for(logged in User), Select the Option and Hardware(Select box).

scenario is - If requested for is Primary and Backup then the respective hardware name will populate in the Hardware field.

 When a requester is a primary or backup of multiple hardware products then both product name should display in the drop down.

 

 

 

Script Include

getUser: function() {
var userSys = [];
var requested_for = this.getParameter('request_for');
var hardware_val = new GlideRecord("u_hardware");
hardware_val.addQuery('u_primary=' + requested_for + '^ORu_backupLIKE' + requested_for);
hardware_val.query();
while (hardware_val.next()) {

userSys.push(hardware_val.getValue('name'));
}
return JSON.stringify(userSys);
},

 

Client script:(onChnage of Requested for)

function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var ga = new GlideAjax('getUsername');
ga.addParam('sysparm_name', 'getUser');
ga.addParam('request_for', newValue);
ga.getXML(setProduct);

function setProduct(response) {
var ans = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
g_form.setValue('u_hardware', ans);

}
}

 

 

Hi @Samiksha2 ,

Try changing your client script to the one below.

 

function onChange(control, oldValue, newValue, isLoading) {
  if (newValue == '') {
    return;
  }
  var ga = new GlideAjax('getUsername');
  ga.addParam('sysparm_name', 'getUser');
  ga.addParam('request_for', newValue);
  ga.getXMLAnswer(setProduct);

  function setProduct(response) {
    var resp = JSON.parse(response);
    for(var i=0; i < resp.length; i++){
      g_form.addOption('u_hardware', resp[i], resp[i]);
    }
  }
}

 

 

Note: Check if the Script Include is Client Callable and returning the values needed.

 

Thanks,

Anvesh

Thanks,
Anvesh