Close Parent Request if RITM has Complete and Cancelled Items

heathers_
Kilo Sage

Hello,

We recently created a new BR so that if a RITM is Cancelled, the parent Request's state and stage would set to Cancelled as well since the oob BR (Close Parent if Required) would set the Request to Complete even if all RITMs were cancelled. I simply replicated the original BR and replaced the applicable values to reflect cancelled. This worked great until I realized that if a request has multiple RITMs, each with a different stage (complete, cancelled, rejected) the parent Request will now remain open. I suspect it is because each BR is running a query to make sure all other items have the same stage and since they do not, the request will not close. Any ideas on how to add a line into the script that checks all associated RITMs and close the parent request with the state and stage of complete if just one item closes as complete? Even if 5 RITMs are cancelled and/or rejected but 1 is complete, I need the Request to close as complete. I think modifying "Close Parent if Required" would be best, because the new BR for "Cancel Parent if Required" truly needs to run only if all RITMs are cancelled.

Thank you,

Heather Swearingen

closeParentIfRequired();

function closeParentIfRequired(){

  // check to see if any of our peers are currently *not* closed

  var gr = new GlideRecord('sc_req_item');

  gr.addQuery('request', current.request);

  gr.addQuery('stage', '!=', 'complete');

  //gr.addQuery('stage', '!=', 'Request Cancelled');

  gr.query();

  if (!gr.next()) {

        // no peer task is currently open

        var sc_request = new GlideRecord('sc_request');

        sc_request.addQuery('sys_id', current.request);

        sc_request.query();

        sc_request.next();

        gs.print("SC_REQUEST.STAGE = " + sc_request.stage + " INDEX = " + sc_request.stage.toString().indexOf('closed'));

        if (sc_request.stage.toString().indexOf('closed') == -1) {

                sc_request.stage = "closed_complete";

                sc_request.comments.setJournalEntry("Request Automatically Closed as all Line Items were complete");

                sc_request.update();

        }

  }  

}

1 ACCEPTED SOLUTION

Hi Heather,



You will need to declare another GlideRecord variable (gr3) instead of reusing gr2, I have modified your codes as below


closeParentIfRequired();



function closeParentIfRequired(){



  // check to see if any of our peers are currently not closed


  var gr = new GlideRecord('sc_req_item');


  gr.addQuery('request', current.request);


  gr.addQuery('stage', '!=', 'complete');


  gr.addQuery('stage', '!=', 'Request Cancelled');


  gr.addQuery('stage', '!=', 'Final Rejected');


  gr.query();


  if (!gr.next()) {


      // no peer task is currently open



      //check if there is any sibling in complete stage


      var gr2 = new GlideRecord('sc_req_item');


      gr2.addQuery('request',current.request);


      gr2.addQuery('stage','complete');


      gr2.setLimit(1);


      gr2.query();



      //closed_complete if there's a single sibling in complete, otherwise cancelled


      var new_stage;



      if (gr2.hasNext()){


            new_stage = 'closed_complete';


      }


      else{


            var gr3 = new GlideRecord('sc_req_item');


            gr3.addQuery('request',current.request);


            gr3.addQuery('stage','Final Rejected');


            gr3.setLimit(1);


            gr3.query();



            if (gr3.hasNext()){


                new_stage = 'closed_rejected' ;


            }


            else{


                new_stage = 'closed_cancelled';


            }


      }



      var sc_request = new GlideRecord('sc_request');


      sc_request.addQuery('sys_id', current.request);


      sc_request.query();


      sc_request.next();


      gs.print("SC_REQUEST.STAGE = " + sc_request.stage + " INDEX = " + sc_request.stage.toString().indexOf('closed'));



      if (sc_request.stage.toString().indexOf('closed') == -1) {


            sc_request.stage = new_stage;


            sc_request.comments.setJournalEntry("Request Automatically Closed as all Line Items were complete");


            sc_request.update();


      }


  }


}



let me know how you go


View solution in original post

7 REPLIES 7

Nam Nguyen
Tera Expert

Hi Heather,



If you're going to modify the OOB BR, may I suggest removing the "Cancel Parent if Required" and just have the following:



closeParentIfRequired();



function closeParentIfRequired(){



  // check to see if any of our peers are currently *not* closed


  var gr = new GlideRecord('sc_req_item');


  gr.addQuery('request', current.request);


  gr.addQuery('stage', '!=', 'complete');


  gr.addQuery('stage', '!=', 'Request Cancelled');


  gr.query();


  if (!gr.next()) {


      // no peer task is currently open



      //check if there is any sibling in complete stage


      var gr2 = new GlideRecord('sc_req_item');


      gr2.addQuery('request',current.request);


      gr2.addQuery('stage','complete');


      gr2.setLimit(1);


      gr2.query();



      //closed_complete if there's a single sibling in complete, otherwise cancelled


      var new_stage = (gr2.hasNext())?'closed_complete':'closed_cancelled';



      var sc_request = new GlideRecord('sc_request');


      sc_request.addQuery('sys_id', current.request);


      sc_request.query();


      sc_request.next();


      gs.print("SC_REQUEST.STAGE = " + sc_request.stage + " INDEX = " + sc_request.stage.toString().indexOf('closed'));


 


      if (sc_request.stage.toString().indexOf('closed') == -1) {


          sc_request.stage = new_stage;


          sc_request.comments.setJournalEntry("Request Automatically Closed as all Line Items were complete");


          sc_request.update();


      }


  }  


}


Thank you, this seems to have done the trick. My next question is, we have also created a custom Rejected stage and I want to include that in this as well. Below is what I initially tried, but the Request ended up closing as Closed Complete. I am assuming it has to do with the following line:



var new_stage = (gr2.hasNext())?'closed_complete':'closed_cancelled'||'closed_rejected';




I was unable to use a : in-between closed_complete and closed_rejected due to syntax errors.



Thanks again for any feedback.






closeParentIfRequired();



function closeParentIfRequired(){



// check to see if any of our peers are currently not closed


var gr = new GlideRecord('sc_req_item');


gr.addQuery('request', current.request);


gr.addQuery('stage', '!=', 'complete');


gr.addQuery('stage', '!=', 'Request Cancelled');


gr.addQuery('stage', '!=', 'Final Rejected');


gr.query();


if (!gr.next()) {


// no peer task is currently open



//check if there is any sibling in complete stage


var gr2 = new GlideRecord('sc_req_item');


gr2.addQuery('request',current.request);


gr2.addQuery('stage','complete');


gr2.setLimit(1);


gr2.query();



//closed_complete if there's a single sibling in complete, otherwise cancelled


var new_stage = (gr2.hasNext())?'closed_complete':'closed_cancelled'||'closed_rejected';



var sc_request = new GlideRecord('sc_request');


sc_request.addQuery('sys_id', current.request);


sc_request.query();


sc_request.next();


gs.print("SC_REQUEST.STAGE = " + sc_request.stage + " INDEX = " + sc_request.stage.toString().indexOf('closed'));



if (sc_request.stage.toString().indexOf('closed') == -1) {


sc_request.stage = new_stage;


sc_request.comments.setJournalEntry("Request Automatically Closed as all Line Items were complete");


sc_request.update();


}


}


}





Thank you,


Heather Swearingen


Hi Heather, glad to have helped



This line below


var new_stage = (gr2.hasNext())?'closed_complete':'closed_cancelled'||'closed_rejected';



Is just short-hand for


  var new_stage;


  if (gr2.hasNext()){


      new_stage = 'closed_complete';


  }


  else {


      new_stage = 'closed_cancelled';


  }



You will need another condition if you want to add the answer, says, when will you set it as closed_cancelled, and when will you set it as closed_rejected?


I do have my condition to run on:



current.stage.changes() && (current.stage=='complete' || current.stage=='Request Cancelled' || current.stage=='Final Rejected').




I would like the Request to close as Rejected if the RITMs DO NOT have a stage of Completed but do have a stage of Final Rejected and then the Request should cancel if all the associated RITMs only have a stage of Cancelled. After my modification, the Request keeps setting itself to Cancelled which in turn sets the Rejected and/or Completed RITM to a state/stage of Cancelled. I have tried several different ways to change the behavior but keep experiencing the same results.





closeParentIfRequired();



function closeParentIfRequired(){



// check to see if any of our peers are currently not closed


var gr = new GlideRecord('sc_req_item');


gr.addQuery('request', current.request);


gr.addQuery('stage', '!=', 'complete');


gr.addQuery('stage', '!=', 'Request Cancelled');


gr.addQuery('stage', '!=', 'Final Rejected');


gr.query();


if (!gr.next()) {


// no peer task is currently open



//check if there is any sibling in complete stage


var gr2 = new GlideRecord('sc_req_item');


gr2.addQuery('request',current.request);


gr2.addQuery('stage','complete');


gr2.setLimit(1);


gr2.query();



//closed_complete if there's a single sibling in complete, otherwise cancelled


var new_stage;


if (gr2.hasNext()){


new_stage = 'closed_complete';


}


else



//var gr3 = new GlideRecord('sc_req_item');


gr2.addQuery('request',current.request);


gr2.addQuery('stage','Final Rejected');


gr2.setLimit(1);


gr2.query();



if (gr2.hasNext()){


new_stage = 'closed_rejected' ;


}


else


//(new_stage != 'closed_complete' && new_stage != 'closed_rejected');{


new_stage = 'closed_cancelled';




var sc_request = new GlideRecord('sc_request');


sc_request.addQuery('sys_id', current.request);


sc_request.query();


sc_request.next();


gs.print("SC_REQUEST.STAGE = " + sc_request.stage + " INDEX = " + sc_request.stage.toString().indexOf('closed'));



if (sc_request.stage.toString().indexOf('closed') == -1) {


sc_request.stage = new_stage;


sc_request.comments.setJournalEntry("Request Automatically Closed as all Line Items were complete");


sc_request.update();


}


}


// }


}