- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 02:51 AM
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:
Catalog Client Script:
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:
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:
Service Portal view:
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 02:57 AM - edited 12-04-2023 02:59 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 02:57 AM - edited 12-04-2023 02:59 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 03:04 AM
Hello Tai Vu,
Thank you very much, result looks way better now, accepted as a solution 🙂
