How to auto generate checklists for records in ServiceNow. Checklists are useful when you don’t want a lot of extra tasks and want to remind the assigned user the steps to complete a task.
Auto Generated Checklist
Create Checklist
In order to generate a checklist, you’ll need a checklist!
In this example, I want to generate a checklist on a Catalog Task form.
Add Checklist to Form
Go to an existing catalog task
Hamburger > Configure Form Layout
Add the Checklist
Click Save
Add CHECKLIST TEMPLATE
Checklist Arrow
Might be other ways to do this, but I add a checklist this way
Go to an existing catalog task
Click the little dropdown arrow next to the checklist
Click Create New
Add some checklist items
Click the little dropdown arrow next to the checklist
Click Save as Template
Give a Template a Name, Sample Checklist
Business Rule
There are different ways to code this part. You could put it in the Workflow and also could use a script include.
I use a business rule as it is easy to test and get working.
If you were doing many different checklists for a lot of different tasks, you might consider the workflow method with a script include.
Business Rule: Generate Sample Checklist
Table: sc_task
Advanced: true
When: before
Insert: true
Filter Conditions: Request.Item is Sample Item
Script:
(function executeRule(current, previous /*null when async*/) {
var task = current.sys_id;
var table = current.getTableName();
var template = getTemplate('Sample Checklist'); //PUT YOUR CHECKLIST NAME HERE
generateChecklist(template, table, task);
})(current, previous);
function getTemplate(name) {
var grTemplate = new GlideRecord('checklist_template');
grTemplate.get('name',name);
return grTemplate.template;
}
function generateChecklist(template,table,task) {
var json = new global.JSON();
var itemJSON = json.decode(template);
var name = itemJSON['name'];
var items = itemJSON['items'];
var owner = itemJSON['owner'];
var checklistId = '';
var grList = new GlideRecord('checklist');
grList.addQuery('document', task + '');
grList.addQuery('table', table);
grList.query();
if (!grList.next()) {
grList.document = task + '';
grList.name = name;
grList.owner = owner;
grList.table = table;
checklistId = grList.insert();
for (var i = 0; i < items.length; i++) {
var grItem = new GlideRecord('checklist_item');
grItem.checklist = checklistId;
grItem.complete = false;
grItem.name = items[i]['name'];
grItem.order = items[i]['order'];
grItem.insert();
}
}
}
Open new Request
If you had the business rule to trigger on insert, you’ll need to open a new request to see it in action.