How to add comma seperated list as option in a Choice type field?

Saurav Bhardwa2
Tera Contributor

Hello,

I am getting a list like ( a, b, c, d, e, f) from GlideAjax in Client script response. If I wanted to add all these options in a choice type field. How will I do that?

This is the script I am using, but it's not adding the options. I had checked, I am getting the response from server side.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 
 
 
    var siteName = g_form.getValue('u_sharepoint_sites');
    var ga = new GlideAjax('global.SharePointUtils');
    ga.addParam('sysparm_name', 'allSiteName');
    ga.addParam('sysparm_site_name', siteName);
    ga.getXMLAnswer(getSite);
 
    function getSite(response) {
console.log('test123 ' + response);
        //if (response) {
        // Parse the JSON response from SharePoint
        var jsonResponse = JSON.parse(response);
        console.log('test1234 ' + jsonResponse);
        if (jsonResponse && jsonResponse.value) {
            // Iterate through the sites and add options to the choice list
            jsonResponse.value.forEach(function(site) {
                if (site.name) {
                    // Add each site as an option to the choice list
                    g_form.addOption('u_sharepoint_sites', site.name, site.name);
                }
            });
 
        }
 
    }
}
6 REPLIES 6

Hemanth M1
Giga Sage
Giga Sage

Hi @Saurav Bhardwa2 ,

did you check if its printing inside forEach function??

try to get a response as an array or convert the response to an array as below

//after parsing
 var jsonResponse = JSON.parse(response);
jsonResponse =jsonResponse.split(",") //you would be having jsonResponse as [a, b, c, d, e, f]
//then rest of the logic....

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Hello @Hemanth M1 

It's not printing anything in jsonResponse variable neither in each function. Is there any other way of doing this?

Hi @Saurav Bhardwa2 ,

 

forEach function wouldn't iterate through comma separated string , it expects an array , so try above logic i mentioned to convert to array and check

or try below one and also try to print responsearray

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 
 
 
    var siteName = g_form.getValue('u_sharepoint_sites');
    var ga = new GlideAjax('global.SharePointUtils');
    ga.addParam('sysparm_name', 'allSiteName');
    ga.addParam('sysparm_site_name', siteName);
    ga.getXMLAnswer(getSite);
 
    function getSite(response) {
console.log('test123 ' + response);
        //if (response) {
        // Parse the JSON response from SharePoint
        var jsonResponse = JSON.parse(response);
        console.log('test1234 ' + jsonResponse);
        if (jsonResponse && jsonResponse.value) {
         var responsearray = jsonResponse.value.split(","); //convert to an array
            // Iterate through the sites and add options to the choice list
            responsearray.forEach(function(site) {
                if (site.name) {
                    // Add each site as an option to the choice list
                    g_form.addOption('u_sharepoint_sites', site, site);
                }
            });
 
        }
 
    }
}

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

No it did not work. 

Below is the function I am using in Script include, if that helps

allSiteName: function() {
try {
var r = new sn_ws.RESTMessageV2('SharePoint Graph', 'Default GET');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

if (httpStatus === 200) {
// Parse the JSON response
var jsonResponse = JSON.parse(responseBody);

// Check if the "value" property exists in the response
if (jsonResponse && jsonResponse.value) {
var siteNames = [];

// Iterate through the "value" array and extract the "name" property
jsonResponse.value.forEach(function(site) {
if (site.name) {
siteNames.push(site.name);
}
});

// Now, siteNames array contains the names of the SharePoint sites
var name = siteNames.join(", ");
return name;
} else {
//gs.info("Response does not contain 'value' property or 'name' property.");
}
} else {
//gs.info("HTTP Request failed with status: " + httpStatus);
}
} catch (ex) {
var message = ex.message;
gs.info("Error: " + message);
}

},