attachment fails
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 03:15 AM
Hi Guys,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 03:37 AM
@tGhadage Please update your code as follows.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var attachment_file = new GlideRecord('sys_attachment');
//attachment_file.addAggregate('COUNT');
//attachment_file.addQuery('table_name',current.getTableName());
attachment_file.addQuery('table_sys_id',current.sys_id);
attachment_file.query();
//
if(!attachment_file.hasNext()){
gs.addErrorMessage('Please attach the requried documents before closing the task');
current.state=previous.state;
current.setAbortAction(true);
}
})(current, previous);
Please mark the response helpful and accepted solution if it manages to address your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 03:40 AM
Hi @tGhadage
Please change while to if and add ! check in your code.
if (!attachment_file.hasNext()) {
gs.addErrorMessage('Please attach the requried documents before closing the task');
current.state = previous.state;
current.setAbortAction(true);
}
hasNext() is returning true and while is going through loop continuously
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 03:47 AM
Hi @tGhadage ,
Please try below script Business Rule:
then script
(function executeRule(current, previous /*null when async*/) {
var attachment_file = new GlideRecord('sys_attachment');
// Query for attachments related to the current record
attachment_file.addQuery('table_name', current.getTableName()); // Ensure it's checking the correct table
attachment_file.addQuery('table_sys_id', current.sys_id); // Query based on the current record's sys_id
attachment_file.query();
if (!attachment_file.hasNext()) {
gs.addErrorMessage('Please attach the requried documents before closing the task');
current.state = previous.state; // Revert the state to its previous value
current.setAbortAction(true); // Prevent closing the task
}
})(current, previous);
after hit the CLOSE TASK button or change state to Close complete then it will throw error
after that refresh the page and see the SCTASK still in previous state not to close
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."
Thanks,
BK