How to check - if a specific value is present in the List type (Table field) from Catalog Client Script?

sik1
Kilo Contributor

Hi All,

I am working on ‘Service Catalog’ to order ‘Hardware’ (Laptop, Headset…etc) via referring “Hardware Model” table. On the catalog form I have a field “location” which is populated from table “cmn_location”. I want dropdown field should be populated with vales where location field matches in the “Hardware Model” table with field “u_country” (List type).

Table details:-

1. Locations (“cmn_location”)
2. Hardware Model (“cmdb_hardware_product_model”) - field “u_country” (List type).

find_real_file.png
3. Catalog form

 find_real_file.png

Function (onchange) – location field. Here is my logic in Catalog Client Script

function onChange(control, oldValue, newValue, isLoading) {

   if (isLoading || newValue == '') {

                 //g_form.addInfoMessage('I am in Loading');

      return;

   } else {

                 //Clear all values/options from all Drop downs

                 g_form.clearOptions('Laptop_Select_Product');

               

                 //Include --None-- values

                 g_form.addOption('Laptop_Select_Product','--None--','--None--');

               

                 //1. Get values in Laptop Drop Down.

                 var gr1 = new GlideRecord('cmdb_hardware_product_model');

                 gr1.addQuery('status','In Production');

                 gr1.addQuery('u_category','Laptop');

                 gr1.addQuery('u_country','CONTAINS', g_form.getValue('location'));

                 gr1.query();

                 while(gr1.next()){

                                g_form.addOption('Laptop_Select_Product',gr1.name,gr1.name);

                 }

But all values are not populating as you can see

 find_real_file.png

 

3 REPLIES 3

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

You should not use glide record in client script.

You have to move this code to Script include and use glide ajax. Also you should not use else loop. meaning this:

   if (isLoading || newValue == '') {

                 //g_form.addInfoMessage('I am in Loading');

      return;

   } else {

 

It should be

   if (isLoading || newValue == '') {

                 //g_form.addInfoMessage('I am in Loading');

      return;

   } 

 

YOur code here

 



Thanks,
Ashutosh

Jaspal Singh
Mega Patron
Mega Patron

Replace 

  gr1.addQuery('u_country','CONTAINS', g_form.getValue('location'));

with

  gr1.addQuery('u_country','IN', g_form.getValue('location'));

 

Also, avoid using GlideRecord in Client side scripting as a best practice.

sik1
Kilo Contributor

Hi @Ashutosh Munot 

i change the things - Using GlideAjax on Client side and calling Script include.

So client script looks like :---

var ga = new GlideAjax('ScriptIncludeCatalog_HardwareUtil');
ga.addParam('sysparm_name', 'myFunction_1');
ga.addParam('sysparm_status', "In Production");
ga.addParam('sysparm_u_category', "Laptop");
ga.addParam('sysparm_u_country', g_form.getValue('location'));
ga.getXML(HelloWorldParse);

function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
; }

 

Include Script part is :---

var ScriptIncludeCatalog_HardwareUtil = Class.create();
ScriptIncludeCatalog_HardwareUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
myFunction_1: function()
{
var arr = [];
//alert('Server Side');
var gr1 = new GlideRecord('cmdb_hardware_product_model');
gr1.addQuery('status', "In Production");
gr1.addQuery('u_category', "Laptop");
gr1.addQuery('u_country', this.getParameter('sysparm_u_country'));
gr1.query();
while(gr1.next()){
arr.push(gr1.name);

}
return arr;
},

type: 'ScriptIncludeCatalog_HardwareUtil'
});

 

it is returning the value as well (i can see only 1 or 1'st value if it contains more then 1) but how parse the values and the main thing is How to Query so that i can find/compare the location value in the List type. List type field is having 2 values separated via , (like Singapore,India)

@Jaspal Singh I already try with 'CONTAIN' and 'IN' but nothing works.

Any thing else i should try ??