How to Prase an Array and set it to a drop down variable in catalog client script

Senthil9
Kilo Contributor

Hi, I have a application id field, as soon as user enters a application id, I need to call a backend REST API and get all projects mapped with that id and display it in a drop down.

so I have an onchange client script and corresponding script include to call rest API

Client script :

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

   //Type appropriate comment here, and begin script below
   var req=newValue;
var ga = new GlideAjax('GetDSProjectDetails');
ga.addParam('sysparm_name', 'getProject');

ga.getXML(GetDSProjectParse);
 
function GetDSProjectParse(response) {
    var proj = response.responseXML.documentElement.getAttribute("answer");
    alert(proj);
    g_form.setValue("projects",parseArray(proj));
  }
}

 

Script Include :

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

    type: 'GetDSProjectDetails',
    getProject:function(){    
        var jsonPayload = "12345";
        var request = new sn_ws.RESTMessageV2('Apllication ID', 'GROUP');
        request.setRequestBody(JSON.stringify(jsonPayload));    
        response = request.execute();
        var JSONData = new global.JSON().decode(response.getBody());
        gs.info(JSONData);
        return JSONData;
    },

});

To test it, for now I have hard coded application id as 1234. Response from the rest API is an ARRAY. 

response : [["A"],["B"],["C"],["D"],["E"],["F"],["G"]] 

Now, How do  I parse this array response and assign it to project field as a drop down in my client script? Any help would be greatly appreicated. Thanks in advance.  

 

1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage
Mega Sage

Hi Senthil,

Convert your array to comma seperated string using below syntax

var arr = ['1,','2','3'];
var arr_string = arr.join(',');

//now arr_string will hold value as "1,2,3"

 

In your client script now again change the comma seperated string back to array as below

var proj = response.responseXML.documentElement.getAttribute('answer');
var array = proj.split(',');
for(var i=0; i<array.length; i++)
{
g_form.clearOptions('projects');
g_form.addOption('projects', array[i] , array[i]);
}

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

 

View solution in original post

7 REPLIES 7

Mahendra RC
Mega Sage

Hello Senthil,

I believe to parse the array you can iterate through all the elements in Array

function GetDSProjectParse(response) {
    var proj = response.responseXML.documentElement.getAttribute("answer");
	g_form.clearOptions('projects'); // to remove the dropdown option which are present on this field
	g_form.addOption('projects', '' , '-- None --'); // to add --None-- option as the start
    for (var element in proj) {
		var projElement = proj[element];
		
		for (var items in projElement) {
			var itemValue = projElement[items];
			g_form.addOption('projects', itemValue , itemValue); // change this as per the Label and Value of your dropdown options
		}
	}
}

Please mark this helpful/correct if this resolves your issue

Thanks

Hi, Its not working .. I tried adding alert in multiple places and seeing this..

 

 alert(projElement) --> org.mozilla.javascript.NativeArray@1c6294d

alert(itemValue) --> prints some error 

    for (var element in proj) {
		var projElement = proj[element];
		alert(projElement)
		for (var items in projElement) {
			var itemValue = projElement[items];
	                alert(itemValue)
			g_form.addOption('projects', itemValue , itemValue); // change this as per the Label and Value of your dropdown options
		}
	}

Hello Senthil,

It seems that the response to your API is not an array. Could you please share the exact API response.

Thanks

SumanthDosapati
Mega Sage
Mega Sage

Hi Senthil,

Convert your array to comma seperated string using below syntax

var arr = ['1,','2','3'];
var arr_string = arr.join(',');

//now arr_string will hold value as "1,2,3"

 

In your client script now again change the comma seperated string back to array as below

var proj = response.responseXML.documentElement.getAttribute('answer');
var array = proj.split(',');
for(var i=0; i<array.length; i++)
{
g_form.clearOptions('projects');
g_form.addOption('projects', array[i] , array[i]);
}

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth