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

Brent Sutton
Mega Sage

Are you running this in a client script?

As a UI action

Hi Ross,

I have no real way to test your script logic but it appears that you are trying to access the "current" object from the client side part of your UI Action. Consider configuring your UI Action based on the following screenshot:

UI Action Configuration

find_real_file.png

UI Action Script

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
		if(Array.isArray(splitlist)){
			gs.info("splitlist is an array");
		}

		gs.info("splitlist: " + splitlist);

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

		gs.info("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();

			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("#");

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

			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();


				if(current.approver == '') {
					gs.addErrorMessage(gs.getMessage('Select an approver to review this contract'));
					action.setRedirectURL(current);
				} else {
					current.substate = 'under_review';
				}

				current.update();
			}
		}
	}
}

Let me know how you get along.

Brent

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

Thanks for the help! Although, the script still does not work. No children incident tickets are being created and attached to the parent incident.