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

I've used your solution with success. One question though, how does it know where to place the checklist on the form? I mean, I have it being written to a task, in a custom section I created in form designer where I just added a Checklist formatter. But the code doesn't explicitly identify the checklist formatter in the custom section I added. So how the what's it does it know to add it there?

Gimme a clue.

Hello Eric,

The formatter is actually a UI Macro that will gather the checklist created with as document the current record and show the items of the checklist in order.

In the code you create a checklist for the current record from the template so the current record is the only key that is used here.

 

Kind Regards,

Timmy

OK,

One more question then. If I wanted to instantiate multiple checklists (a, b, c) in multiple sections (A, B, C), would this process know to push each checklist to another section? or would I just end up with one big checklist? (a, b, c) -> (A)

If you instantiate new check list b, it will remove checklist a, since there can only be one checklist per record. You can add checklist items to a checklist to create one big checklist however.

Hi Timmy,

i have used the same , but its not working . Can you please help me where am I doing wrong. I have added it in the run script of the Work flow

Template name is : Test Template

Can you please help 

Here is the script :

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", "Test 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();
}

}