The array are not Returning values

sayanghosh0
Tera Contributor

Hello Team,

 

I was practicing the Array concept in ServiceNow.

I have written a script include where it will return the groups , a particular user belongs to.

Script:

   usergroup: function() {
        var arr = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('sys_id', this.getParameter('sysparm1'));
        gr.query();
        while (gr.next()) {
            arr.push(gr.group.toString());
        }
        return arr;
    },

 

 

And in client script , whenever the caller changes the groups will be printed to the Description.

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

    var ga = new GlideAjax('global.useremail');
    ga.addParam('sysparm_name','usergroup');
    ga.addParam('sysparm1',newValue);
    ga.getXMLAnswer(pop);
    function pop(response){
       
        g_form.setValue('description',response);
    }
   
}

But the Result is showing
sayanghosh0_0-1767512903521.png

 

Can you please help me what is wrong in this.
 
Best Regards

 

9 REPLIES 9

Sarthak Kashyap
Mega Sage

Hi @sayanghosh0 ,

 

I saw 1 issue in your script include

usergroup: function() {
        var arr = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', this.getParameter('sysparm1')); // make user here bcoz your are taking the sysId of caller which is user.
        gr.query();
        while (gr.next()) {
            arr.push(gr.group.toString());
        }
        return arr.join(', ');
    },

 

Also make sure your script include is client callable/GlideAjax callable.

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

satyam SINGH2
Tera Contributor

You are passing the caller sys id in client script - that means your sys id record is store in sys user table so while querying the gr member table use name field which make relationship with user table. That's the concept. 

 

d_17
Tera Expert

@sayanghosh0 ,

 

After pushing into array and before passing back to client you must convert it to string from array

return JSON.stringify(arr);

 

TejasSN_LogicX
Tera Contributor

You’re almost right! The issue is happening because you are returning an array from your Script Include, but the Description field on the form is a string type.

When GlideAjax sends data back to the client script, it always returns it as a string. So if you return an array directly, it may not display correctly in the Description field.

To fix this, you can simply convert the array to a string before returning it in the Script

in your script include return like :

return arr.join(",");

this will convert your array into string 

 

 

or 

 

 

 

if you prefer to return the array as-is, then you should convert it to a string in your client script before setting the field value:

 

g_form.setValue('description', response.toString());

 

Let me know if you need the proper syntax or any further help — feel free to reach out to me anytime!