Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Populate Single Line Text field from the selected in the List Collector

mluxsubr
Tera Contributor

Task: "Populate serial number field based on all devices that are selected in the ListCollector"

Question: Any tips or recommendations to improve this might be helpful

Process description: In the personal developer instance inside (any) Catalog Item a new Variable set is created with the following variables:

  • Device / "u_device_list"  (List Collector) 
  • Serial Number / "u_serial_number" (Single Line Text)

Serial Number can be auto-populated using new Vancouver feature "Auto Populate" only if based on the Reference type fields, but not on the ListCollector type. For this case can be used Catalog Client Script + Script Include with GlideAjax (as GlideRecord in Client Scripts is not recommended by the best practices, as it slows down the instance performance). 

Solution:

Variable Set: 

mluxsubr_7-1701686160693.png

 

Catalog Client Script: 

 

mluxsubr_2-1701685622132.png

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    function showSerialNumbers(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_serial_number', answer);
    }

    var AllCurrentSelectedDevices = g_form.getValue('u_device_list');
    var ga = new GlideAjax('pdi_test_ScriptInclude');
    ga.addParam('sysparm_name', 'TestFunction');
    ga.addParam('sysparm_currentdevices', AllCurrentSelectedDevices);
    ga.getXML(showSerialNumbers);

}

 

Script Include: 

 

mluxsubr_4-1701685735379.png

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

	TestFunction: function() {
		var device_serialNum = this.getParameter('sysparm_currentdevices');
		var answer = '';
		var gr = new GlideRecord('cmdb_ci');
		gr.addQuery('sys_id','IN',device_serialNum.toString());
		gr.query();
		while(gr.next())
		{			
			answer = answer + gr.getValue('serial_number')+',';
		}
		return answer;
	},

    type: 'pdi_test_ScriptInclude'
});

 

Result:

Backend view:

 

mluxsubr_5-1701685858474.png

 

Service Portal view:

 

mluxsubr_6-1701685954325.png

P.S.: When the selected device (any record from the cmdb_ci table) has empty serial number field, Script Include returns "null"; The result will always have "," in the end; In what order the serial numbers will be displayed in the u_serial_number field is out of control.

 

Question: Is there a way to improve what is mentioned above?

1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @mluxsubr 

Let's adjust a little bit in your function from the script include.

1. Define an array to store the Serial Number

2. Filter out CIs with empty Serial Number

3. Push Serial Number to the array

4. Return array convert to string.

 

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

	TestFunction: function() {
		var device_serialNum = this.getParameter('sysparm_currentdevices');
		var answer = []; //array to collect the Serial Number
		var gr = new GlideRecord('cmdb_ci');
		gr.addQuery('sys_id','IN',device_serialNum.toString());
		gr.addNotNullQuery('serial_number'); //filter out CIs with empty Serial Number
		gr.query();
		while(gr.next()){			
			answer.push(gr.getValue('serial_number')); //push Serial Number to the array
		}
		return answer.join(',');
	},

    type: 'pdi_test_ScriptInclude'
});

 

 

Cheers,

Tai Vu

View solution in original post

2 REPLIES 2

Tai Vu
Kilo Patron
Kilo Patron

Hi @mluxsubr 

Let's adjust a little bit in your function from the script include.

1. Define an array to store the Serial Number

2. Filter out CIs with empty Serial Number

3. Push Serial Number to the array

4. Return array convert to string.

 

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

	TestFunction: function() {
		var device_serialNum = this.getParameter('sysparm_currentdevices');
		var answer = []; //array to collect the Serial Number
		var gr = new GlideRecord('cmdb_ci');
		gr.addQuery('sys_id','IN',device_serialNum.toString());
		gr.addNotNullQuery('serial_number'); //filter out CIs with empty Serial Number
		gr.query();
		while(gr.next()){			
			answer.push(gr.getValue('serial_number')); //push Serial Number to the array
		}
		return answer.join(',');
	},

    type: 'pdi_test_ScriptInclude'
});

 

 

Cheers,

Tai Vu

mluxsubr
Tera Contributor

Hello Tai Vu,

Thank you very much, result looks way better now, accepted as a solution 🙂

mluxsubr_0-1701687839010.png