- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 09:06 AM
Hi All,
I need to insert multiple subcategories (>50) for a common dependent value of category.
Is there any way that it cab be done by some background /fix script rather than creating it manually.
Please suggest.
Thank you
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 12:51 AM
Let's consider the script you have already done:
var choices = ['Test03','Test01'];
for(var i = 0; i < choices.length; i++){
var grChoice = new GlideRecord("sys_choice");
grChoice.initialize();
grChoice.name = 'incident';
grChoice.element = 'subcategory';
grChoice.dependent_value = 'inquiry';
grChoice.value = 'test';
grChoice.label = choices[i];
grChoice.insert();
}
If you want to generate the values like this, and you know exactly what you want, then you can use nested arrays.
For example:
var choices = [['Test03.Label','Test03.value'],['Test01.label','Test01.value']];
for(var i = 0; i < choices.length; i++){
var grChoice = new GlideRecord("sys_choice");
grChoice.initialize();
grChoice.name = 'incident';
grChoice.element = 'subcategory';
grChoice.dependent_value = 'inquiry';
grChoice.label = choices[i][0];
grChoice.value = choices[i][1];
grChoice.insert();
}
Here the choices now has another array in each index. These arrays contain pairs for Label and Value.
So in this case in your loop we are getting the array in index i and then from that array we get the label and value both held in their own index 0 and 1. Saying choices[i][0] means we want the value from choices arrays index i, which is an array, and from it we want the value held in first index 0.
You could also do this with JSON if you wanted, but I made the example with arrays since you're already using them.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 09:16 AM
Hi Preeti,
Is it for catalog item then try importing question_choice table
OR
If for backend form then try importing it to sys_choice table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 09:21 AM
Hi Jaspal,
This is for backend table sys_choice one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 09:31 AM
Hi Jaspal,
This is for sys_choice table .
I tried using background script
var choices = [{'Test03'},{'Test01'}];
for(var i = 0; i < choices.length; i++){
var grChoice = new GlideRecord("sys_choice");
grChoice.initialize();
grChoice.name = 'incident';
grChoice.element = 'subcategory';
grChoice.dependent_value = 'inquiry';
grChoice.value = 'test';
grChoice.label = choices[i];
grChoice.insert();
}
This is creating records but in the lable column I am getting [object][object]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2022 09:33 AM
Hi Jaspal,
I just removed the curly braces from Line 1.
var choices = ['Test03','Test01'];
It worked. Not sure if this is the correct approach to insert multiple records
Have a good day!
Thanks ,
Preeti