The array are not Returning values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Saturday
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:
And in client script , whenever the caller changes the groups will be printed to the Description.
But the Result is showing
Can you please help me what is wrong in this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
After pushing into array and before passing back to client you must convert it to string from array
return JSON.stringify(arr);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
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!
