Scheduled creation of a Private Task (including a ... - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

Hi there,

While answering questions on the Community, I came across an interesting question, a question that was also asked recently within our company. "Would it be possible, to create daily cards on a Visual Task Board". Thought being, these cards would be repetitive tasks that should be handled. The community post also mentioned applying a Checklist on the task.

Would this be possible? Sure!

In this article I'll describe a short version, just using a scripted Scheduled Job. The scripted part already being done for you, and only low-code remaining. In a later article, I'll describe a longer version, using a custom app and no-code to achieve the same results.

---

What do we need…

… for the daily card creation?

- Scheduled Job 🙂 [sysauto_script]
- Existing Visual Task Board (in this example, we are using a Freeform Board)
- Task record [vtb_task]
- Visual Task Board Card [vtb_card]
- Define the Visual Task Board Lane the Card should land on

Though actually, this is not it. Ideally, you would create the Task based on a Template record.
- Template record [sys_template]

… for applying a checklist?

- Checklist Template [checklist_template]
- Checklist [checklist]
- Checklist Items [checklist_item]

Scripting the Visual Task Board Card

Adding the task consists of two things. 1) Creating the Task itself, 2) Creating the actual Visual Task Board Card. The Card holding the relation between the Task and the Visual Task Board (and which Visual Task Board Lane the Card is on).

// Define variables
var boardSysId = '<vtb_sys_id>'; // Visual Task Board
var laneSysId = '<lane_sys_id>'; // Visual Task Board Lane

// Actual code performing the job
var grTask = new GlideRecord('vtb_task');
grTask.initialize();
grTask.insert();

var grCard = new GlideRecord('vtb_card');
grCard.initialize();
grCard.setValue('task', grTask.getUniqueValue());
grCard.setValue('board', boardSysId);
grCard.setValue('lane', laneSysId);
grCard.insert();

With this code, we could already generate a daily card on the Visual Task Board. That wasn't that hard!

Applying a template on the scripted Task

In my opinion ideally, you would create the Task based on a Template. The template can be easily maintained within ServiceNow. So how do we apply a template, thru script? Do we have to query the sys_template table for this? Actually, there's a nice feature for this: "applyTemplate". Read about it on this Docs page: Scripted templates.

So we just expand our code a little bit. Adding a variable holding the Template name:

var templateStr = 'TEMPLATE2'; // Name of the Task Template

And adding applyTemplate before grTaskInsert():

grTask.applyTemplate(templateStr);

 

Adding a Checklist on the scripted Task

The toughest part of the daily Task creation with a Checklist, is the last bit, the Checklist. The actual checklist information you need is in JSON format within a Checklist Template record [checklist_template] looking something like:

{"owner":"mark.ragavan","name":"Community Test","items":[{"name":"Hamburger","order":0},{"name":"Vegie","order":1},{"name":"Chicken","order":2}]}

The part we are after is within "items". This holds all Checklist Items.

First, we just need to set up a shell template. We can already achieve this with:

var grChecklist = new GlideRecord('checklist');
grChecklist.initialize();
grChecklist.setValue('document', grTask.getUniqueValue());
grChecklist.setValue('table', 'vtb_task');
grChecklist.insert();

Again, this is just a shell template. Nothing will be shown yet on the created Task with this. The Checklist record [checklist] actually needs Checklist Item records [checklist_item]. Because the Items are defined within the JSON on the Checklist Template record, we would actually need to query the Checklist Template before creating the Checklist record as we did in the previous step.

var grTemplate = new GlideRecord('checklist_template');
grTemplate.addQuery('name', checklistStr);
grTemplate._query();

if(grTemplate._next()) {
	// The Checklist creation would actually happen within here
}

By querying the Checklist Template record, we would have the JSON available. JSON which we can parse, loop thru and create the Checklist Items on. Within the if, after the Checklist record creation, add:

for(var i = 0; i < templateObj.items.length; i++) {
	var grChecklistItem = new GlideRecord('checklist_item');
	grChecklistItem.initialize();
	grChecklistItem.setValue('checklist', grChecklist.getUniqueValue());
	grChecklistItem.setValue('name', templateObj.items[i].name);
	grChecklistItem.insert();
}

 

The full code for the Scheduled Script Execution record [sysauto_script]

Out full code would be like below. This would work fine within the Scheduled Script Execution record [sysauto_script], in the "Run this script" field.

// Define variables
var templateStr = '<template name>'; // Name of the Template record [sys_template]
var checklistStr = '<checklist name>'; // Name of the Checklist record [checklist_template]

var boardSysId = '<vtb_sys_id>'; // Visual Task Board
var laneSysId = '<lane_sys_id>'; // Visual Task Board Lane

// Actual code performing the job
var grTask = new GlideRecord('vtb_task');
grTask.initialize();
grTask.applyTemplate(templateStr);
grTask.insert();

var grCard = new GlideRecord('vtb_card');
grCard.initialize();
grCard.setValue('task', grTask.getUniqueValue());
grCard.setValue('board', boardSysId);
grCard.setValue('lane', laneSysId);
grCard.insert();

var grTemplate = new GlideRecord('checklist_template');
grTemplate.addQuery('name', checklistStr);
grTemplate._query();

if(grTemplate._next()) {
	var grChecklist = new GlideRecord('checklist');
	grChecklist.initialize();
	grChecklist.setValue('document', grTask.getUniqueValue());
	grChecklist.setValue('table', 'vtb_task');
	grChecklist.insert();
	
	var templateObj = JSON.parse(grTemplate.template);
	
	for(var i = 0; i < templateObj.items.length; i++) {
		var grChecklistItem = new GlideRecord('checklist_item');
		grChecklistItem.initialize();
		grChecklistItem.setValue('checklist', grChecklist.getUniqueValue());
		grChecklistItem.setValue('name', templateObj.items[i].name);
		grChecklistItem.insert();
	}
}

 

Low-code

With this Scheduled Script Execution record, only lines 2, 3, 5, and 6 have to be maintained in the "Run this script" field. A low-code solution. As mentioned, I will come back with an article for a custom app with a now-code solution for this.

---

And that's it actually. Hope you like it. If any questions or remarks, let me know!

👍
If this post helped you in any way, I would appreciate it if you hit bookmark or mark it as helpful.

Interested in more articles, blogs, videos, and Share projects on Visual Task Board I published?
Visual Task Board


Kind regards,
Mark

---

LinkedIn

Version history
Last update:
‎11-24-2019 10:31 PM
Updated by: