Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

gs.getUser().getPreference() it returns me an object, but the data is a string

Fabrizio Joaqui
Mega Guru
i made this function to take user preference in script server:
find_real_file.png
find_real_file.png
it returns me an object, why?
The value is a string.
find_real_file.png


1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Fabrizio,

According to the documentation, getPreference() returns a "String". Note the uppercase "S". This denotes that the returned type is an Object of type String. This is different from primitive type "string".

https://www.w3schools.com/js/js_typeof.asp

https://www.tutorialsteacher.com/articles/how-to-get-type-of-object-in-javascript

Try the following using "new String()". This will return an object of type "String" and not a primitive "string".

prefReport = new String('test');
gs.info(typeof(prefReport));

Result:

*** Script: object

If it's necessary to get type as a string, use "String()" without the "new" to create a primitive "string".

var prefReport = String(gs.getUser().getPreference('alm_asset.db.order'));
gs.info(typeof(prefReport));

Result

*** Script: string

View solution in original post

8 REPLIES 8

Aman Kumar S
Kilo Patron

A/C SN docs:

The getPreference() method returns a string containing the value of the preference requested, or null if no such preference is defined.

 

Can you explicitly change the value to string using JSON.stringify(prefReport); and check the alert or logs

Best Regards
Aman Kumar

I did as you said but in the logs it keeps giving me 'object'

find_real_file.png

I tried this:

gs.info(gs.getUser().getPreference('catalog_script_client.db.order'));
 
Its giving me the correct value.
 
Try with:
gs.getUser().getPreference('custom_report.main');
 
I hope you are writing this in a server side editor, doesn't work on client side.
 
Best Regards
Aman Kumar

var prefReport = gs.getUser().getPreference('custom_report.main');

yes, give me the value but if you try to write

gs.info(typeof(prefReport));

it gives me an object not a string, i need a string, i also tried JSON.stringify(prefReport) but it doesn't works