Checklist template in Incident Template

adri
Giga Contributor

Hello!

I am looking to create an Incident template that already contains a checklist template.

We are planning to schedule this incident template to be created once a day, and we are looking to have this checklist already in the incident when created.

I am trying directly in the Incident Templates configuration but does not allow me to choose the checklist template.

Do you have any idea? Thank you!

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

There's no way to do this out-of-box (because of the issue you've identified), but I've seen several questions about this so I decided to come up with a solution.  Here's how you can accomplish this...

1)  Create a new Script Include by navigating to 'System Definition -> Script Includes' in your left nav.  We use a Script Include because ServiceNow doesn't ship with a way (or at least to-date hasn't communicated a way) to apply a checklist template so we need to create our own function.  Your Script Include should have the following settings...

Name: u_ChecklistUtil

Description: 

Custom Checklist Utils

Usage:
var cu = new u_ChecklistUtil();
cu.applyChecklistTemplate(<GlideRecord>, <Checklist Template SysID OR Name>);

Example applying a checklist template with a standard template:
var rec = new GlideRecord("incident");
rec.initialize();
rec.applyTemplate("My Standard Template");
rec.insert();

var cu = new u_ChecklistUtil();
cu.applyChecklistTemplate(rec, "My Checklist Template");

Script:

var u_ChecklistUtil = Class.create();
u_ChecklistUtil.prototype = {
	initialize: function() {
	},
	
	applyChecklistTemplate : function(record, templateID) {
		// Get the template GlideRecord if available
		var templateRec = this.getTemplate(templateID);
		
		// If we found one
		if (templateRec) {
			// Remove any existing checklists associated with the record
			this.removeChecklist(record);
			
			// Parse the JSON string in the template record
			var parser = new JSONParser();
			var templateObj = parser.parse(templateRec.template);
			
			// Create and associate a new checklist based on the template
			var newChecklist = new GlideRecord('checklist');
			newChecklist.initialize();
			newChecklist.table = record.getTableName();
			newChecklist.document = record.sys_id;
			newChecklist.name = templateRec.name;
			newChecklist.owner = templateObj.owner;
			newChecklist.insert();
			
			for (var i=0; i < templateObj.items.length; i++) {
				// Add the checklist items
				var checkItem = new GlideRecord('checklist_item');
				checkItem.initialize();
				checkItem.checklist = newChecklist.sys_id;
				checkItem.name = templateObj.items[i].name;
				checkItem.order = templateObj.items[i].order;
				checkItem.insert();
			}
		}
	},
	
	getTemplate : function(templateID) {
		var ct = new GlideRecord('checklist_template');
		ct.addQuery('sys_id', templateID).addOrCondition('name', templateID);
		ct.query();
		if (ct.next())
			return ct;
		return false;
	},
	
	removeChecklist : function(record) {
		var ck = new GlideRecord('checklist');
		ck.addQuery('document', record.sys_id);
		ck.query();
		ck.deleteMultiple();
	},
	
	type: 'u_ChecklistUtil'
};

 

As for scheduling the creation of the template, you'll see an example of that in the description of the Script Include.  Again, we need to use our own custom scripting to generate a record from a template (we can at least use ServiceNow's built-in 'applyTemplate' for this) and then generate the checklist.  When you create your scheduled job, instead of selecting the 'Automatically generate something' option, select the 'Automatically run a script of your choosing' option.  You can supply the example script from the Script Include description there and modify as-needed for your table and template names.

Good luck, I'll be interested to hear how this works for you!

View solution in original post

4 REPLIES 4

Mark Stanger
Giga Sage

There's no way to do this out-of-box (because of the issue you've identified), but I've seen several questions about this so I decided to come up with a solution.  Here's how you can accomplish this...

1)  Create a new Script Include by navigating to 'System Definition -> Script Includes' in your left nav.  We use a Script Include because ServiceNow doesn't ship with a way (or at least to-date hasn't communicated a way) to apply a checklist template so we need to create our own function.  Your Script Include should have the following settings...

Name: u_ChecklistUtil

Description: 

Custom Checklist Utils

Usage:
var cu = new u_ChecklistUtil();
cu.applyChecklistTemplate(<GlideRecord>, <Checklist Template SysID OR Name>);

Example applying a checklist template with a standard template:
var rec = new GlideRecord("incident");
rec.initialize();
rec.applyTemplate("My Standard Template");
rec.insert();

var cu = new u_ChecklistUtil();
cu.applyChecklistTemplate(rec, "My Checklist Template");

Script:

var u_ChecklistUtil = Class.create();
u_ChecklistUtil.prototype = {
	initialize: function() {
	},
	
	applyChecklistTemplate : function(record, templateID) {
		// Get the template GlideRecord if available
		var templateRec = this.getTemplate(templateID);
		
		// If we found one
		if (templateRec) {
			// Remove any existing checklists associated with the record
			this.removeChecklist(record);
			
			// Parse the JSON string in the template record
			var parser = new JSONParser();
			var templateObj = parser.parse(templateRec.template);
			
			// Create and associate a new checklist based on the template
			var newChecklist = new GlideRecord('checklist');
			newChecklist.initialize();
			newChecklist.table = record.getTableName();
			newChecklist.document = record.sys_id;
			newChecklist.name = templateRec.name;
			newChecklist.owner = templateObj.owner;
			newChecklist.insert();
			
			for (var i=0; i < templateObj.items.length; i++) {
				// Add the checklist items
				var checkItem = new GlideRecord('checklist_item');
				checkItem.initialize();
				checkItem.checklist = newChecklist.sys_id;
				checkItem.name = templateObj.items[i].name;
				checkItem.order = templateObj.items[i].order;
				checkItem.insert();
			}
		}
	},
	
	getTemplate : function(templateID) {
		var ct = new GlideRecord('checklist_template');
		ct.addQuery('sys_id', templateID).addOrCondition('name', templateID);
		ct.query();
		if (ct.next())
			return ct;
		return false;
	},
	
	removeChecklist : function(record) {
		var ck = new GlideRecord('checklist');
		ck.addQuery('document', record.sys_id);
		ck.query();
		ck.deleteMultiple();
	},
	
	type: 'u_ChecklistUtil'
};

 

As for scheduling the creation of the template, you'll see an example of that in the description of the Script Include.  Again, we need to use our own custom scripting to generate a record from a template (we can at least use ServiceNow's built-in 'applyTemplate' for this) and then generate the checklist.  When you create your scheduled job, instead of selecting the 'Automatically generate something' option, select the 'Automatically run a script of your choosing' option.  You can supply the example script from the Script Include description there and modify as-needed for your table and template names.

Good luck, I'll be interested to hear how this works for you!

Thank you!

It worked! 🙂

Thanks for posting this!  We wanted to script in extra checklist items based on specific information of a record and this got me straight there.

If anyone is curious.  From wherever you need to get your extra checklist items (record producer, workflow, whatever server side script), just put a JSON object in an array and send it to the script include.  Use "name" and "order".

Edit 2020/12/09: I ran into some problems with just trying to cram the extra items into the templateObj.  I've updated the below to loop through them instead.

var checkListItems = [];
//Scripty query lookup stuff happens...
while(scriptyQuery.next()){
checkListItems.push(JSON.stringify({
        "name": "The caller is a home router user.  Do the thing about the Active Directory group.",
        "order": 2
    }));
}

var cu = new u_ChecklistUtil();
cu.applyChecklistTemplate(wom, 'Hardware Refresh', checkListItems);​

Then in u_ChecklistUtil(), add a third object to applyChecklistTemplate function.

applyChecklistTemplate : function(record, templateID, extraItems)

Then just before the templateObj.items for loop, add a loop to add the items into the templateObj.

if (extras.length > 0) { 
                for (var k = 0; k < extras.length; k++) {
                    templateObj.items.push(JSON.parse(extras[k]));
                }
            }

Now your scripted items will appear in your template.

 

We used this on Work Orders for a specific template to flag home router asset users to make sure an extra fulfillment step got done.  Since this is a case-by-case basis obviously a static checklist wouldn't work (unless the list item was like, "check if they have a home router").

ty_roach
Tera Guru

The problem with the OOB checklists is that they are unreliable, meaning, people can change the content of the checklist item after the fact, add items, delete items, change items.  There's also no guarantee that the same checklist will appear for same situation.  There is no way to restrict who can edit the checklist or when it can be edited (like do you really want people changing checklist responses after the record has been closed).

Enter Checklist Pro.  We built Checklist Pro to solve all those problems and more.  Checklist Pro application administrators can define when checklists get created and associated with records in a table (any table - not just those that extend TASK), when these checklists can be edited, when the associated record is considered "closed" (and thus should prohibit further checklist updates).  We even added the ability to define "Required" checklist items that allow enforcement, thereby preventing a record from closing unless the required items are completed.  We've built convenience Database Views to go with the most common checklist tables, which include TASK, SYSAPPROVAL_APPROVER, CMDB_CI.

They work in the Service Portal as well as in the Classic UI.

They'll work with other Scoped Applications (like HR or SecOps or even custom scoped apps & tables).

For more information contact TyGR LLC or goto our youtube channel to see it in action.