- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2019 05:21 AM
I am new to scripting.
I want to have a business rule that prevents Requested Item record from closing if Catalog task is NOT in closed complete state
Thank you kindly,
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2019 09:56 AM
try this
var gr = new GlideRecord('sc_task');
gr.addQuery('requested_item', current.sys_id);
gr.addEncodeQuery('state=1^ORstate=2');
gr.addActiveQuery();
gr.query();
if (gr.next())
{
gs.addErrorMessage('There are open task records for this requested item. Please close all tasks.');
current.setAbortAction(true);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2019 05:30 AM
Hi Joyce,
have a before update business rule on sc_req_item table with condition as state changes to close complete and following script;
what it does is checked in sc_task table for this RITM if any task is in open state then show error message and stop record update
var gr = new GlideRecord('sc_task');
gr.addQuery('request_item', current.sys_id);
gr.addEncodedQuery('state=1');
gr.query();
if(gr.getRowCount() > 0){
gs.addErrorMessage('Please close all the open tasks to close the RITM');
current.setAbortAction(true);
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2019 06:47 AM
I am getting invalid update error
I am not sure about the line ...if (gr.getRowCount()>0);
I think the code should check if the state is either a 1 (open) or 2 (work in progress)
Also the states come from task table. Do I need to change glide record to 'task' instead on 'sc_task'?
var gr = new GlideRecord('sc_task');
gr.addQuery('requested_item', 'current.sys_id');
gr.addEncodeQuery('state=1');
gr.addEncodeQuery('state=2');
gr.query();
if (gr.getRowCount()>0);
gs.addErrorMessage('There are open task records for this requested item. Please close all tasks.');
current.setAbortAction(true);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2019 06:55 AM
var gr = new GlideRecord('sc_task');
gr.addQuery('requested_item', current.sys_id);
gr.addEncodeQuery('state=1^ORstate=2');
gr.query();
if (gr.getRowCount()>0)
{
gs.addErrorMessage('There are open task records for this requested item. Please close all tasks.');
current.setAbortAction(true);
}
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2019 07:19 AM
Hi Joyce,
the invalid update message is out of the box which comes from platform level so you cannot change that
so if you want to check if any task record is in open or in progress then you need to update the query as below
gr.addEncodeQuery('state=1^ORstate=2');
regarding the rowCount(); what it does is it checks if it finds any record of task in open or in progress then stop form submission
Also you should query sc_task table since RITM has SC Tasks under it
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader