How to get the Key and value from the System Property in a script include?

Subhashree3
Giga Contributor

Hi All,

I have a system property that has value stored like :

{"US":"United States","CN":"China","CA":"Canada","JP":"Japan"}

I want to access this System property in a script include where the condition needs to be checked as if the Key is 'US', Value should be returned and 'United States'. 

Very new to system properties. Could you all please help me how to resolve this?

Thank you.

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

Here is an example using an OOTB System Property with code run in Xplore

System Property Value

{"REPORT" : "book","MAP" : "image","HOMEPAGE" : "home","SURVEY" : "form","ASSESSMENT" : "form","TIMELINE" : "calendar","SCRIPT" : "script","LIST" : "list","DETAIL" : "book-open","NEW" : "add","SEARCH" : "search","LABEL" : "label","HTML" : "script","DIRECT" : "view","NULL" : "help"}

Script

var index = 'REPORT';
var props = gs.getProperty('glide.ui.nav.type_icon_map');
var hashMap = JSON.parse(props); // Add global prefix if in scope
var answer = hashMap[index];

answer; 

Output

book

Replace the system property with your own and the appropriate error checking and you should be good.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

9 REPLIES 9

As the original question has now been answered, can you please mark my question as correct so this question is shown as solved and removed from the unsolved questions list?

Thanks 🙂

Paul


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hey The SN Nerd,
Thank you for this one, I have a question though. What if we want to change the value of 'REPORT' as specified in the System Property Value to another value, shall we say for example 'ServiceNow Nerd' instead of 'book'? Can you please write me a script on how to do it? I haven't found one from my days of searching.

Subhashree3
Giga Contributor

Hi Paul,

The above solution worked fine for one scenario. Thank You for this. However for one more scenario, I need to get the reverse. Means if I give input as "book", output should be "Report" as per your example and we need to maintain this one System property.

Please let me know how it can be done.

Thanks

skumar_srbh
Tera Guru

in the above question scernario only,  how to handle the default/another case ? 

suppose, I  am getting 3 contry code("US":"United States","CN":"China","CA":"Canada","JP":"Japan) via system property whereas 2 other country code via direct script/ source.. (in this case what country code we are getting, should be remain as is)

please suggest... Thanks in advance!!!

muhammadosama55
Tera Contributor

Easiest way to check the system property is to use  Background script

var propertyValue = gs.getProperty('sla.date');
gs.print('The value of the system property is: ' + propertyValue);
 
Output:
The value of the system property is: 2024-10-01 00:00:00