RITM must be closed-incomplete if any of its child SCTASKs are Closed-Incomplete

PatlalaShaR
Tera Contributor

Hi Team can anyone help me achieving the requirement below


RITM can be closed-complete if all of its child SCTASKs are a combination of Closed-Complete
RITM must be closed-incomplete if any of its child SCTASKs are Closed-Incomplete
A RITM should not be able to be closed without its child SCTASKs being closed


Below script i have return but its not working for the second requirement and its over riding help me with script and it has to achieve only via script

 

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

    var ritm = new GlideRecord('sc_req_item');
    if (!ritm.get(current.request_item)) {
        gs.info('RITM not found for SCTASK {0}', current.number);
        return;
    }


    var CLOSED_COMPLETE = 3;
    var CLOSED_INCOMPLETE = 4;
    if (ritm.state == CLOSED_COMPLETE || ritm.state == CLOSED_INCOMPLETE) {
        gs.info('RITM {0} already closed, skipping update', ritm.number);
        return;
    }


    var task = new GlideRecord('sc_task');
    task.addQuery('request_item', ritm.sys_id);
    task.query();

    var hasIncomplete = false;
    var allClosed = true;
    var allComplete = true;

    while (task.next()) {
        if (task.active == true) {
            allClosed = false;
        }
        if (task.state == CLOSED_INCOMPLETE) {
            hasIncomplete = true;
        }
        if (task.state != CLOSED_COMPLETE) {
            allComplete = false;
        }
    }


    if (allClosed) {
        if (hasIncomplete) {
            ritm.state = CLOSED_INCOMPLETE;
        } else if (allComplete) {
            ritm.state = CLOSED_COMPLETE;
        }
        ritm.update();
        gs.info('Updated RITM {0} to state {1}', [ritm.number, ritm.state]);
    } else {
        gs.info('RITM {0} not updated: not all tasks closed', ritm.number);
    }
})(current, previous);
 

 

7 REPLIES 7

Nilesh Pol
Tera Guru

@PatlalaShaR 

Verify with following two:

 

1. After Update Business Rule on sc_task

Purpose: Automatically update the RITM's state based on the states of its child SCTASKs whenever a SCTASK is updated.

Configuration:

  • Table: sc_task

  • When: After

  • Conditions:

    • State changes to one of: Closed Complete, Closed Incomplete, Closed Skipped

(function executeRule(current, previous /*null when async*/) {
// Retrieve the associated RITM
var ritm = new GlideRecord('sc_req_item');
if (!ritm.get(current.request_item)) {
gs.info('RITM not found for SCTASK {0}', current.number);
return;
}

// Define state values
var CLOSED_COMPLETE = 3;
var CLOSED_INCOMPLETE = 4;

// If RITM is already closed, no action is needed
if (ritm.state == CLOSED_COMPLETE || ritm.state == CLOSED_INCOMPLETE) {
gs.info('RITM {0} already closed, skipping update', ritm.number);
return;
}

// Query all SCTASKs associated with the RITM
var task = new GlideRecord('sc_task');
task.addQuery('request_item', ritm.sys_id);
task.query();

var hasIncomplete = false;
var allClosed = true;
var allComplete = true;

while (task.next()) {
if (task.active == true) {
allClosed = false;
}
if (task.state == CLOSED_INCOMPLETE) {
hasIncomplete = true;
}
if (task.state != CLOSED_COMPLETE) {
allComplete = false;
}
}

// Update RITM state based on SCTASKs' states
if (allClosed) {
if (hasIncomplete) {
ritm.state = CLOSED_INCOMPLETE;
} else if (allComplete) {
ritm.state = CLOSED_COMPLETE;
}
ritm.update();
gs.info('Updated RITM {0} to state {1}', [ritm.number, ritm.state]);
} else {
gs.info('RITM {0} not updated: not all tasks closed', ritm.number);
}
})(current, previous);

 

2. Before Update Business Rule on sc_req_item

Purpose: Prevent the RITM from being closed if any associated SCTASKs are still open.

Configuration:

  • Table: sc_req_item

  • When: Before

  • Conditions:

    • State changes to Closed Complete

(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord("sc_task");
gr.addActiveQuery();
gr.addQuery("request_item", current.sys_id);
gr.setLimit(1);
gr.query();
if (gr.hasNext()) {
gs.addErrorMessage("Please close all SCTASKs before closing the RITM.");
current.setAbortAction(true);
}
})(current, previous);

 

Hi Nilesh, Thanks for your response by using above script few are working as expected but for few items when i make sc task state to closed incomplete the ritm should also closed incomplete but the state is closed complete can u plz help how to make it possible for all the items

@PatlalaShaR  then use following updated business rule that ensures the RITM's state accurately reflects the states of its associated SCTASKs. table -  sc_task

(function executeRule(current, previous /*null when async*/) {
var ritm = new GlideRecord('sc_req_item');
if (!ritm.get(current.request_item)) {
gs.info('RITM not found for SCTASK {0}', current.number);
return;
}

// Define state values
var CLOSED_COMPLETE = 3;
var CLOSED_INCOMPLETE = 4;

// If RITM is already closed, no action is needed
if (ritm.state == CLOSED_COMPLETE || ritm.state == CLOSED_INCOMPLETE) {
gs.info('RITM {0} already closed, skipping update', ritm.number);
return;
}

// Query all SCTASKs associated with the RITM
var task = new GlideRecord('sc_task');
task.addQuery('request_item', ritm.sys_id);
task.query();

var hasIncomplete = false;
var allClosed = true;
var allComplete = true;

while (task.next()) {
if (task.active == true) {
allClosed = false;
}
if (task.state == CLOSED_INCOMPLETE) {
hasIncomplete = true;
}
if (task.state != CLOSED_COMPLETE) {
allComplete = false;
}
}

// Update RITM state based on SCTASKs' states
if (allClosed) {
if (hasIncomplete) {
ritm.state = CLOSED_INCOMPLETE;
} else if (allComplete) {
ritm.state = CLOSED_COMPLETE;
}
ritm.update();
gs.info('Updated RITM {0} to state {1}', [ritm.number, ritm.state]);
} else {
gs.info('RITM {0} not updated: not all tasks closed', ritm.number);
}
})(current, previous);

 

Hi Nilesh, 
Its still the same for few flows its not working as expected initially state changes to incomplete within seconds the state changes to closed complete there is over-riding can you help me out like how to overcome this over ride?