Script to change SCTASK state when RITM updated

steveturley
Tera Guru

Hi all

 

Just after a bit of script to help me update the SCTASK state out of Pending when the linked RITM is updated

 

Thanks!

1 ACCEPTED SOLUTION

piyushsain
Tera Guru
Tera Guru

create a after update BR on RITM table. In script :

var scTask = new GlideRecord('sc_task');
scTask.addQuery('parent',current.getUniqueValue());
scTask.addQuery('state',add Pending state value);
scTask.query();
while(scTask.next()){
scTask.state = 'add the new state value';
scTask.update();

}
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

View solution in original post

3 REPLIES 3

Vishal Birajdar
Giga Sage

Hi @steveturley 

 

Have you written any script (Business rule) to achieve this or you need script from scratch...??

 

if from scratch then below is script :

 

1.Write After update business rule on RITM

 

VishalBirajdar_0-1697015382548.png

 

2 script 

 

(function executeRule(current, previous /*null when async*/ ) {
    
	/*1. Get current ritm */
    var ritm = current.getUniqueValue();
    
	/*2.Get state of ritm */
	var state = current.getValue('state');

	/*3. Glide record on sc_task to update state w.r.t state of ritm */
    var grScTask = new GlideRecord('sc_task');
    grScTask.addQuery('request_item', ritm);
    grScTask.query();

    while (grScTask.next()) {

        /* update sctask state w.r.t ritm state */
        grScTask.state = state;
        grScTask.update();
    }

})(current, previous);

 

 

VishalBirajdar_1-1697015446805.png

 

Hope this helps....!!!

 

 

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

piyushsain
Tera Guru
Tera Guru

create a after update BR on RITM table. In script :

var scTask = new GlideRecord('sc_task');
scTask.addQuery('parent',current.getUniqueValue());
scTask.addQuery('state',add Pending state value);
scTask.query();
while(scTask.next()){
scTask.state = 'add the new state value';
scTask.update();

}
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

Sandeep Rajput
Tera Patron
Tera Patron

@steveturley Here is how you should configure the business rule.

Screenshot 2023-10-11 at 2.30.37 PM.pngScreenshot 2023-10-11 at 2.40.26 PM.png

 

Here is the script for you.

 

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

	// Add your code here
	var glideSCTask = new GlideRecord('sc_task');
	glideSCTask.addQuery('request_item',current.getValue('sys_id'));
	glideSCTask.addQuery('state','-5');//filter for pending state
	glideSCTask.query();
	while(glideSCTask.next()){
		glideSCTask.setValue('state','1');//set state to open
		glideSCTask.update();
	}

})(current, previous);