- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 06:39 PM - edited 11-06-2023 09:51 PM
I have a payload that has around 15 Key and value pairs. If I use below logic , I can fetch key , value as expected. But that is resulting in too many iterations. Is there a way to use IF condition directly , without for loop. (These fields are choice fields)
var mapLoad= JSON.parse(gs.getProperty('XXXX'));
//Both Fields are Choice fields
if (mapLoad) {
for (var key in mapLoad) {
if (current.FIELD_NAME_1 == key)
{
current.FIELD_NAME_2= mapLoad[key]
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 09:48 PM
Hi @Kumar38 ,
Try below script
var mapLoad = JSON.parse(gs.getProperty('XXXX'));
if (mapLoad && current.FIELD_NAME_1) {
var key = current.FIELD_NAME_1; // Assuming FIELD_NAME_1 contains the key you want to use
if (mapLoad.hasOwnProperty(key)) {
current.FIELD_NAME_2 = mapLoad[key];
}
}
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 07:44 PM
can you share the json and which key you want to fetch from it?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 08:52 PM - edited 11-06-2023 09:13 PM
It can be anything from the payload , its from a BR , if values changes on field 1 (choice list) , it updates field 2 (Choice list ) , based on key value pair.
where as key = Field1 choice 1 : Filed 2 choice 1 and so on
{
"Field1_Choice1": "Field2_Choice1",
"Field1_Choice2": "Field2_Choice1",
"Field1_Choice3": "Field2_Choice3",
"Field1_Choice4": "Field2_Choice2"
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 09:48 PM
Hi @Kumar38 ,
Try below script
var mapLoad = JSON.parse(gs.getProperty('XXXX'));
if (mapLoad && current.FIELD_NAME_1) {
var key = current.FIELD_NAME_1; // Assuming FIELD_NAME_1 contains the key you want to use
if (mapLoad.hasOwnProperty(key)) {
current.FIELD_NAME_2 = mapLoad[key];
}
}
Thanks,
Anand