How to change Request state to Closed Rejected and State to Closed Cancelled when the RITM gets rejected ?

Bogdan18
Tera Guru

Hi everyone,

I would need a business rule that would look if the RITM(sc_req_item) Approval was Rejected and if it did then the Request state must be changed to Closed Rejected and the State of the Request(sc_request) to Closed Cancelled.

I have the below script in an after BR   with the condition   current.approval.changes() && current.approval == 'rejected' and on the sc_req_item table but it doesn't work.

function onBefore(current, previous) {

var gr = new GlideRecord("sc_request");

    gr.addQuery('request_item',current.sys_id);

    gr.query();

    while (gr.next())

  gr.request_state = 'Closed Rejected';

  gr.state = 'Closed Cancelled';

  gr.update();

      }

Thanks,

Bogdan Pirsan

1 ACCEPTED SOLUTION

zica
Giga Guru

Hi Bodgan,



Here is a way to achieve your quiery.


create a new business rules which will run on insert and update. The BR should also run on 'after'


under advanced tab, copy the sript below :


condition : current.stage.changes() && (current.stage=='Request Cancelled')


closeReq();




function closeReq(){




  // 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('state', '7'); // 7 = closed cancelled


  gr.query();


  if (!gr.next()) {


        // no peer ritm 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'));


        sc_request.stage = "closed_complete";


        sc_request.state = '7';


                sc_request.comments.setJournalEntry("Request closed");


                sc_request.update();


}}}






       



I am sorry, but I have one more question : how do you reject your item ? is it from approval ?


if yes, therefore you need this following script in order to cancel your approval in the first place, than cancel your ritm then cancel your request :


Create a BR as following :


table : Approval [sysapproval_approver]


When to run : after


Update : Checked


Filter condition : state changes to cancelled


  1. var sc_request_item = new GlideRecord('sc_req_item');  
  2.   sc_request_item.addQuery('sys_id', current.sysapproval);
  3. // current is the object of current record. in this case it is sysapproval_approver table record since BR is running on it. there is no field with name 'Request' but //it is the 'sysapproval' which points to the Record for which approval is inserted. I assume it is the RITM record.  
  4. sc_request_item.query();    
  5. while(sc_request_item.next())   {  
  6. sc_request_item.state = "Closed Cancelled";  
  7. // here you need to pass on the VALUE of choice field and not the LABLE, normally OOB values for choice list of state are Integers. So please check this as well    
  8. sc_request_item.stage = "Request Cancelled";   //here you need to pass on the VALUE of choice field and not the LABLE,   So please check this as well    
  9. gs.log('Executring BR');  
  10. // always try to log statements in scripts to find whether it is even being fired or not. After successful testing, you can simly comment this out.    
  11. sc_request_item.update();  
  12. }    


It will cancel your requested_item and then the business Rules 'Close Parent if Required" will trigger automatically



Test the follwing script and let me know if you need futher info



Kind regards,


ZA



Do not feel shy to mark correct or helpful answer if it helps or is correct


View solution in original post

11 REPLIES 11

zica
Giga Guru

change this


request.state='closed_cancelled';


to


request.state='7';



Kind regards,


ZA



Do not feel shy to mark correct or helpful answer if it helps or is correct


Bogdan18
Tera Guru

Thank you,



I already had the script to cancel the approval and then the RITM, I only needed to cancel the Request as well when it gets rejected.



Best regards,


Bogdan