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

srinivasthelu
Tera Guru

Hi Bogdan,



something like below should work.




function onBefore(current, previous) {


var request=current.request.getRefRecord();


request.request_state='closed_rejected';


request.state='closed_cancelled';


request.update();


}



Srini


Thank you Srinivas,



The solution you provided works but not for the State field, I guess it doesn't work because the State field on the Request form is set on the task table not on the sc_request one. Is there a way to get the State field to also update to Closed Cancelled?



Thanks,


Bogdan


Hi Bogdan,



I have assumed state value as 'closed_cancelled' for the label Closed Cancelled. That is is the issue.



You need to find the value for label Closed Cancelled and replace it in the above code. Everything should work as is.



How to find value of the Label?



Right click on the field-> choose 'Show Choice List' .



Screen Shot 2016-02-10 at 4.37.00 PM.png




Thanks


Srini


Hello again,



For some strange reason having the gr.state = '4'; which is the correct value for Closed Cancelled on the task table will not work, the State will stay as the default option which is Open. Anyway I had to create a new choice with the same name "Closed Cancelled" and I added the value 25 to it, then I changed the script to gr.state = '25'; and it worked. Now I am not sure if by creating another choice named the same as the existing one on the task table will cause any issue for other things in the system but I guess I will find out.



Thanks,


Bogdan