Project tasks and child task with checklist

Aishwarya Pulas
Tera Contributor

Hi , so lets consider L1 as parent project task(task under project) and L2 as child project task(task under L1) now  I have to implement a scenario where

a)L1 state is not allowed to closed complete if there are incomplete checklist under itself or under its L2

b)Similarly L2 is not allowed to closed complete if there are incomplete checklist under itself or under its L1

 

To achieve the above I have written an befor update BR with script but it was not working as expected at order 100 as some other BR was cutting it hence changed the order to 910 and it works now.

 

But the problem is ,everytime state changes to closed complete on L2 bec of Out of box rollup it tries changing L1 to closed complete too bec of which my BR runs twice and parents error appear on child task .

Also get duplicate errors .I have tried finding differentiator between l1 and l2 but that does not stop the double runing

Any solution to handle this situation. Kindly help.

(function executeRule(current, previous /*null when async*/ ) {
    var parent = current.parent.parent;
    gs.log("parentash " + parent);
    var ptaskno = current.number;

    if (parent) {
        gs.log("entered not empty");
        // Actions for 'pm_project'
        if (new projectTaskServerUtil().checkOpenCheckList(current.sys_id) == true) {
            current.work_start = previous.work_start;
            current.work_end = previous.work_end;
            current.work_duration = previous.work_duration;
            current.percent_complete = previous.percent_complete;
            current.state = previous.state;

            gs.addErrorMessage("Please close all checklists on " + ptaskno + " before closing.");
        }
    } else if (!parent) {
        gs.log("entered  empty");
        // Actions for 'pm_project_task'
        if (new projectTaskServerUtil().checkOpenCheckList(current.sys_id) == true) {
            current.work_start = previous.work_start;
            current.work_end = previous.work_end;
            current.work_duration = previous.work_duration;
            current.percent_complete = previous.percent_complete;
            current.state = previous.state;

            gs.addErrorMessage("Please close all checklists on " + ptaskno + " before closing.");
        }
    }
})(current, previous);

 

 

3 REPLIES 3

Aishwarya Pulas
Tera Contributor

Pating the SCript Include method used for details:

 checkOpenCheckList: function(taskSysID) {
        var chklistGR = new GlideRecord('checklist');
        chklistGR.addQuery('document', taskSysID); //task sys id
        chklistGR.query();
        if (chklistGR.next())
            return this._checkIfChecklistItemIsCompleted(chklistGR.sys_id);
        else
            return false;
    },

    _checkIfChecklistItemIsCompleted: function(checkListSysID) {

        var chkListItemGR = new GlideRecord('checklist_item');
        chkListItemGR.addQuery("checklist", checkListSysID);
        chkListItemGR.addQuery("complete", false);
        chkListItemGR.query();
        if (chkListItemGR.next())
            return true;
        else
            return false;
    },

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Aishwarya Pulas ,

 

Can u try below code plz

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

    // Check if the Business Rule should run based on the record state

    if (current.state != 'Closed Complete') {

        var parent = current.parent.parent;

        var ptaskno = current.number;

 

        if (parent) {

            // Actions for 'pm_project'

            if (new projectTaskServerUtil().checkOpenCheckList(current.sys_id)) {

                // Temporarily update the record to prevent the Business Rule from running again

                current.setWorkflow(false); // Disable workflow to prevent unnecessary triggers

                current.state = previous.state; // Restore the previous state

                current.update();

                current.setWorkflow(true); // Re-enable workflow

 

                gs.addErrorMessage("Please close all checklists on " + ptaskno + " before closing.");

            }

        } else if (!parent) {

            // Actions for 'pm_project_task'

            if (new projectTaskServerUtil().checkOpenCheckList(current.sys_id)) {

                // Temporarily update the record to prevent the Business Rule from running again

                current.setWorkflow(false); // Disable workflow to prevent unnecessary triggers

                current.state = previous.state; // Restore the previous state

                current.update();

                current.setWorkflow(true); // Re-enable workflow

 

                gs.addErrorMessage("Please close all checklists on " + ptaskno + " before closing.");

            }

        }

 

    }

})(current, previous);

 

Thanks,

Danish

 

Aishwarya Pulas
Tera Contributor

@Danish Bhairag2 

So , the running of the BR twice stopped when I used the Abort Action .But I aslso want to redirect them to the record with error.  But I see in few articles that combination of abortAction and gs.rediect dont work. Any workaround ? 

Business Rule : 

(function executeRule(current, previous /*null when async*/ ) {
    var ptaskname = current.number;
    var ptaskno = current.sys_id;
    var tablename = current.getTableName();

    if (new projectTaskServerUtil().checkOpenCheckList(current.sys_id) == true) {
        current.work_start = previous.work_start;
        current.work_end = previous.work_end;
        current.work_duration = previous.work_duration;
        current.percent_complete = previous.percent_complete;
        gs.eventQueue('parent_projecttask_redirection', current, ptaskno,tablename);
        gs.addErrorMessage("Please close all checklists on " + ptaskname + " before closing.");
        current.setAbortAction(true);
    }
})(current, previous);                                                                                                                                                                    
Script action that executes on event trigger :                                          
projecterrorredirection();

function projecterrorredirection() {

    gs.log("trigerred");
    var taskNumber = event.parm1;
    var tableName = event.parm2;

    gs.log("paramsash " + taskNumber + tableName);

    // Perform actions based on the event parameters
    var redirectURL = '/' + tableName + '.do?sys_id=' + taskNumber;
    gs.log("urlash " + redirectURL);
    gs.log("Redirection is happening...");
    gs.setRedirect(redirectURL);
}
Its logging the correct url , but no redirection happens