How to create array in Glide record to perform some actions in the array's value

VIKAS MISHRA
Tera Contributor

I have 10 incidents where i need to change the state of all the incidents from open to close.

i do not have any condition/filter to put in the Glide record to get all these 10 records hence i want to create one array where i will mentioned all the 10 incidents then will run a loop so that it will close all the incidents one after another, 

Please suggest the background script for this.

3 ACCEPTED SOLUTIONS

Sagar Pagar
Tera Patron

Hi @VIKAS MISHRA,

 

Here is sample scripts. modify it accordingly.

var incArray = ["INC12345", "INC23456", "INC4353464", "INC5676578"];

gs.info(incArray + " - " + incArray.length);

for (var i = 0; i < incArray.length; i++) {

	var inc_record = new GlideRecord('incident');
	inc_record.query("number", incArray[i]);
	inc_record.query();
	if (inc_record.next()) {

		inc_record.field_name1 = "values1";
		inc_record.field_name2 = "values2";

		inc_record.setWorkflow(false);
		inc_record.autoSysFields(false);
		inc_record.update()
	}

}

 

Thanks,

Sagar Pagar

The world works with ServiceNow

View solution in original post

Mohith Devatte
Tera Sage
Tera Sage

hello @VIKAS MISHRA ,

you can try this script 

 

 

var incs = ["incident number 1", "incident number 2", "incident number 3", "incident number 4"]; 
// store your 10 incident numbers in this array 
for (var i = 0; i < incs.length; i++) {
var gr = new GlideRecord('incident');
gr.addQuery("number",incs[i]);
gr.query();
if (gr.next()) {
gr.state= "state_choice_value";
gr.update();
}
}

 

 

Hope this helps 

Mark the answer correct if this helps you

View solution in original post

Show your script

If should be something like below

var incs = ["incident number 1", "incident number 2", "incident number 3", "incident number 4"]; 
// store your 10 incident numbers in this array 
for (var i = 0; i < incs.length; i++) {
var gr = new GlideRecord('incident');
gr.addQuery("number",incs[i]);
gr.query();
if (gr.next()) {
gr.state= "<Add yiour resolved state value>"; //You need to add this valye in <> from your instance
gr.<close_code> = '<Close Code in yoiur instance>';   //You need to add this valye in <> from your instance
gr.<close_notes> = '<text value you want to add as notes>';   //You need to add this valye in <> from your instance
gr.update();
}
}
-Anurag

View solution in original post

9 REPLIES 9

Sagar Pagar
Tera Patron

Hi @VIKAS MISHRA,

 

Here is sample scripts. modify it accordingly.

var incArray = ["INC12345", "INC23456", "INC4353464", "INC5676578"];

gs.info(incArray + " - " + incArray.length);

for (var i = 0; i < incArray.length; i++) {

	var inc_record = new GlideRecord('incident');
	inc_record.query("number", incArray[i]);
	inc_record.query();
	if (inc_record.next()) {

		inc_record.field_name1 = "values1";
		inc_record.field_name2 = "values2";

		inc_record.setWorkflow(false);
		inc_record.autoSysFields(false);
		inc_record.update()
	}

}

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Thanks for the above mentioned script, but when i am using this script then field "resolution code" and resolved Note" are coming as mandatory hence incident is not getting updated.

I tried to jupdat those 2 fields along with the state field but still getting below mentioned error :

 

*** Script: INC0014697,INC0014705 - 2
Background message, type:error, message: Data Policy Exception: The following fields are mandatory: Resolution code, Close notes
Background message, type:error, message: Data Policy Exception: The following fields are mandatory: Resolution code, Close notes

 

Mohith Devatte
Tera Sage
Tera Sage

hello @VIKAS MISHRA ,

you can try this script 

 

 

var incs = ["incident number 1", "incident number 2", "incident number 3", "incident number 4"]; 
// store your 10 incident numbers in this array 
for (var i = 0; i < incs.length; i++) {
var gr = new GlideRecord('incident');
gr.addQuery("number",incs[i]);
gr.query();
if (gr.next()) {
gr.state= "state_choice_value";
gr.update();
}
}

 

 

Hope this helps 

Mark the answer correct if this helps you

Thanks for the above mentioned script, but when i am using this script then field "resolution code" and resolved Note" are coming as mandatory hence incident is not getting updated.

I tried to jupdat those 2 fields along with the state field but still getting below mentioned error :

 

*** Script: INC0014697,INC0014705 - 2
Background message, type:error, message: Data Policy Exception: The following fields are mandatory: Resolution code, Close notes
Background message, type:error, message: Data Policy Exception: The following fields are mandatory: Resolution code, Close notes