We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Problem with gs.getUser().getRecord().getDisplayValue() when comparing in array

adriantan08
Giga Expert

Hi, Guys.

I stumbled upon this interesting observation.

So when I did this:

var t = ["Finance", "Sales"];

var t1 = gs.getUser().getRecord().getDisplayValue('department');

gs.log(t.indexOf(t1));

 

this will always evaluate to -1. It's very odd because doing a simple == comparison returns true (i.e. t1 == "Finance"). 

Please let me know if there's just something I'm missing - could even be a something I'm missing in basic javascript. 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

Hi Adrian,

are you trying to search the department value into that array then do this;

indexOf doesn't work properly on array in servicenow; instead use ArrayUtil ServiceNow class

var t = ["Finance","Sales"];

var t1 = gs.getUser().getRecord().getDisplayValue('department');

var arrUtil = new ArrayUtil();

var val = arrUtil.indexOf(t, t1);

gs.info(val);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

6 REPLIES 6

Manish Vinayak1
Tera Guru

Typecast the result with String type and see if you get result on using indexof:

var t = ["Finance", "Sales"];

var t1 = String(gs.getUser().getRecord().getDisplayValue('department'));

gs.log(t.indexOf(t1));

Hope this helps!

Cheers,

Manish

Ankur Bawiskar
Tera Patron

Hi Adrian,

are you trying to search the department value into that array then do this;

indexOf doesn't work properly on array in servicenow; instead use ArrayUtil ServiceNow class

var t = ["Finance","Sales"];

var t1 = gs.getUser().getRecord().getDisplayValue('department');

var arrUtil = new ArrayUtil();

var val = arrUtil.indexOf(t, t1);

gs.info(val);

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hello @Ankur Bawiskar ,

I have written below to print the logged-in user's manager name. but I am not getting any value it's returning nothing. but for other reference filed like title, and location it's giving me proper data .please guide me.

thank you.

gs.print(gs.getUser().getRecord().getDisplayValue('manager'));

@abhijee 

it worked fine for me in global scope. it won't work in scoped app

screenshot below

manager name.gif

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