How to send array data from Script Include to Client script

abhaysingh98
Tera Contributor

Hi Guys,

 

I have to send array data from Script Include to Client script as I have to sort this array data to get only those fields that are visible on form not all the fields but issue is that when I send it then it convert to object I tried every possible way to convert it again to array but it remains as object.

 

Script Include -

 

var getFieldDetails = Class.create();
getFieldDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getFields: function() {
        //var tableName = this.getParameter('sysparm_tableName');
        var recordSysId = this.getParameter('sysparm_recordSysId');

        var fields = [];
var viewGR = new GlideRecord('sys_ui_element');
viewGR.addEncodedQuery('sys_ui_section=ae375ebd874359109508c8480cbb355c'); //
viewGR.query();
while (viewGR.next()) {
    var field_name = viewGR.getValue('element');
    var type = viewGR.getValue('type');
    if (type == ".end_split" || type == ".split" || type == "formatter" || field_name == "major_incident") //
        continue;
    fields.push(field_name);
}
var gr = new GlideRecord('x_roho_rwd_activity');
gr.addQuery('sys_id', recordSysId);
gr.query();
while (gr.next()) {
    rec_arr = [];
    for (x = 0; x < fields.length; x++) {
        if (gr.getValue(fields[x]) == null) {
            rec_arr.push(fields[x]);
        }
    }
}
return JSON.stringify(rec_arr);
},
    type: 'getFieldDetails'
});

 

Thanks in advance.

4 REPLIES 4

Najmuddin Mohd
Mega Sage

HI @abhaysingh98 ,

Can you try with using JSON.parse() function in the client script to convert into an Array ?

If the above information helps you, Kindly mark it as Helpful or Accept the solution.
Regards,
Najmuddin.

Hi @Najmuddin Mohd ,

 

I tried using JSON.parse() function in the client script to convert into an Array to convert it but still it is not working I want to get all the elements of that array and then filter those elements or fields which are visible on the form not all fields .

Hi @abhaysingh98 ,
After you parse, did you try something like this.
If 'answer' was an Object which has a Key value pair with key 'Group Name' and value as the Group.
g_form.setValue('assignment_group',answer['Group Name']);

Regards,
Najmuddin.

Brad Bowman
Kilo Patron
Kilo Patron

If this is just a simple array, then don't JSON.stringify it in the Script Include return:

return rec_arr.join(',');

If it's not a simple array, what does it look like if you log rec_arr.join(',') before the return?  I get what the fields array would look like, but not the custom table one.