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

Thanks @SUMANTH DOSAPATI  for your inputs. 

 

These did not work for me. When I tried to join , it says "undefined"

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

So instead of merging and splitting, I tried below and it almost worked ..

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

 


Now, the drop down is having the options as I want but only problem is ids adding "[" and "]" at the start and end options like below..

Drop down options ..

["1"

"2"

"3"

"4"]


Its literally splitting the string with "," not treating it as array. Any idea how to make this treat as an array ? Thanks

You can use replace function to replace brackets with null.

Example :

var formatted_string = "[hi hello]";
var value = formatted_string.replace("[", "");

//it will become "hi hello]"

You can use as per your string to replaces brackets or anything else.

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

 

Feel free to reach out if you have further questions or else you can mark an answer as correct and helpful to close the thread so that it benefits future visitors also.

Regards,
Sumanth