Auto add checklist template to incident

Sharon24
Tera Contributor

Hi guys, 

 

I'm working on adding the checklist template to the incident automatically while the ticket is in the certain category. How do I achieve it with Business Rule? Thank you

Sharon24_0-1673836416433.png

 

Regards,

Sharon

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Sharon24 ,

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.

 

View solution in original post

2 REPLIES 2

Community Alums
Not applicable

Hi @Sharon24 ,

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.

 

MichaelZischeck
Kilo Sage

great! after one understands the "structure" it's not even that complicated... thanks a lot