Move & Close RCA PTASK when Problem Closed As Duplicate

Ben28
Tera Contributor

I'm using the OoTB business rule, "Cascade closure of Problem Tasks" to move PTASKs from a duplicate Problem, over to the original Problem when the duplicate closes. However, currently this results in having multiple RCA PTASKS, of which we only want 1, so I wanted to close the RCA PTASK at the same time as moving it. I tried using the below code, before the move action, but it doesn't seem to be working. The PTASK moves over but is never closed. 

// Check if PTASK is of type 'rca' and close it before moving
if (current.type == 'rca') {
current.state = ProblemTaskState.CLOSED; // or use the appropriate numeric value
current.update(); // Persist the state change
gs.debug("PTASK of type 'rca' automatically closed during problem duplicate movement.");
}

Also tried the below JavaScript, in the same BR, which also fails to close the RCA PTASK.

if (recordsMap.hasOwnProperty('problem_task')) {
        var ptaskGr = new GlideRecord('problem_task');
        ptaskGr.addQuery('problem', current.sys_id);
        ptaskGr.addQuery('type', 'rca'); // assuming 'type' is a choice field or string
        ptaskGr.addQuery('state', '!=', '3'); // assuming 3 = Closed; adjust if needed
        ptaskGr.query();
        while (ptaskGr.next()) {
            ptaskGr.state = '3'; // Set to Closed; use actual state ID as needed
            ptaskGr.work_notes = 'Automatically closed because this problem was marked as a duplicate.';
            ptaskGr.update();
            gs.debug("Closed PTASK " + ptaskGr.number + " of type 'rca' before moving.");
        }
    }

What am I missing? I've double checked the closure code - CLOSED is 3, the RCA PTASK type value is 'rca' (lowercase), I tried setting "current.state = 3;" instead of "current.state = ProblemTaskState.CLOSED;".

 

It's probably something simple I'm missing, but I can't for the life of me see what!

 

The OoTB business rule "Cascade closure of Problem Tasks" is below for awareness. I was originally going to modify B.R. "Copy related records to original Problem", but apparently "The records for problem_task(s) are handled via the BR - 'Cascade closure of Problem Tasks' - DEF0190002". 

var bypassCanCreatePTaskCheck; // Adding this var to pass a flag to subsequent BR to relax its check if current BR is originator.
(function executeRule(current, previous /*null when async*/ ) {

// Applies only on Ptask movement to a) Open (Non-closed) state target problem b) Closed state problem. The below if kicks in only in
// cases of duplicate marking. - DEF0190002
if (!current.duplicate_of.nil() && current.resolution_code + '' === 'duplicate') {
// check if Ptask movement is indeed specified on Problem properties else ignore
var problemUtil = new ProblemV2Util();
var recordsToMove = gs.getProperty("problem.duplicate.records_to_move");
var recordsMap = problemUtil.dotCommaToMap(recordsToMove.split(","));
if (recordsMap.hasOwnProperty('problem_task') && recordsMap['problem_task'] + '' === 'problem') {
bypassCanCreatePTaskCheck = true;
var originalProblemGr = current.duplicate_of.getRefRecord();

// ** New code was going here **

// Only close if Target problem is closed and property for cancelling is enabled.
if (originalProblemGr.state + '' === ProblemState.STATES.CLOSED &&
gs.getProperty("problem.closed.cancel_open_tasks") == "true") {
gs.debug("Initiating moveAndCloseRecords from BR 'Cascade closure of Problem Tasks' bypassCanCreatePTaskCheck set to True");
problemUtil.moveAndCloseRecords('problem_task', 'problem', current, originalProblemGr);
} else {
problemUtil.moveRecords('problem_task', 'problem', current, originalProblemGr);
}
} else {
// OOTB we are shipping this as enabled
gs.debug("Skipping movement of PTasks as Problem properties does not have Ptask movement enabled");
}
}

// Retained as-is for Cases of simple closure and not duplicate marking.
if (gs.getProperty("problem.closed.cancel_open_tasks") == "true") {
var PRB_TASK_CLOSED_STATE = ProblemTaskState.States.CLOSED;
var problemTaskGr = new GlideRecord("problem_task");
problemTaskGr.addQuery("problem", current.getUniqueValue());
problemTaskGr.addActiveQuery();
problemTaskGr.setValue("state", PRB_TASK_CLOSED_STATE);
problemTaskGr.setValue("close_code", "canceled");
problemTaskGr.setValue("close_notes", gs.getMessage("Problem Task is Canceled based on closure of {0}.", current.getDisplayValue()));
problemTaskGr.updateMultiple();
}
})(current, previous);

 

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Ben28 

try this

// Check if PTASK is of type 'rca' and close it before moving
if (current.type == 'rca') {
    current.state = 3; // Assuming 3 is the numeric value for Closed
    current.update(); // Persist the state change
    gs.debug("PTASK of type 'rca' automatically closed during problem duplicate movement.");
}

// Additional code to handle multiple PTASKs
if (recordsMap.hasOwnProperty('problem_task')) {
    var ptaskGr = new GlideRecord('problem_task');
    ptaskGr.addQuery('problem', current.sys_id);
    ptaskGr.addQuery('type', 'rca'); // assuming 'type' is a choice field or string
    ptaskGr.addQuery('state', '!=', '3'); // assuming 3 = Closed; adjust if needed
    ptaskGr.query();
    while (ptaskGr.next()) {
        ptaskGr.state = '3'; // Set to Closed; use actual state ID as needed
        ptaskGr.work_notes = 'Automatically closed because this problem was marked as a duplicate.';
        ptaskGr.update();
        gs.debug("Closed PTASK " + ptaskGr.number + " of type 'rca' before moving.");
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Thanks for the reply.

Which BR should I be putting this in? Seems there are 2 than handle moving PTASKS: Copy related records to original Problem & Cascade closure of Problem Tasks. The former does have a comment line in it, that points to the latter, so I assumed it should be the casade BR?

Went ahead and added it to BR "Cascade closure of Problem Tasks", but didn't work. The PTASK moved across in Assess state.