how to write a script when a parent ritm is cancelled,child ritm need to be cancelled

raj765_32
Tera Contributor

Hi 

can anyone help me with the code correction for scenario when:

Parent ritm has been cancelled through request table through ui action button named as ' cancel' or the parent ritm  approvers has been rejected in sc_req_item table

i want to cancel a child related ritm in the field called as 'sgdc_ref' (backend value)

 

i have written a ' after' business rule with an insert and update and condition as approvers has been rejected, how to add a condition for the ui action button as cancel in sc_req table 

below code :

 

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

{  if (current.state == 4)

 

  var childRITMs = new GlideRecord('sc_req_item');

childRITMs.addQuery('sgdc_ref', current.request);

 childRITMs.query(); 

while (childRITMs.next()) {

if (childRITMs.state != 4)

 childRITMs.state = 4;

 childRITMs.update();

 })(current, previous);

 

request to please help me with code correction

1 ACCEPTED SOLUTION

Nilesh Pol
Tera Guru

@raj765_32 You can try with the below updated code:

 

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

 

if (current.state == 4) {
if (current.canceled_via_ui_action == true || previous.state != current.state) {

var childRITMs = new GlideRecord('sc_req_item');
childRITMs.addQuery('sgdc_ref', current.request); 
childRITMs.query();

while (childRITMs.next()) {
if (childRITMs.state != 4) {
childRITMs.state = 4; 
childRITMs.update(); 
}
}
}
}

if (current.state == 3) { 
var childRITMs = new GlideRecord('sc_req_item');
childRITMs.addQuery('sgdc_ref', current.request); 
childRITMs.query();

while (childRITMs.next()) {
if (childRITMs.state != 4) {
childRITMs.state = 4; 
childRITMs.update(); 
}
}
}

})(current, previous);

 

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi @raj765_32 ,

 

Please refer below code.

image.png

 

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

    if (current.state == 4 && previous.state != 4) {

        // Get all child RITMs related to this parent
        var childRITMs = new GlideRecord('sc_req_item');
        childRITMs.addQuery('parent', current.sys_id);
        childRITMs.addQuery('state', '!=', 4); // Only update non-cancelled RITMs
        childRITMs.query();

        while (childRITMs.next()) {
            childRITMs.state = 4; // Set state to "Cancelled"
            childRITMs.update();
        }
    }

})(current, previous);

 

Mark this as Helpful / Accept the Solution if this helps.

hi divesh,

the cancel ui button is in sc_req table and here you are writing code in sc_req_item table?

 

and there is another scenario when the parent ritm approver has been rejected also child related ritm need to be closed incomplete

Nilesh Pol
Tera Guru

@raj765_32 You can try with the below updated code:

 

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

 

if (current.state == 4) {
if (current.canceled_via_ui_action == true || previous.state != current.state) {

var childRITMs = new GlideRecord('sc_req_item');
childRITMs.addQuery('sgdc_ref', current.request); 
childRITMs.query();

while (childRITMs.next()) {
if (childRITMs.state != 4) {
childRITMs.state = 4; 
childRITMs.update(); 
}
}
}
}

if (current.state == 3) { 
var childRITMs = new GlideRecord('sc_req_item');
childRITMs.addQuery('sgdc_ref', current.request); 
childRITMs.query();

while (childRITMs.next()) {
if (childRITMs.state != 4) {
childRITMs.state = 4; 
childRITMs.update(); 
}
}
}

})(current, previous);