The CreatorCon Call for Content is officially open! Get started here.

How to get one value from an array in GlideAjax after transforming string to Object.

Markell
Tera Guru

**** Edited as the screenshots contained sensitive work info***

Hi Guys 

So I wrote a code to go to sys_groupmember table and get back the groups a user is a member of as an array, to then do an itteration to get the groups and check if the user is a member of a particular group by matching their sys_ids.

I get an issue where If I do method 1 (below):

1. Create object then bring back array of objects

find_real_file.png

then client side get that array and use evalJason to get the object and iterate through to separate the values that I can use:

find_real_file.png

This method works in Application for the Cat Item but in Portal causes an 'Unhandled exception in GlideAjax' error.

Reading around the community, I tried Option 2:

2. Turn Object into string and then doing iteration:

Server Side: using stringify to convert array to string

find_real_file.png

Client Side: parse the array itterate and use the resulting Object:

find_real_file.png

This version of the code gets me the Object and doesn't cause any JavaScript errors, but the resulting value comes back as 'undefined'

find_real_file.png

Although in the Alerts, I always get back values using either method before my iteration:

 

For me method 1 at least brings me back the value, but to work on Portal I need to use stringy.

So in Summary what I need to know is after I stringify the array in the Server script include, I pass it to the Client script, how do I get the singular value from the iteration that doesn't just return 'undefined'.

 

Kind Regards

 

Markell

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

for the 2nd approach

1) you can just return the stringified version of string

just use this

return JSON.stringify(array);

Also you require to push the groups then you should be using this

obj.sys_id = groupGr.getValue('group');

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

for the 2nd approach

1) you can just return the stringified version of string

just use this

return JSON.stringify(array);

Also you require to push the groups then you should be using this

obj.sys_id = groupGr.getValue('group');

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Both peices of advice guided me to what I needed. You are a saint and a scholar good sir.

Glad to help.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Markell 

Were you able to find the solution? I am facing the similar issue but following your script(s) are not returning the same results for me. I am trying to go a step further and below is what I am trying to do:

- User Selects Reassign UI Action on workspace

- I am using g_modal and to show reference field to sys_user in the g_modal

- User then selects a user from the list

- My GlideAjax/Script Include gets called and should return the results based on the selected user

- then another g_modal should pop-up

- I am trying to either populate this g_modal with choices of groups retrieved from the script include or only display reference to group for the selected user

 

I am getting as far retrieving object and my alert show [object object] on the client side. I feel like I am so close to getting everything I need but just cannot figure out the last few steps. 

 

Any help is much appreciated. Thanks!