How to clone task with a checklist?

Kevin Meier
Mega Contributor

I have been struggling with this script for too long.  My task and the checklist are cloned just fine, but the query for the checklist items never returns anything.  I have added logging and the correct sys_ids are being passed.  When I view the checklist_item table in a listview, the checklist field is empty for all records, however, if I view the checklist, the correct related items are shown.

var ot = new GlideRecord('u_ot');
var recordToClone = '84e213c80f2672003436807be1050e48';
if (ot.get(recordToClone)) {
     ot.number = '';
     if (ot.insert()) {
          CloneCheckList(recordToClone, ot.sys_id);
     }
}

function CloneCheckList(originalTask, newTask) {
     var chkLst = new GlideRecord('checklist');
     chkLst.addQuery('document',originalTask);
     chkLst.query();
     if (chkLst.next()) {
          chkLst.number = '';
          chkLst.document = newTask; // newly inserted task
          if (chkLst.insert()) {
               CloneCheckListItems(recordToClone, chkLst.sys_id);
          }
     }
}

function CloneCheckListItems(originalChecklist, newCheckList) {
     var chkLstItem = new GlideRecord('checklist_item');
     chkLstItem.addQuery('checklist',originalChecklist);
     chkLstItem.query();   // never returns anything
     while (chkLstItem.next()) {
          chkLstItem.checklist = newCheckList;
          chkLstItem.complete = false;
          chkLstItem.insert();
     }
}

What I would rather do, is clone the Task and then create a checklist from a template, but I haven't been able to figure that out either.

 

1 ACCEPTED SOLUTION

Michael Fry1
Kilo Patron

We created a Template and applied the Template to the Create Task workflow activity. So when the task is created, the template is populated. Then using a before business rule, we're using the following script:

(function executeRule(current, previous /*null when async*/) {
	
	var checklist = '';
	
	//checklist = '38a48bc4dba59f002bd8f33eae9619ce';
	
	var temp = current.template;
	var cklist = temp.checklist_template;
	//gs.log('what is cklist = '+cklist);
	
	var getTemplate = new GlideRecord('checklist_template');
	getTemplate.addQuery('sys_id', cklist);
	getTemplate.query();
	if(getTemplate.next()) {
		var itemJSON = new JSON().decode(getTemplate.template);
		var name = itemJSON['name'];
		var items = itemJSON['items'];
		var owner = itemJSON['owner'];
	}
	
	//var table = current.getTableName();
	var checklistId = '';
	var list = new GlideRecord('checklist');
	list.name = name;
	list.document = current.sys_id;
	list.table = 'sn_sm_finance_task';
	list.insert();
	
	
	// create checklist items
	for (var i = 0; i < items.length; i++) {
		var item = new GlideRecord('checklist_item');
		item.checklist = list.sys_id;
		item.complete = false;
		item.name = items[i]['name'];
		item.order = items[i]['order'];
		item.insert();
	}
})(current, previous);

Maybe this will help switching you to templates.

View solution in original post

6 REPLIES 6

Michael Fry1
Kilo Patron

We created a Template and applied the Template to the Create Task workflow activity. So when the task is created, the template is populated. Then using a before business rule, we're using the following script:

(function executeRule(current, previous /*null when async*/) {
	
	var checklist = '';
	
	//checklist = '38a48bc4dba59f002bd8f33eae9619ce';
	
	var temp = current.template;
	var cklist = temp.checklist_template;
	//gs.log('what is cklist = '+cklist);
	
	var getTemplate = new GlideRecord('checklist_template');
	getTemplate.addQuery('sys_id', cklist);
	getTemplate.query();
	if(getTemplate.next()) {
		var itemJSON = new JSON().decode(getTemplate.template);
		var name = itemJSON['name'];
		var items = itemJSON['items'];
		var owner = itemJSON['owner'];
	}
	
	//var table = current.getTableName();
	var checklistId = '';
	var list = new GlideRecord('checklist');
	list.name = name;
	list.document = current.sys_id;
	list.table = 'sn_sm_finance_task';
	list.insert();
	
	
	// create checklist items
	for (var i = 0; i < items.length; i++) {
		var item = new GlideRecord('checklist_item');
		item.checklist = list.sys_id;
		item.complete = false;
		item.name = items[i]['name'];
		item.order = items[i]['order'];
		item.insert();
	}
})(current, previous);

Maybe this will help switching you to templates.

Hi Michael,

Thanks for this.

Can you please advise how to make the checklist mandatory on resolve on an incident?

I have created a client script which is working but it is making ALL updates mandatory to check the boxes. 

I only want the boxes to be required on resolve not on update. 

Client script:

function onSubmit() {
	// Check to make sure checklist is complete before continuing
	var ci = new GlideRecord('checklist_item');
	ci.addQuery('checklist.document', g_form.getUniqueValue());
	ci.addQuery('complete', false);
	ci.query(); // Must be a synchronous call because this is an 'onSubmit' script
	if (ci.next()) {
		alert('All checklist items must be completed before this ticket is resolved.');
		return false;
	}
}

Kevin Meier
Mega Contributor

Thanks Michael.  That solution worked great.