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
make these changes
Script Include
usergroup: function() {
var arr = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', this.getParameter('sysparm1')); // use user column
gr.query();
while (gr.next()) {
arr.push(gr.group.getDisplayValue()); // get group name
}
return arr.toString(); // return string
},
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
Hello @sayanghosh0 ,
When you return an array from a Script Include, it must be returned as a string, because the callback function in a Client Script can only receive string values.
That’s why you need to convert the array into a string format before returning it.
You can do this by using:
return JSON.stringify(arrayName);
Parse it in client script callback function :
var response=response.responseXML.documentElement.getAttribute('answer')
var result = JSON.parse(res);
you can refer following attachment.
If this helps you then mark it as helpful and accept as solution.
Regards,
Aditya,
Technical consultant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
Hello @sayanghosh0 ,
If my response helps you then mark it as helpful and accept as solution.
regards,
Aditya,
Technical Consultant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
Hello @sayanghosh0
Please make the following changes in your script include and onChange client script:
Script Include: Client Callable true
usergroup: function() {
var arr = [];
var userID = this.getParameter("sysparm_callerID");
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userID); // this will be your callerID
gr.query();
while (gr.next()) {
arr.push(gr.group.getDisplayValue()); // get group name
}
return arr.join(", "); // return string
},
OnChange Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue == '') {
return;
}
var ga = new GlideAjax("incidentCount");
ga.addParam("sysparm_name", "usergroup");
ga.addParam("sysparm_callerID", newValue);
ga.getXML(callback);
function callback(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
g_form.setValue("description", answer);
}
}
Thank you!
Thank You!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
Any reason to share the same script again which was already shared by other members?
this leads to confusion to the person who asked question
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
