How to update RITM state when SCTASK state change

Lkowalski
Tera Contributor

Hello All,

 

I am trying to create a Business rule which will change state of RITM:

if SCTASK state = Pending, then RITM state = Pending
if SCTASK state = Work in progress, then RITM state = Work in progress
if SCTASK state = Awaiting User Info, then RITM state = Awaiting Customer

 

I had created a below BR:

 SCRIPT:

I am trying to below script create a code for condition: if SCTASK state = Pending, then RITM state = Pending

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

    var ritm = new GlideRecord('sc_req_item');
    if (ritm.get(current.request_item)) {
        if (ritm.state != '1') {
            ritm.setValue('state', 1);
            ritm.update();
        }
    }
})(current, previous);

What script should I write to make it works ?

 

Regards,

Lukas

2 ACCEPTED SOLUTIONS

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @Lkowalski ,

 

I am giving you two examples of code that you can use. Based on your situation you can any of this.

 

Example No 1:

This will only if the state choice values are same for both sc_task and RITM record. (OOTB they are same but confirm before using if there any customization made)

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

//GlideRecord to get the parent RITM of the current sc_task
var gr = new GlideRecord('sc_req_item');
if (gr.get(current.request_item)) {
//Update the state of the parent RITM to the state of the current sc_task
gr.state = current.state;         // in this case it will set the same choice value to RITM choice of state.
gr.update();
}

})(current, previous);

 

 

Example No 2: 

In this one if the choice value are not same then you need to check sc_task choice value and based on that you can set the state of RITM. (In my case I have same values but it may be different for you please check the state values first)

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

var ritmGr = new GlideRecord('sc_req_item');
ritmGr.get(current.request_item);

switch(current.state.toString()) {       
case '1': //Open
ritmGr.state = 1; //Requested   // for ritmGr.state you can set any other value if you need set differnt value. same goes for another choies as well.
break;
case '2': //Work in Progress
ritmGr.state = 2; //In Progress
break;
case '3': //Closed Complete
ritmGr.state = 3; //Closed Complete
break;
case '4': //Closed Incomplete
ritmGr.state = 4; //Closed Incomplete
break;
case '7': //Closed Skipped
ritmGr.state = 7; //Closed Skipped
break;
default:
//Default case if the sc_task state doesn't match any of the above
ritmGr.state = current.state;
}

ritmGr.update();

})(current, previous);

 

Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.


Regards,

Siddhesh

View solution in original post

Hello @Lkowalski , 

 

Your script looks correct. Please confirm the Item is IT security. If that's correct can you put some gs.info into your code. And let me know if you are getting logs or not.

 

Regards

Siddhesh

View solution in original post

7 REPLIES 7

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @Lkowalski ,

 

I am giving you two examples of code that you can use. Based on your situation you can any of this.

 

Example No 1:

This will only if the state choice values are same for both sc_task and RITM record. (OOTB they are same but confirm before using if there any customization made)

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

//GlideRecord to get the parent RITM of the current sc_task
var gr = new GlideRecord('sc_req_item');
if (gr.get(current.request_item)) {
//Update the state of the parent RITM to the state of the current sc_task
gr.state = current.state;         // in this case it will set the same choice value to RITM choice of state.
gr.update();
}

})(current, previous);

 

 

Example No 2: 

In this one if the choice value are not same then you need to check sc_task choice value and based on that you can set the state of RITM. (In my case I have same values but it may be different for you please check the state values first)

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

var ritmGr = new GlideRecord('sc_req_item');
ritmGr.get(current.request_item);

switch(current.state.toString()) {       
case '1': //Open
ritmGr.state = 1; //Requested   // for ritmGr.state you can set any other value if you need set differnt value. same goes for another choies as well.
break;
case '2': //Work in Progress
ritmGr.state = 2; //In Progress
break;
case '3': //Closed Complete
ritmGr.state = 3; //Closed Complete
break;
case '4': //Closed Incomplete
ritmGr.state = 4; //Closed Incomplete
break;
case '7': //Closed Skipped
ritmGr.state = 7; //Closed Skipped
break;
default:
//Default case if the sc_task state doesn't match any of the above
ritmGr.state = current.state;
}

ritmGr.update();

})(current, previous);

 

Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.


Regards,

Siddhesh

Hi @Siddhesh Gawade,

Unfortunately both version not works. Maybe I have wrong settings in "When to run" section.

Hi @Lkowalski your BR should be on sc task table

when to run After update checked

cond : state changes

 

share your updated script here to check

Regards
Harish

Hello @Lkowalski ,

 

Please make sure business rule is on sc_task table. And the when to run conditions are satisfied. 

 

If it is not working for you, please share the screenshot of your conditions and the script as well.

 

Regards

Siddhesh