- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 07:16 AM
Hi,
I have a list collector ref to customer_account. When I select records here in a new field it will populate the name-number of those records. Tried onchange client script not working in UI/Portal.
The table is in Global scope and RP is another. I checked allow configuration as well.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var comDetails = [];
var name_num;
var comL = g_form.getValue('additional_accounts');
g_form.addInfoMessage(comL);
var com = comL.split(',');
for (var i = 0; i < com.length; i++) {
var gr = new GlideRecord('customer_account');
gr.query('sys_id', com[i]);
gr.query();
while (gr.next()) {
g_form.addInfoMessage(gr.getValue('sys_id'));
name_num = gr.name + "-" + gr.number;
comDetails.push(name_num);
}
}
g_form.setValue('additional_companies_details',comDetails);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 08:28 PM
Hi @Rosy14
You can give my adjustment a try.
#Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('AccountDetails'); //script include name
ga.addParam('sysparm_name', 'getAccountDetails'); //function name within script include
ga.addParam('sysparm_account_ids', newValue);
ga.getXMLAnswer(function(answer){
var result = answer; //just split the answer if you'd like to loop through each element
g_form.setValue('additional_companies_details', result);
});
}
#Script Include
var AccountDetails = Class.create();
AccountDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAccountDetails: function() {
var accountIDs = this.getParameter('sysparm_account_ids');
var accounts = [];
var gr = new GlideRecord('customer_account');
gr.addQuery('sys_id', 'IN', accountIDs);
gr.query();
while(gr.next()){
var account = gr.getValue('name') + "-" + gr.getValue('number');
accounts.push(account);
}
return accounts.join(',');
},
type: 'AccountDetails'
});
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 07:21 AM
Hello @Rosy14
You are using GlideRecord object in client script which is not recommended.
You can use Script include & GlideAjax.
Below link have same logic...may be helpful for you...
You can adust your code accordingly.
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 08:11 AM
I tried but not working .
//Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var comDetails = [];
var name_num;
var comL = g_form.getValue('additional_accounts');
var ga = new GlideAjax('AccountDetails'); //script include name
ga.addParam('sysparm_name', 'getAccountDetails'); //function name within script include
ga.addParam('sysparm_sysID', comL);
ga.getXMLAnswer(callBackFunction);
function callBackFunction(answer) {
/* here we will get the result from script include & Parse the answer */
var result = answer;
g_form.setValue('additional_companies_details', result);
}
//Script Include
var AccountDetails = Class.create();
AccountDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAccountDetails: function() {
var accdetails, name_num;
var comDetails = [];
var getSysId = this.getParameter('sysparm_sysID');
g_form.addInfoMessage(getSysId);
var com = getSysId.split(',');
for (var i = 0; i < com.length; i++) {
var gr = new GlideRecord('customer_account');
gr.query('sys_id', com[i]);
gr.query();
while (gr.next()) {
g_form.addInfoMessage("test" + gr.getValue('sys_id'));
name_num = gr.name + "-" + gr.number;
comDetails.push(name_num);
}
}
accdetails = comDetails.toString();
return accdetails;
},
type: 'AccountDetails'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 08:07 PM
Hello @Rosy14
Try like below :
1.Script include :
//Script Include
var AccountDetails = Class.create();
AccountDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAccountDetails: function() {
/*1.Declare & initialize array to return */
var comDetails = [];
/*2.Get the value of list from client script */
var getSysId = this.getParameter('sysparm_sysID');
/*3.Glide record on your table */
var gr = new GlideRecord('customer_account');
gr.addQuery('sys_idIN', getSysId); // use addQuery
gr.query();
while (gr.next()) {
/*3.1 Create object to push it array */
var comObject = {
'name' :'',
'number' :''
}
/*3.2 set value in object */
comObject.name = gr.getValue('name');
comObject.number = gr.getValue('number');
/*3.3 Push comObject in array */
comDetails.push(comObject);
}
/*4 Return the comDetails array */
return comDetails.toString();
},
type: 'AccountDetails'
});
2.Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var comL = g_form.getValue('additional_accounts');
var ga = new GlideAjax('AccountDetails'); //script include name
ga.addParam('sysparm_name', 'getAccountDetails'); //function name within script include
ga.addParam('sysparm_sysID', comL);
ga.getXMLAnswer(callBackFunction);
function callBackFunction(answer) {
/* here we will get the result from script include in the form of array */
var result = answer; //if this not work try using var result = JSON.parse(answer);
g_form.setValue('additional_companies_details', result);
/*=====================================================
If you want to separate the data you can loop through result & get it
=====================================================*/
}
}
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 08:28 PM
Hi @Rosy14
You can give my adjustment a try.
#Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('AccountDetails'); //script include name
ga.addParam('sysparm_name', 'getAccountDetails'); //function name within script include
ga.addParam('sysparm_account_ids', newValue);
ga.getXMLAnswer(function(answer){
var result = answer; //just split the answer if you'd like to loop through each element
g_form.setValue('additional_companies_details', result);
});
}
#Script Include
var AccountDetails = Class.create();
AccountDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAccountDetails: function() {
var accountIDs = this.getParameter('sysparm_account_ids');
var accounts = [];
var gr = new GlideRecord('customer_account');
gr.addQuery('sys_id', 'IN', accountIDs);
gr.query();
while(gr.next()){
var account = gr.getValue('name') + "-" + gr.getValue('number');
accounts.push(account);
}
return accounts.join(',');
},
type: 'AccountDetails'
});
Cheers,
Tai Vu