- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2022 12:56 AM
Request is from 3rd party tool like : "weekoff" : "[{saturday, sunday}]";
then, I have 7 checkboxes named (monday,tuesday,wednesday,thursday,friday,saturday,sunday) on catelog item.
and I want to check saturday & sunday check boxes. How to achieve this?
How to write for loop on this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2022 04:43 AM
I'm assuming your sample text is part of a larger object, so I've used that in my mock up with a hard-coded object. If not, you can remove the first 'for' and 'if' blocks. If the value of "weekoff" is truly formatted as above (curly brackets inside of an array, a space after the comma), you'll need the 'replace' lines to convert this to an actual array before the second loop, otherwise there might be a better way to work with the value you're getting, but this works (onLoad Catalog Client Script in this example)...
var obj = {"name1" : "value1", "weekoff" : "[{saturday, sunday}]"};
for (var field in obj) {
if (field == "weekoff") {
var days = obj.weekoff;
days = days.replace("[", "");
days = days.replace("{", "");
days = days.replace("]", "");
days = days.replace("}", "");
var daysArr = days.split(", ");
for (var i=0; i < daysArr.length; i++) {
g_form.setValue(daysArr[i], true);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2022 04:43 AM
I'm assuming your sample text is part of a larger object, so I've used that in my mock up with a hard-coded object. If not, you can remove the first 'for' and 'if' blocks. If the value of "weekoff" is truly formatted as above (curly brackets inside of an array, a space after the comma), you'll need the 'replace' lines to convert this to an actual array before the second loop, otherwise there might be a better way to work with the value you're getting, but this works (onLoad Catalog Client Script in this example)...
var obj = {"name1" : "value1", "weekoff" : "[{saturday, sunday}]"};
for (var field in obj) {
if (field == "weekoff") {
var days = obj.weekoff;
days = days.replace("[", "");
days = days.replace("{", "");
days = days.replace("]", "");
days = days.replace("}", "");
var daysArr = days.split(", ");
for (var i=0; i < daysArr.length; i++) {
g_form.setValue(daysArr[i], true);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 12:25 AM
Thanks a lot, brad. its working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-01-2022 08:42 AM
You are welcome!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2022 05:00 AM
Hi,
So what script did you start?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader