how to get value from list collector and display it in read only text box field in servicenow service portal

Arkesh Kumar
Giga Guru

how to get value from list collector and display it in read only text box field in servicenow service portal using client script

find_real_file.png

"Select Role Name of South Africa" is list collector and "Selected Role Name for South Africa" is text box field

3 REPLIES 3

Vamshi dhar
Kilo Guru

Hi Arkesh,

You want to populate the text box variable onselection of List collector field or onload it self

 

Can you please elaborate???

 

 

Thanks

Vamshidhar

yes when i select in List Collector, it should autopopulated in text field

Follow the below Steps Arkesh, it will fulfill your requirement.

1. Create Onchange client script and pass the value which we get from List collector field to script include. Follow the below snippet of code

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setReadOnly('employee', true); // Code to make the text field read only on load of a form.
}

var listCol = g_form.getValue('requester');

// code to get the name of List Collector field instead of Sys Id
var ga = new GlideAjax('calculate');
ga.addParam('sysparm_name','returnName');
ga.addParam('sysparm_sys_id',listCol);
ga.getXML(getSysId);

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

}

2. Create a Client Callable script include and get the value from Client script as shown in the below code to return the person name.

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

returnName: function() {

var usrName = '';
var sysId = this.getParameter('sysparm_sys_id');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', sysId);
gr.query();

if (gr.next()) {
usrName = gr.name;
}
gs.log(usrName,'usrName');
return usrName;  //. Returning the name of the value which selected In the list collector field.
},
});

In My case reference table was User Table, so I glided user table to get Name instead of Sysid. 

Adjust the code as per the requirement it will fulfill your requirement.

 

2nd Alternative:

Instead of Text field, use the reference field. If you do this there is no need of calling script include and all.

Simply this code works for you.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setReadOnly('employee', true);
}

var listCol = g_form.getValue('requester');

g_form.setValue('employee',listCol); 

}

 

Mark this answer as helpful, if your requirement is fulfilled.

Let me know if you any questions. 

Please find the screenshots attached for your reference.