- 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 04:20 AM
Hi Bogdan,
That definitely will cause issues. I would rather find why the value with 4 is not working?
1) Is that Choice option Inactive is true?
2) That task state has any Dictionary override choices defined for request table?
3) Are there any Business Rules stopping me to do so. (I would use field watcher for this).
Thanks
Srini
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2016 07:28 AM
Hi Srinivas,
You were correct a business rule was interfering with it.
thanks a lot,
Bogdan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2016 01:28 AM
Hi,
First, Best way to do this I would say is through the workflow which I guess you have for this.
But if you want to this with a BR for some reason this would work.
The reason it aint working for you is that the table "sc_request" where the requests is, doesn't have a field called request_item. Easiest way to see this is that you go to "tables" and find the sc_request there and see what it contains. Or open up a request and look at what fields there are.
var gr = new GlideRecord('sc_request');
//Here you can use get() since you know you only will get 1 record.
gr.get(current.request); //This is the field on the RITM that contains the connection to the request
gr.request_state = 'closed_rejected';//Remember to use the value of the state and not the label
gr.state = '4': //the value of "canceled".
gr.update();
Try and let me know
//Göran
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2016 03:57 AM
Hi Goran,
Thanks for the suggestion, for some strange reason the Closed Cancelled value is set to 4 but the script doesn't take it in consideration, it is defaulting to Open, the solution I found was to create another choice on the task table with the same name "Closed Cancelled" and then added the value 25 then changed the script to gr.state = '25';, and it worked.
Best regards,
Bogdan
- 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