How to automatically add a saved checklist to a task?

robinsnow
Tera Contributor

Hi guys,

Really trying to work something out in order to add a checklist automatically on a Task when it is created.

Tried with business rule since I did not know how to do it in the workflow.

(function executeRule(current, previous /*null when async*/) {
	
	// Query checklist
	
	var mcl = new GlideRecord('checklist_template');
	mcl.addQuery('name','CONTAINS', "The best template");
	mcl.query();
	
	// set checklist items
	if(mcl.next()){
	var myListItem = new GlideRecord('sc_task');
	myListItem.addEncodedQuery('request_item.cat_item=c053f28737703e00143ed2e843990ea4^state!=3^ORstate=NULL');
	myListItem.orderByDesc('sys_created_on');
	myListItem.setLimit(1);
	myListItem.setValue('checklist', mcl.getValue('template'));
	myListItem.insert();
	}
	
})(current, previous);
1 ACCEPTED SOLUTION

timmyweytjens
Tera Expert

 Hello Robin,

I use this code in the workflow catalog task to add a checklist template        

 var theList = new GlideRecord("checklist");
 theList.initialize();
 theList.table = "sc_task";
 theList.owner = gs.getUserID();
 theList.document = task.setNewGuid();
 var listId = theList.insert();

var checklistArr = {};

var grTemplate = new GlideRecord("checklist_template");
grTemplate.addQuery("name", "The best template");
grTemplate.query();
if(grTemplate.next())
{
         checklistArr = JSON.parse(grTemplate.getValue('template'));
 
         //Loop through template and create checklist
         for(var key in checklistArr.items)
         {
               var chki = new GlideRecord('checklist_item');
               chki.initialize();
               chki.setValue('checklist', listId);
               chki.setValue('name', checklistArr.items[key]['name']);
               chki.setValue('order', checklistArr.items[key]['order']);
               chki.insert();
          }

}

 

View solution in original post

10 REPLIES 10

timmyweytjens
Tera Expert

 Hello Robin,

I use this code in the workflow catalog task to add a checklist template        

 var theList = new GlideRecord("checklist");
 theList.initialize();
 theList.table = "sc_task";
 theList.owner = gs.getUserID();
 theList.document = task.setNewGuid();
 var listId = theList.insert();

var checklistArr = {};

var grTemplate = new GlideRecord("checklist_template");
grTemplate.addQuery("name", "The best template");
grTemplate.query();
if(grTemplate.next())
{
         checklistArr = JSON.parse(grTemplate.getValue('template'));
 
         //Loop through template and create checklist
         for(var key in checklistArr.items)
         {
               var chki = new GlideRecord('checklist_item');
               chki.initialize();
               chki.setValue('checklist', listId);
               chki.setValue('name', checklistArr.items[key]['name']);
               chki.setValue('order', checklistArr.items[key]['order']);
               chki.insert();
          }

}

 

This worked very well!! Thank you Timmy

Hello Timmy,

We upgraded to London and the task is now being completed by itself. Have you experienced this?

Kind regards,

Robin

Hello Robin,

 

No, in our customer instances it is still working as expected.

 

Kind Regards,

Timmy Weytjens