Dynamically create checkbox using catalog client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2017 03:52 AM
Hi,
My requirement is to dynamically create checkbox using catalog client script in the "On Change" event of a select box.
I have a variable set (which applies to some service catalogs), in which there is a select box, which is populated using a REST API. When the selected value in the select box changes, a catalog client script should be triggered, and check box (multiple check boxes) should be created. The text of these check boxes will be populated from the response of a REST API.
Is this possible? I have checked the wiki and couldn't find a way to do this. I have tried for other variable types as well (like List Collector) but I couldn't find anything that can help.
Help is much appreciated.
Thanks,
Satheesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2017 04:22 AM
Hi Satheesh,
You can create a drop down variable and hide it onLoad. Once you do some change in other field and get response from REST api you can populate the drop down with g_form.addOptions() and show the variable.
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2017 05:16 AM
Hi Ankur,
Thanks for the response.
I am able to populate the drop down variable in the right way. But the issue that I am facing is with displaying the Check box (multiple check boxes). The number of check boxes depends upon the response from the API which is invoked once the drop down variable value is set.
I don't think I can even define a check box variable within the variable set, because the number of check boxes is unknown.
Any help is much appreciated.
Thanks,
Satheesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2017 05:36 AM
Hi Satheesh,
Got your point. Since they are checkboxes you will have to give values for those in the variable. Here is one workaround.
Once you get the response create the choices from script.
var arr = ['value1', 'value2', 'value3'];
for(var i=0;i<arr.length;i++){
var gr = new GlideRecord('question_choice');
gr.initialize();
gr.question = sys_id // this is the sys_id of the variable in item_option_new table
gr.text = arr[i];
gr.value = arr[i];
gr.insert();
}
The above line of code will be repeated in an array
This script assumes that the values you get from REST API are sometimes constant. Or else what you can do is first delete all the choices and then use the above script. This solution is a workaround. May be some other approach will be present.
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2017 05:54 AM
Hi Satheesh,
One thing which will work for you is below:
1) Create a string field on catalog and store the values which user has selected while submitting the request
2) Use this string field and iterate over this to create dynamic checkboxes in ui page using DOM manipulation
3) Once user submits the form in the same UI page fetch the string field which contains the values user had selected while submitting and create them again in UI page through DOM manipulation and make them as readonly.
I know this solution is somewhat trickier but it will work. I had followed the same approach earlier for one of my requirement.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader