Need to add multiple subcategories incident form

Preeti T
Kilo Contributor

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 

1 ACCEPTED SOLUTION

Weird
Mega Sage

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.

View solution in original post

9 REPLIES 9

Try removing the curly brackets & try

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

ersureshbe
Giga Sage
Giga Sage

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.

Regards,
Suresh.

Weird
Mega Sage

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.

Preeti T
Kilo Contributor

Wow , it worked!

Thanks a lot Joni 🙂