Populate list collector value of a reference field in catalogue form based on another field value

Shilpa29
Tera Contributor

If the variable 'v_ci_service_all_business_units' is Yes, auto populate all the active business unit names from 'u_business_unit' table to the catalogue variable 'v_ci_dependent_business_units' which is a list collector on the catalogue form. 

I was able to directly fetch the name in script include querying u_buisness_unit table where active is true, append and pass them to client script which is working well. But one record contains comma in the name which is being displayed as two records under 'v_ci_dependent_business_units' attribute. For example: Business unit 'People, Culture & advocacy' is being displayed as two values 'People', 'Culture & advocacy'. Hence I tried to pass the sys_id and name from script include to client script and use them to set the desired value. This code works for single record but not working for all business units where I passed values using array. Please help. 

Script include:

var cmdbBusinessUnitFilter = Class.create();
cmdbBusinessUnitFilter.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCDTOCIData: function() {
var result = {};

var us = new GlideRecord('u_business_unit');
us.addEncodedQuery('u_active=true');
us.query();
var array = [];

while (us.next()) {
      result.u_name = us.u_name.toString();
      result.sys_id = us.sys_id.toString();
      array.push(result); //--> This is passing only single business unit name, sys_id for 16 times as opposed to 16 actual sys_id's and their names.  
}
return JSON.stringify(array);
},
type: 'getCDTOCIInfo'
});

 

Client  Script:
var bear_info=[];
var ga = new GlideAjax('cmdbBusinessUnitFilter');
ga.addParam('sysparm_name', 'getCDTOCIData');
ga.getXML(GetBearDetails);

 

function GetBearDetails(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");
var bear_info = JSON.parse(answer);
var bear_info1 = answer.split(','); //to split the object variables for measuring length.
alert('answer:'+ answer);
alert('bear_info:'+bear_info);
alert('bear_info1:'+bear_info1);
alert('bear_info1.length'+bear_info1.length);
for (var a = 0; a<bear_info1.length; a++) { 
alert(bear_info1.u_name[a]);
alert(bear_info1.sys_id[a]);

g_form.setValue('v_ci_dependent_business_units', bear_info1.sys_id[a],bear_info1.u_name[a]);
}

 

 
 
1 ACCEPTED SOLUTION

@Shilpa29 

have a look at my blog on how to set list collector based on other variable, mark it helpful as well

Dynamically set list collector on change of variable 

I have also updated your scripts as this-> please enhance it from your side

Script Include:

 

var cmdbBusinessUnitFilter = Class.create();
cmdbBusinessUnitFilter.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCDTOCIData: function() {
		var us = new GlideRecord('u_business_unit');
		us.addEncodedQuery('u_active=true');
		us.query();
		var array = [];
		while (us.next()) {
			array.push(us.sys_id.toString());
		}
		return array.toString();
	},

	type: 'getCDTOCIInfo'
});

 

client script

 

var ga = new GlideAjax('cmdbBusinessUnitFilter');
ga.addParam('sysparm_name', 'getCDTOCIData');
ga.getXML(GetBearDetails);

function GetBearDetails(response) {

	var answer = response.responseXML.documentElement.getAttribute("answer");

	g_form.setValue('v_ci_dependent_business_units', answer);
}

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Shilpa29 

update this line the result object should be inside the while

var cmdbBusinessUnitFilter = Class.create();
cmdbBusinessUnitFilter.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getCDTOCIData: function() {
var us = new GlideRecord('u_business_unit');
us.addEncodedQuery('u_active=true');
us.query();
var array = [];

while (us.next()) {
var result = {};
result.u_name = us.u_name.toString();
result.sys_id = us.sys_id.toString();
array.push(result); //--> This is passing only single business unit name, sys_id for 16 times as opposed to 16 actual sys_id's and their names.
}
return JSON.stringify(array);
},

type: 'getCDTOCIInfo'
});

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Shilpa29
Tera Contributor

@Ankur Bawiskar, Thanks for your reply. It worked. Now, I am able to send all sys_id and names from script include to client script. However, the original issue still exists where I am unable retrieve the array data (sys_id and name) in client script and display it in list collector catalogue variable.

 

Client  Script:
var bear_info=[];
var ga = new GlideAjax('cmdbBusinessUnitFilter');
ga.addParam('sysparm_name', 'getCDTOCIData');
ga.getXML(GetBearDetails);

 

function GetBearDetails(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");
var bear_info = JSON.parse(answer);
var bear_info1 = answer.split(','); //to split the object variables for measuring length.
alert('answer:'+ answer);
alert('bear_info:'+bear_info);
alert('bear_info1:'+bear_info1);
alert('bear_info1.length'+bear_info1.length);
for (var a = 0; a<bear_info1.length; a++) { 
g_form.setValue('v_ci_dependent_business_units', bear_info1.sys_id[a],bear_info1.u_name[a]); --> this is not working, I am sure something wrong with the forloop. But unable to figure out. 
}

 

@Shilpa29 

have a look at my blog on how to set list collector based on other variable, mark it helpful as well

Dynamically set list collector on change of variable 

I have also updated your scripts as this-> please enhance it from your side

Script Include:

 

var cmdbBusinessUnitFilter = Class.create();
cmdbBusinessUnitFilter.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCDTOCIData: function() {
		var us = new GlideRecord('u_business_unit');
		us.addEncodedQuery('u_active=true');
		us.query();
		var array = [];
		while (us.next()) {
			array.push(us.sys_id.toString());
		}
		return array.toString();
	},

	type: 'getCDTOCIInfo'
});

 

client script

 

var ga = new GlideAjax('cmdbBusinessUnitFilter');
ga.addParam('sysparm_name', 'getCDTOCIData');
ga.getXML(GetBearDetails);

function GetBearDetails(response) {

	var answer = response.responseXML.documentElement.getAttribute("answer");

	g_form.setValue('v_ci_dependent_business_units', answer);
}

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

The above client script is not returning the list values, facing run time error. I realized that we need to pass both sys_id and name to set the attribute as below. I tried for single record it is working but facing issues for array of records.

 

var ga = new GlideAjax('cmdbBusinessUnitFilter');
ga.addParam('sysparm_name', 'getCDTOCIData');
ga.getXML(GetBearDetails);

function GetBearDetails(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");
var parser = JSON.parse(answer);
var arrSysId = [];
var arrName = [];

for(var i=0;i<parser.length;i++){
    arrName.push(parser[i].u_name.toString());
    arrSysId.push(parser[i].sys_id.toString());
}
// give here the list collector variable name
for(var j=0;j<arrSysId.length;ji++){
g_form.setValue('v_ci_dependent_business_units', arrSysId[j],arrName[j]); // 
}

 

This code is returning single value as opposed to full list of business units. Thanks in advance