- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2016 01:06 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2016 02:57 AM
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
- var sc_request_item = new GlideRecord('sc_req_item');
- sc_request_item.addQuery('sys_id', current.sysapproval);
- // 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.
- sc_request_item.query();
- while(sc_request_item.next()) {
- sc_request_item.state = "Closed Cancelled";
- // 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
- 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
- gs.log('Executring BR');
- // 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.
- sc_request_item.update();
- }
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2016 01:27 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2016 02:58 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2016 03:07 AM
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' .
Thanks
Srini
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2016 03:47 AM
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