List collector

Rosy14
Tera Guru

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.

Rosy14_0-1706022976981.png

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);

}
1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

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

View solution in original post

5 REPLIES 5

Vishal Birajdar
Giga Sage

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...

https://www.servicenow.com/community/developer-forum/onchange-client-script-is-not-working-in-worksp...

 

You can adust your code accordingly.

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

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'
});

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 
        =====================================================*/
       
    }

} 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Tai Vu
Kilo Patron
Kilo Patron

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