make attachment mandatory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2025 02:37 AM
Write code to make attachment mandatory if priority is critical and state changed to resolved in Incident table.
Please suggest.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2025 04:58 AM
Hi @snowsid88 ,
I hope you are doing well !
Please let us know if your issue has been resolved or not.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards,
Mohammad Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2025 02:16 AM
Hi @snowsid88 ,
As per my understanding below is the problem statement :-
Make an attachment mandatory when:
* Priority = Critical
* State changes to Resolved
on the Incident table (incident)
You can do this elegantly with a Business Rule, UI Policies, Client Script, Flow Designer and a small server-side script.
Step 1: Create Business Rule
* Table: incident
* When: Before update
* Advanced: ✔ (check "Advanced")
* Conditions: leave blank (handle in script)
Script: add the following
Sample code:
(function executeRule(current, previous /*null when async*/) {
// Only when state changes to Resolved (state = 6)
if (current.state == 6 && previous.state != 6) {
// Only when priority is Critical (priority = 1)
if (current.priority == 1) {
// Check for attachments
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_sys_id', current.sys_id);
attachment.addQuery('table_name', 'incident');
attachment.query();
if (!attachment.hasNext()) {
gs.addErrorMessage('An attachment is required when closing a critical incident.');
current.setAbortAction(true);
}
}
}
})(current, previous);
Explanation:
* Runs before saving the update.
* Checks:
* Priority = Critical (priority == 1)
* State is changing to Resolved (state == 6)
* Looks for attachments in sys_attachment linked to the Incident.
* If none → stops the update and shows an error.
Tips:
* Test carefully in a sub-production instance.
* If you use custom priority values / states, adjust numbers accordingly.
* If you want to make it work on specific resolution states, you can expand the check.
Create a UI Policy -
if Priority = Critical and State = Resolved, the user sees that an attachment is mandatory before saving.
Because attachments aren’t native fields, we can’t directly make them mandatory via UI Policy. Instead, we use:
* UI Policy: to react to field changes.
* Client Script: to do the real check before submit.
Field | Value |
Table | incident |
When | onLoad and onChange |
Conditions | Priority is 1 (Critical) AND State is 6 (Resolved) |
Reverse if false |
UI Policy doesn’t control attachments, but it helps trigger client logic and make the UI consistent.
Please appreciate the efforts of community contributors by marking appropriate response as Mark my Answer Helpful or Accept Solution this may help other community users to follow correct solution in future.
Thank You
AJ - TechTrek with AJ - ITOM Trainer
LinkedIn:- https://www.linkedin.com/in/ajay-kumar-66a91385/
YouTube:- https://www.youtube.com/@learnitomwithaj
Topmate:- https://topmate.io/aj_techtrekwithaj (Connect for 1-1 Session)
ServiceNow Community MVP 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2025 05:02 AM
so what script did you start with and where are you stuck?
You can use before update business rule on incident table with that matching condition
Priority == Critical and State [Changes to] Resolved
Script:
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
if (!current.hasAttachments()) {
gs.addErrorMessage('Please attach attachments when resolving a Critical incident.');
current.setAbortAction(true);
}
})(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