Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Comma separation is not working

suvarna sonar2
Tera Contributor

I have created two field on custom table

1)u_access_to_countries - List Collector and refernce to the country table

2)u_access_to_region - string field

 

I want to populate region of country in string field. Below script is working for the initial value of u_access_to_countries.But when i am selecting 2nd value that time it is not working.

 

 

Script include:

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

    getRegionsForCountries: function() {
       
       
        var country_sysID = this.getParameter('sysparm_access');
        //for (var i = 0; i < country_sysID.length; i++) {
        var gr_cntry = new GlideRecord('core_country');
            gr_cntry.addQuery('sys_id', country_sysID);
            gr_cntry.query();
            var answer = [];
            if (gr_cntry.next()) {
                //var answer = [];
                answer.push(gr_cntry.u_region.toString());
               
               
               
            }
           
       
        return JSON.stringify(answer);
},
    type: 'AccessCountryScript'
});
 
 
Client script: Onchange client script on "u_access_to_countries"
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

   
    var ga = new GlideAjax('AccessCountryScript');
    ga.addParam('sysparm_name', 'getRegionsForCountries');
    ga.addParam('sysparm_access', g_form.getValue('u_access_to_countries').split(','));
    ga.getXMLAnswer(populateRegion);

    function populateRegion(answer) {
        answer = JSON.parse(answer);
        var regions = [];
        for (var i = 0; i < answer.length; i++) {
        regions.push(answer[i]);

        }
        // Set the value of the u_access_to_region field with the returned regions
        g_form.setValue('u_access_to_region', regions.join(','));
    }
}
2 ACCEPTED SOLUTIONS

Brad Bowman
Mega Patron

Your Script Include needs to accommodate multiple sys_ids, and you want to return a joined array, not JSON:

 

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

    getRegionsForCountries: function() {
        var country_sysID = this.getParameter('sysparm_access');
        var gr_cntry = new GlideRecord('core_country');
        gr_cntry.addQuery('sys_id', 'IN', country_sysID);
        gr_cntry.query();
        var answer = [];
        while (gr_cntry.next()) {
                answer.push(gr_cntry.u_region.toString());
        }
       
        return answer.join(', ');
},
    type: 'AccessCountryScript'
});

Your Client Script just needs to set the string field to the returned value:

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

    var ga = new GlideAjax('AccessCountryScript');
    ga.addParam('sysparm_name', 'getRegionsForCountries');
    ga.addParam('sysparm_access', g_form.getValue('u_access_to_countries').split(','));
    ga.getXMLAnswer(populateRegion);

    function populateRegion(answer) {
        // Set the value of the u_access_to_region field with the returned regions
        g_form.setValue('u_access_to_region', answer);
    }
}

 

 

View solution in original post

Ankur Bawiskar
Tera Patron

@suvarna sonar2 

Try the approach shared by @Brad Bowman   and it should work

Share us the updates.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Brad Bowman
Mega Patron

Your Script Include needs to accommodate multiple sys_ids, and you want to return a joined array, not JSON:

 

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

    getRegionsForCountries: function() {
        var country_sysID = this.getParameter('sysparm_access');
        var gr_cntry = new GlideRecord('core_country');
        gr_cntry.addQuery('sys_id', 'IN', country_sysID);
        gr_cntry.query();
        var answer = [];
        while (gr_cntry.next()) {
                answer.push(gr_cntry.u_region.toString());
        }
       
        return answer.join(', ');
},
    type: 'AccessCountryScript'
});

Your Client Script just needs to set the string field to the returned value:

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

    var ga = new GlideAjax('AccessCountryScript');
    ga.addParam('sysparm_name', 'getRegionsForCountries');
    ga.addParam('sysparm_access', g_form.getValue('u_access_to_countries').split(','));
    ga.getXMLAnswer(populateRegion);

    function populateRegion(answer) {
        // Set the value of the u_access_to_region field with the returned regions
        g_form.setValue('u_access_to_region', answer);
    }
}

 

 

Ankur Bawiskar
Tera Patron

@suvarna sonar2 

Try the approach shared by @Brad Bowman   and it should work

Share us the updates.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader