We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Update REQ based on RITM state and stage

Priya Rao
Tera Contributor

Hi all,

 

I have created an order guide, and it contains four different catalog items, each of which follows a different flow. I have used flow design. Now, I want to check the status of each RITM, and if the state and stage of all four RITMs are complete, then I need to update the REQ record for those RITMs. How can I achieve this?

 

Thanks in advance!

@Ankur Bawiskar @Rahul Talreja @PavanK960672992 @Samaksh Wani @VishalB06557037 @Dr Atul G- LNG @Hemanth M1 @Saurabh Gupta @Sagar Pagar @Aman Kumar S 

3 ACCEPTED SOLUTIONS

Hi @Priya Rao 

 

Ok so yo need write Business rule as below on RITM table :

 

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

    /* 1. Get the request from current form */
    var request = current.request;

    /* 2. Glide Record on Request item table & check if there is any RITM which is not in closed complete state */
    var grItem = new GlideRecord('sc_req_item');
    grItem.addEncodedQuery('request=' + request + '^state!=3');
    grItem.query();

    if(!grItem.next()){
		/* update request record */
		var grReq = new GlideRecord('sc_request');
		grReq.addQuery('sys_id',request);
		grReq.query();

		if(grReq.next()){
              // set values on request
			  grReq.state = 3;
			  grReq.update();
		}

	}

})(current, previous);

 

VishalBirajdar_0-1696576567748.png

 

VishalBirajdar_1-1696576601918.png

 

Hope this helps....!!

 

 

 

Vishal Birajdar
ServiceNow Developer

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

View solution in original post

 

var request = current.request;
var grRITM = new GlideRecord('sc_req_item');
grRITM.addEncodedQuery('stateNOT IN3,4,7^request=' + request);
grRITM.query();
    if (!grRITM.hasNext()) {
        var req = new GlideRecord('sc_request');
        req.get(request);
        req.state = ''; //Update according to your requirement
    }

 

 

piyushsain_0-1696579344494.png

piyushsain_1-1696579374853.png

 

 

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

PavanK960672992
Mega Patron

Hi @Priya Rao ,

Create after business rule on RITM table and select update and give condition as when state changes to closed complete. In advance use below script

Script:

var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('request',current.request); //get RITMS only for the same request
grRITM.addEncodedQuery('state!=3');//state is not closed complete
grRITM.query();
if (!grRITM.next()) {//if we don't have any RITM other than close complete
var grReq = new GlideRecord('sc_request');
if(grReq.get(current.request)){
grReq.stage = 'closed_complete'; //give your stage value
grReq.update();
}
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

15 REPLIES 15

 

var request = current.request;
var grRITM = new GlideRecord('sc_req_item');
grRITM.addEncodedQuery('stateNOT IN3,4,7^request=' + request);
grRITM.query();
    if (!grRITM.hasNext()) {
        var req = new GlideRecord('sc_request');
        req.get(request);
        req.state = ''; //Update according to your requirement
    }

 

 

piyushsain_0-1696579344494.png

piyushsain_1-1696579374853.png

 

 

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

@piyushsain , Thanks for your response. I tried with this but it's not setting the state of the REQ to Closed Complete when all the RITM's are closed. 

Tai Vu
Kilo Patron

Hi @Priya Rao 

You can create your custom rule to achieve it.

OOTB manage Request State [request_state] through the Business Rule below.

Name: Set Request State

URL: https://<instance_name>/sys_script.do?sys_id=19a9ecb40a0a0a65013c62ab86e8036e

 

And also for the Request Stage

Name: Mark Request Closed

URL: https://<instance_name>/sys_script.do?sys_id=40afa110c611228401d8643e2c2748cb

 

Just be careful with any conflict may occur.

 

Cheers,

Tai Vu

Dr Atul G- LNG
Tera Patron

Hi @Priya Rao 

 

Thanks for adding me.

 

I am not a pro/coder but I tested your case in PDI and in OOTB once all RITM closed , then REQ closed, My point instead writing any code, please check OOTB flow / workflow and do this.

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

PavanK960672992
Mega Patron

Hi @Priya Rao ,

Create after business rule on RITM table and select update and give condition as when state changes to closed complete. In advance use below script

Script:

var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('request',current.request); //get RITMS only for the same request
grRITM.addEncodedQuery('state!=3');//state is not closed complete
grRITM.query();
if (!grRITM.next()) {//if we don't have any RITM other than close complete
var grReq = new GlideRecord('sc_request');
if(grReq.get(current.request)){
grReq.stage = 'closed_complete'; //give your stage value
grReq.update();
}
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar