Need to set state of RITM based on sc task

kun1
Tera Expert

Hi Team,

 

Suppose i have a RITM which have more than 1 task and i need to change the state of RITM based on sc task .

if any of task move to WIP then state of RITM move to WIP

If any of the task move to pending then state of RITM move to pending

When all task are close complete then only state change to close complete of RITM 

When state of any task changes to closed skipped or incomplete then state of RITM changed to close incomplete .

 

How to achieved this using Script?

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@kun1 

Create a Business Rule on the sc_task table
Trigger Conditions: Set it to run on update when the state field changes.
When to Run: Use After.
Filter Conditions: State Changes

 

(function executeRule(current, previous /*null when async*/) {
    // Get the parent RITM (Requested Item)
    var ritm = current.request_item.getRefRecord();

    // If no RITM found, exit
    if (!ritm) return;

    // Flag to track tasks
    var hasWip = false;
    var hasPending = false;
    var hasClosedIncomplete = false;
    var allClosedComplete = true;

    // Query all tasks related to the RITM
    var taskGr = new GlideRecord('sc_task');
    taskGr.addQuery('request_item', ritm.sys_id);
    taskGr.query();
    while (taskGr.next()) {
        // Skip the current task as its state has already changed
        if (taskGr.sys_id == current.sys_id) {
            taskGr.state = current.state;
        }

        // Check states
        if (taskGr.state == 2) { // WIP (Work in Progress)
            hasWip = true;
        } else if (taskGr.state == 3) { // Pending
            hasPending = true;
        } else if (taskGr.state == 8 || taskGr.state == 9) { // Closed Skipped or Closed Incomplete
            hasClosedIncomplete = true;
        } else if (taskGr.state != 4) { // Not Closed Complete
            allClosedComplete = false;
        }
    }

    // Determine the RITM state based on conditions
    if (hasClosedIncomplete) {
        ritm.state = 9; // Closed Incomplete
    } else if (hasWip) {
        ritm.state = 2; // WIP
    } else if (hasPending) {
        ritm.state = 3; // Pending
    } else if (allClosedComplete) {
        ritm.state = 4; // Closed Complete
    }

    // Update the RITM if state has changed
    ritm.update();

})(current, previous);

 Hope this helps.

View solution in original post

12 REPLIES 12

Sandeep Rajput
Tera Patron
Tera Patron

@kun1 

Create a Business Rule on the sc_task table
Trigger Conditions: Set it to run on update when the state field changes.
When to Run: Use After.
Filter Conditions: State Changes

 

(function executeRule(current, previous /*null when async*/) {
    // Get the parent RITM (Requested Item)
    var ritm = current.request_item.getRefRecord();

    // If no RITM found, exit
    if (!ritm) return;

    // Flag to track tasks
    var hasWip = false;
    var hasPending = false;
    var hasClosedIncomplete = false;
    var allClosedComplete = true;

    // Query all tasks related to the RITM
    var taskGr = new GlideRecord('sc_task');
    taskGr.addQuery('request_item', ritm.sys_id);
    taskGr.query();
    while (taskGr.next()) {
        // Skip the current task as its state has already changed
        if (taskGr.sys_id == current.sys_id) {
            taskGr.state = current.state;
        }

        // Check states
        if (taskGr.state == 2) { // WIP (Work in Progress)
            hasWip = true;
        } else if (taskGr.state == 3) { // Pending
            hasPending = true;
        } else if (taskGr.state == 8 || taskGr.state == 9) { // Closed Skipped or Closed Incomplete
            hasClosedIncomplete = true;
        } else if (taskGr.state != 4) { // Not Closed Complete
            allClosedComplete = false;
        }
    }

    // Determine the RITM state based on conditions
    if (hasClosedIncomplete) {
        ritm.state = 9; // Closed Incomplete
    } else if (hasWip) {
        ritm.state = 2; // WIP
    } else if (hasPending) {
        ritm.state = 3; // Pending
    } else if (allClosedComplete) {
        ritm.state = 4; // Closed Complete
    }

    // Update the RITM if state has changed
    ritm.update();

})(current, previous);

 Hope this helps.

Will it work for multiple Sc-task scenarios?

@kun1 yes it will.

This script wil need to be run on update only not on insert