- 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:37 AM
Try removing the curly brackets & try
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 11:25 PM
Thank you Jaspal,
And if we want to put different values as per the label . for example - label : ['Test03','Test01']; we would like to have different values, how can we achieve this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 09:17 AM
Hi,
Prepare the choices as excel file and import using import set table to create the record. Can you refer below link to understand the steps.
https://www.servicenowelite.com/blog/2019/6/27/import-configuration-items-with-excel-spreadsheets
Please mark correct answer if it helped.
Regards,
Suresh.
Suresh.
- 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-23-2022 01:03 AM
Wow , it worked!
Thanks a lot Joni 🙂