logo

NJP

Checklists are cool

Import · Oct 21, 2019 · article

Checklists are cool but they're horribly underutilised. They're a great way of reducing the number of tasks involved in a process and can be set up from Flows.

image

Here's the code for a checklist utility that can be called from business rules, UI actions and Flows. The two functions are:

  • Create (pass the sys_id for the record and a comma separated list of your checklist items)
  • isComplete (pass the sys_id for the record to see if the checklist has been completed)
var checklistHelper = Class.create();
checklistHelper.prototype = {
    initialize: function() {
    },

    //////////////////////////////////////////////
    create: function(table, sys_id, items){

        var checklistID;
        var check = new GlideRecord('checklist');
        check.setWorkflow(false);

        if (!check.get('document', sys_id)) {
            check.document = sys_id;
            check.table = table;
            checklistID = check.insert();
        }else{
            checklistID = check.sys_id.toString();
        }

        for (var i = 0; i < items.length; i++) {
            var checklistItem = new GlideRecord('checklist_item');
            checklistItem.setWorkflow(false);
            checklistItem.initialize();
            checklistItem.checklist = checklistID;
            checklistItem.complete = false;
            checklistItem.name = items[i];
            checklistItem.order = i;
            checklistItem.insert();
        }

    },

    //////////////////////////////////////////////
    isComplete: function(sys_id){

        var checklistComplete = true;

        var check = new GlideRecord('checklist');

        if(check.get('document', sys_id)) {

            var checklistItem = new GlideRecord('checklist_item');
            checklistItem.query('checklist',check.sys_id);

            while(checklistItem.next()){
                if(!checklistItem.complete){checklistComplete = false;}
            }   
        }

        return checklistComplete;
    },
    type: 'checklistHelper'
}; 

Then from a business rule, it's really easy to check to see if someone has completed their checklist.

(function executeRule(current, previous /*null when async*/) {

    var myCheckList = new checklistHelper();
    var checklistFinished = myCheckList.isComplete(current.sys_id.toString());

    if(!checklistFinished){
        current.setAbortAction(true);
        gs.addErrorMessage('You cannot close this task until the checklist is complete');
    }

})(current, previous);

Now we've got our code sitting in a Script Includes we can use it anywhere, including in Flows. So here's a custom action that was added to allow a checklist to be added to any record in a flow.

image

Then when you create tasks in a Flow you can quickly and easily add a checklist to the tasks you're creating along the way.

image

I've included the flow action and all of this code in the attached update set (JDS) Checklists, so try it out in your development instance.

Have fun

View original source

https://www.servicenow.com/community/now-platform-articles/checklists-are-cool/ta-p/2309563