Difference in States REQ and RITM.

abc1233
Tera Contributor

For any closed incomplete or closed skipped RITM, the corresponding REQ shows state Closed Complete.
I believe this is done automatically at closure of the RITM.

This needs to be resolved so that when the RITM is closed skipped or closed incomplete, the associated REQ state is also set to closed skipped or closed incomplete.

 

I want to fullfill this by business rule.

 

5 REPLIES 5

Danish Bhairag2
Tera Sage
Tera Sage

Hi @abc1233 ,

 

Please note make sure we have similar kind of states available in Request table as well.

 

Certainly, you can achieve this by creating a Business Rule in ServiceNow. Here's how you can create a Business Rule to update the state of the associated Request (REQ) when its corresponding Requested Item (RITM) is closed skipped or closed incomplete.

 

### Step 1: Create a Business Rule

 

1. Go to **Business Rules** in your ServiceNow instance.

 

2. Click on **New** to create a new Business Rule.

 

3. Provide a name for the Business Rule, such as "Update REQ State on RITM Closure."

 

4. Set the **Table** to the Requested Item (RITM) table (usually `sc_req_item`).

 

5. Define the **When to run** condition. This condition checks if the RITM is being closed skipped or closed incomplete.

 

current.state == 6 || current.state == 7

 

   - `6` represents "Closed Incomplete" state.

   - `7` represents "Closed Skipped" state.

 

6. In the **Advanced** tab, add the following script to update the corresponding Request's state:

 

  

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

       // Check if the RITM has a corresponding REQ

       if (current.request) {
           var req = new GlideRecord('sc_request');
           if (req.get(current.request)) {
               // Set REQ state to match RITM state
               req.state = current.state;
               req.update();
           }
       }

   })(current, previous);

  

 

7. Save the Business Rule.

 

With this Business Rule in place, whenever an RITM is closed skipped or closed incomplete, the corresponding REQ's state will be updated accordingly to match the state of the RITM. Make sure to test this in a non-production environment before applying it to your live instance.

 

Thanks,

Danish

 

Harish KM
Kilo Patron
Kilo Patron

Hi @abc1233 You can write a after update BR on Request item table

cond: state changesTO closed Complete

or State ChangesTO Closed Skipped

Script:

var req = new GlideRecord('sc_request');
req.addQuery('sys_id',current.request);
req.query();
if(req.next())
{
if(current.state == 4) // closed incomplete
{
req.state = 4;
req.update();
}
else if(current.state == 7) // closed skipped
{
req.state = 7;

req.update();
}
}

Regards
Harish

Hi @Harish KM 

Above one is working fine for SCtask -> RITM

but I want SCTask -> RITM -> REQ

Means,

When filtering on SCTASKs that are in state Closed Skipped or Closed Incomplete, I can get a list of the SCTASKs and the associated RITMs and REQs.

For any closed incomplete or closed skipped SCTASK, the corresponding RITM shows state Closed Complete.
In addition, the corresponding state of the REQ also shows as Closed Complete.
I believe this is done automatically at closure of the SCTASK.

 

This needs to be resolved so that when the SCTASK is closed skipped or closed imcomplete, the associated RITM state is also set to closed skipped or closed incomplete.

 

I need to Fulfill this by Business rule

can you please help?

 

Hi @abc1233 You can use below BR on sc_task table

// to close RITM

    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('sys_id', current.request_item);
    ritm.query();
    if (ritm.next()) {
        if (current.state == 4) {
            ritm.state = 4; // close Incomplete
        } else if (current.state == 7) {
            ritm.state = 7; // close skip
       
        }
        ritm.update();

    }
    // To Close Req
    var req = new GlideRecord('sc_request');

    req.addQuery('sys_id', current.request);
    req.query();
    if (req.next()) {
        if (current.state == 4) // closed incomplete
        {
            req.state = 'closed_incomplete';
            req.request_state = 'closed_incomplete';
            req.stage = 'closed_incomplete'; // you need to set stage for request state to change

        } else if (current.state == 7) // closed skipped
        {
            req.state = 'closed_skipped';
req.request_state = 'closed_skipped';
req.stage = 'closed_skipped';
        }


        req.update();

    }


Regards
Harish