state changes while business rule executes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2024 10:59 PM
Hi,
I have created the business rule to make attachment mandatory while closing the problem task. When state is changed to closed it validates the attachment and gives the warning message.
The concern is the state moves to closed state but actually it does not closed and we have to refresh the page. How do keep the state as working progress while business rule is executed.
I have used the below code. let me know if any changes required.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gr=new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", current.sys_id);
gr.query();
if(!gr.next()){
gs.addErrorMessage("This task cannot be closed until a final summary or conclusion is attached");
current.state = previous.state;
current.setAbortAction(true);
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2024 11:16 PM
try this i.e. reverse the lines
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", current.sys_id);
gr.query();
if (!gr.next()) {
gs.addErrorMessage("This task cannot be closed until a final summary or conclusion is attached");
current.setAbortAction(true);
current.state = previous.state;
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2024 11:17 PM
another way is to have onChange on state and check if state changes to closed and then make a GlideAjax call to check if file is attached
If not then show error message using g_form.addErrorMessage()
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader