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

Also, if you have added user preference in this session, try login out and then login back to verify.

And if you are using client script just use:

getPreference("DESIRED_PREFERENCE_NAME");

Best Regards
Aman Kumar

Jaspal Singh
Mega Patron
Mega Patron

Hi Fabrizio,

If it is a property then you can use

data.mainReportPrefrence=gs.getProperty('custom_report_admin');

to get the value.

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

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi again,

To check the type of object, use instanceof instead of typeof.

e.g.

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

Result

*** Script: true