- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi everyone,
I have a custom table u_documentsdemo_table with a custom field u_document (type: [fill in: Document ID / Attachment / other, as shown in the dictionary entry]). On the form, this field shows a “Click to add...” link.
Requirement
I want to block the submit and show an alert (“Please attach the document before submitting”) if no document is attached, and allow the submit if a document is attached. I know a Mandatory field or UI Policy can do this, but I specifically need to implement it with an onSubmit client script for learning purposes.
What I built
An onSubmit client script (UI Type: All, Global checked, Isolate script checked) and a client-callable Script Include. Both are in the screenshots.
Client script:
function onSubmit() {
var ga = new GlideAjax('AttachmentCheck');
ga.addParam('sysparm_name', 'hasAttachment');
ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
ga.getXMLWait();
var answer = ga.getAnswer();
if (answer == 'true') {
return true;
}
alert('Please attach the document before submitting.');
return false;
}
var AttachmentCheck = Class.create();
AttachmentCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasAttachment: function() {
var recId = this.getParameter('sysparm_sys_id');
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', recId);
gr.query();
if (gr.hasNext()) {
return 'true';
}
return 'false';
},
type: 'AttachmentCheck'
});
What works
- Clicking Submit with no attachment shows the alert and stops the submit.
- Adding a file with the paperclip icon at the top, then clicking Submit, saves the record.
What does not work
- When I click “Click to add...” on the u_document field, the alert “Please attach the document before submitting” appears immediately, and the file picker never opens. So I cannot add a file through the field at all. It behaves as if clicking the link triggers the onSubmit script.
What I already tried
- Filtering with g_form.getActionName() to skip non-submit actions. It did not change the behavior.
- Checking g_form.getValue('u_document') before calling GlideAjax. The alert still appears when the field is empty at click time.
- A UI Policy making u_document mandatory works as expected, but I need to understand and solve this with onSubmit.
- Action name shown when the link is clicked: [fill in, using a temporary script alert('Action name: ' + g_form.getActionName()); return true;, or write “no alert appeared”]
My questions
- Why does clicking “Click to add...” on this field trigger the onSubmit script?
- How can I validate that a document is attached on Submit without blocking the field’s own “Click to add...” action?
- Is there a reliable way to tell a real Submit/Update click apart from the field’s internal action inside onSubmit?
- Does a file added through this field get stored in sys_attachment with a different table_name or table_sys_id than one added with the paperclip, especially on a new record that is not saved yet?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
Hi @kishorvanam ,
When you click "New" to open a brand new form, ServiceNow immediately generates a temporary 32-character sys_id in the browser before you even type anything.
When you use the global paperclip, the system just takes your uploaded file, drops it into the sys_attachment table, and stamps it with that temporary sys_id. It doesn't care if your actual record exists yet. If you close the browser tab and never click submit, that file just becomes a hidden orphan in the database and a background scheduled job cleans it up a few days later.
The "File Attachment" field type is completely different. It acts essentially as a specialized reference field. When you add a file via that specific field, the system doesn't just upload the file; it has to grab the sys_id of the newly created attachment and save it directly into the u_document column on your custom table.
Because of how the native UI macro for that field is hardcoded, the platform cannot update a column value on a record that hasn't been committed to the database yet. To get around this, ServiceNow forces a sysverb_insert to physically create the row in your table first. Once the record actually exists, it opens the upload window, attaches the file using that ZZ_YY prefix, and finally updates the u_document column on your newly created row with the attachment's sys_id.
Basically, the paperclip is just a blind file dump using a temporary ID, while the specific field requires a real database row to establish the 1:1 field mapping.
Happy to help! If this resolved your issue, kindly mark it as the Accept Solution✅ and Helpful and close the thread 🔒 so it helps other community members too.
Warm Regards,
Deepak Sharma
Community Rising Star 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hey @kishorvanam
Could you please confirm the dictionary type of u_document? Is it a File Attachment field or a Document ID field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @vaishali231, thanks for replying. The dictionary type of u_document is File Attachment.
The issue: on a new record, when I click “Click to add...” on this field, my onSubmit script runs immediately and shows the alert before any file picker opens. The paperclip icon at the top works fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
hi @kishorvanam
1. Why does clicking "Click to add..." trigger the onSubmit script? Because you cannot attach a file to a record that doesn't exist yet. Attachments require a valid table_sys_id in the database. When you click "Click to add..." on a brand new record, the platform secretly forces a form submission (an insert) to create the record and generate a real sys_id so it has somewhere to attach the file. Your onSubmit script catches this forced save, sees that there is no attachment yet (because the upload window hasn't opened), and blocks the save. This breaks the link entirely.
How are files stored in sys_attachment for this field vs. the paperclip? (Your Question 4) This is a huge platform secret: files added via a specific File Attachment field do not use the standard table name. If your table is u_documentsdemo_table, files added via the paperclip get stored with table_name = u_documentsdemo_table. However, files added via your u_document field get stored with a special hidden prefix: table_name = ZZ_YYu_documentsdemo_table. This prefix is what tells the platform to hide that specific file from the global paperclip list at the top of the form and only display it inside your custom field.
using the standard Dictionary "Mandatory" checkbox is the only clean way to do this. ServiceNow's internal mandatory UI policies have hardcoded logic to bypass the block when you click "Click to add...", which custom onSubmit scripts don't have access to.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Deėpak Sharma , thank you for the detailed explanation, that really helps.
I know a Mandatory field or UI Policy would solve this, but I specifically want to understand and solve it with a client script only for learning purposes.
One thing I don’t fully understand: on a new, unsaved record, the paperclip icon works fine without any save. I can add a file, and my onSubmit script is not triggered. As I understand it, the file is stored temporarily in sys_attachment using the sys_id the form already generated (with the ZZ_YY prefix), and it is linked properly when I click Submit.
So why does the “Click to add...” link on the File Attachment field need a forced insert, when the paperclip does not? Both are adding a file to a record that does not exist in the database yet.
