Trying to create multiple child incidents for a parent incident

rossdrew
Tera Contributor

I am trying to create 1 or many child incidents records (depending on the number of rows in a special note in work notes that is parsed into arrays). The works up to the point where I am attempting to insert the children records. Any ideas on what is wrong in the code below?

function getCustomerList(){

//var incsysid = g_form.getUniqueValue();

//alert('sysid = ' + incsysid);

var product = g_form.getValue('u_product');
var priority = g_form.getValue('priority');

if (product == "AAAA" && priority == 1){

var gr = new GlideRecord('sys_journal_field');
gr.addQuery('element_id',incsysid);
gr.addQuery('value','CONTAINS','Multiple Customers Impacted;;'); //header of special note in worknotes
gr.query();

while(gr.next()){
var list = gr.getValue('value');

//alert("customer list: " + list);

var splitlist = list.split(";;"); //creates customer list array that still needs to be split further

if(Array.isArray(splitlist)){alert("splitlist is an array");}

alert("splitlist: " + splitlist);

var arrlen = splitlist.length - 1; // minus one ensure no empty record at the end of the for loop

alert ("array length is " + arrlen);

//the for loop parses the inner array to get the customer SLA details for the children incident

for (var i = 0; i < arrlen; i++) {

var sla_list = splitlist.shift();

//alert("sla_list is " + i + ": "+sla_list);

var arrlen2 = sla_list.length;

//alert ("array length2 is " + arrlen2);

var sla_data_list = sla_list.split("#");

if(Array.isArray(sla_data_list)){alert("sla_data_list is an array");}

alert("sla_data_list" : " + sla_data_list);

var cusname = sla_data_list[0];

if(cusname != "Multiple Customers Impacted"){
sid = sla_data_list[1];
slamins = sla_data_list[2];

alert ("cusname"+i+": " + cusname + " sid: " + sid + " sla mins " + slamins); // Code works perfectly up to here

alert ("parent incident is " + current.number);
alert ("caller is " + current.caller_id);


/**********Code works perfectly up to here and then script stops and no children incidents are created*************************/

var inc = new GlideRecord('incident');

inc.initialize();

inc.caller_id = current.caller_id;
inc.contact_type = current.contact_type;
inc.state = current.state;
inc.location = current.location;
inc.impact = current.impact;
inc.urgency = current.urgency;
inc.category = current.category;
inc.subcategory = current.subcategory;
inc.cmdb_ci = current.cmdb_ci;
inc.assignment_group = current.assignment_group;
inc.assigned_to = current.assigned_to;
inc.company = current.company;
inc.u_product = current.u_product;
inc.u_environment = current.u_environment;
inc.work_start = current.work_start;
inc.work_end = current.work_end;
inc.u_rca_case = current.u_rca_case;
inc.short_description = current.short_description;
inc.description = current.description;
inc.u_sla_impact_minutes = slamins;
inc.impact = current.impact;
inc.urgency = current.urgency;
inc.opened_by = current.opened_by;
inc.close_code = current.close_code;
inc.closed_at = current.closed_at;
inc.close_notes = current.close_notes;
inc.parent_incident = current.sys_id;

inc.insert();

}


}

}

}

}

1 ACCEPTED SOLUTION

Hi Ross,

I just removed some unnecessary code at the bottom of the script and commented out a lot of the fields you are trying to populate into the child incident. Can you please retest and see if it creates the simple incident.

function getCustomerList() {
	var product = g_form.getValue('u_product');
	var priority = g_form.getValue('priority');

	if (product == "AAAA" && priority == 1){
		gsftSubmit(null, g_form.getFormElement(), 'customer_list'); //MUST call the 'Action name' set in this UI Action
	}
	else {
		return false;
	}
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
	createChildIncidents();

function createChildIncidents() {

	var gr = new GlideRecord('sys_journal_field');
	gr.addQuery('element_id',current.sys_id);
	gr.addQuery('value','CONTAINS','Multiple Customers Impacted;;'); //header of special note in worknotes
	//gr.setLimit(1); //set the limit to 1 if you only expect one journal entry to contain details
	gr.query();

	while (gr.next()){
		var list = gr.getValue('value');
		var splitlist = list.split(";;"); //creates customer list array that still needs to be split further
		var arrlen = splitlist.length - 1; // minus one ensure no empty record at the end of the for loop

		//the for loop parses the inner array to get the customer SLA details for the children incident
		for (var i=0; i< arrlen; i++) {

			var sla_list = splitlist.shift();
			gs.info("sla_list is " + i + ": "+sla_list);

			var arrlen2 = sla_list.length;
			gs.info("array length2 is " + arrlen2);

			var sla_data_list = sla_list.split("#");
			var cusname = sla_data_list[0];

			if(cusname != "Multiple Customers Impacted"){
				sid = sla_data_list[1];
				slamins = sla_data_list[2];

				gs.info("cusname"+ i + ": " + cusname + " sid: " + sid + " sla mins " + slamins); // Code works perfectly up to here

				gs.info("parent incident is " + current.number);
				gs.info("caller is " + current.caller_id);


				/**********Code works perfectly up to here and then script stops and no children incidents are created*************************/

				var inc = new GlideRecord('incident');

				inc.initialize();

				inc.caller_id = current.caller_id;
// 				inc.contact_type = current.contact_type;
// 				inc.state = current.state;
// 				inc.location = current.location;
// 				inc.impact = current.impact;
// 				inc.urgency = current.urgency;
// 				inc.category = current.category;
// 				inc.subcategory = current.subcategory;
// 				inc.cmdb_ci = current.cmdb_ci;
// 				inc.assignment_group = current.assignment_group;
// 				inc.assigned_to = current.assigned_to;
// 				inc.company = current.company;
// 				inc.u_product = current.u_product;
// 				inc.u_environment = current.u_environment;
// 				inc.work_start = current.work_start;
// 				inc.work_end = current.work_end;
// 				inc.u_rca_case = current.u_rca_case;
// 				inc.short_description = current.short_description;
// 				inc.description = current.description;
// 				inc.u_sla_impact_minutes = slamins;
// 				inc.impact = current.impact;
// 				inc.urgency = current.urgency;
// 				inc.opened_by = current.opened_by;
// 				inc.close_code = current.close_code;
// 				inc.closed_at = current.closed_at;
// 				inc.close_notes = current.close_notes;
				inc.parent_incident = current.sys_id;

				inc.insert();
			}
		}
	}
	action.setRedirectURL(current);
}

Since the script seems to be failing at the incident creation we'll try to create a simple incident to see if any of the values are causing the issue.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

View solution in original post

7 REPLIES 7

Hi Ross,

I just removed some unnecessary code at the bottom of the script and commented out a lot of the fields you are trying to populate into the child incident. Can you please retest and see if it creates the simple incident.

function getCustomerList() {
	var product = g_form.getValue('u_product');
	var priority = g_form.getValue('priority');

	if (product == "AAAA" && priority == 1){
		gsftSubmit(null, g_form.getFormElement(), 'customer_list'); //MUST call the 'Action name' set in this UI Action
	}
	else {
		return false;
	}
}

//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
	createChildIncidents();

function createChildIncidents() {

	var gr = new GlideRecord('sys_journal_field');
	gr.addQuery('element_id',current.sys_id);
	gr.addQuery('value','CONTAINS','Multiple Customers Impacted;;'); //header of special note in worknotes
	//gr.setLimit(1); //set the limit to 1 if you only expect one journal entry to contain details
	gr.query();

	while (gr.next()){
		var list = gr.getValue('value');
		var splitlist = list.split(";;"); //creates customer list array that still needs to be split further
		var arrlen = splitlist.length - 1; // minus one ensure no empty record at the end of the for loop

		//the for loop parses the inner array to get the customer SLA details for the children incident
		for (var i=0; i< arrlen; i++) {

			var sla_list = splitlist.shift();
			gs.info("sla_list is " + i + ": "+sla_list);

			var arrlen2 = sla_list.length;
			gs.info("array length2 is " + arrlen2);

			var sla_data_list = sla_list.split("#");
			var cusname = sla_data_list[0];

			if(cusname != "Multiple Customers Impacted"){
				sid = sla_data_list[1];
				slamins = sla_data_list[2];

				gs.info("cusname"+ i + ": " + cusname + " sid: " + sid + " sla mins " + slamins); // Code works perfectly up to here

				gs.info("parent incident is " + current.number);
				gs.info("caller is " + current.caller_id);


				/**********Code works perfectly up to here and then script stops and no children incidents are created*************************/

				var inc = new GlideRecord('incident');

				inc.initialize();

				inc.caller_id = current.caller_id;
// 				inc.contact_type = current.contact_type;
// 				inc.state = current.state;
// 				inc.location = current.location;
// 				inc.impact = current.impact;
// 				inc.urgency = current.urgency;
// 				inc.category = current.category;
// 				inc.subcategory = current.subcategory;
// 				inc.cmdb_ci = current.cmdb_ci;
// 				inc.assignment_group = current.assignment_group;
// 				inc.assigned_to = current.assigned_to;
// 				inc.company = current.company;
// 				inc.u_product = current.u_product;
// 				inc.u_environment = current.u_environment;
// 				inc.work_start = current.work_start;
// 				inc.work_end = current.work_end;
// 				inc.u_rca_case = current.u_rca_case;
// 				inc.short_description = current.short_description;
// 				inc.description = current.description;
// 				inc.u_sla_impact_minutes = slamins;
// 				inc.impact = current.impact;
// 				inc.urgency = current.urgency;
// 				inc.opened_by = current.opened_by;
// 				inc.close_code = current.close_code;
// 				inc.closed_at = current.closed_at;
// 				inc.close_notes = current.close_notes;
				inc.parent_incident = current.sys_id;

				inc.insert();
			}
		}
	}
	action.setRedirectURL(current);
}

Since the script seems to be failing at the incident creation we'll try to create a simple incident to see if any of the values are causing the issue.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

Brent,

Thank you, so much! It works now and I removed the 'commented out' and the children tickets have the details that is expected. I appreciate your help!

Regards,

Ross

Fantastic, glad I could help. Brent