Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

onSubmit client script runs when clicking “Click to add...” on a custom attachment/document field, b

kishorvanam
Kilo Explorer

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

    1. Why does clicking “Click to add...” on this field trigger the onSubmit script?
    2. How can I validate that a document is attached on Submit without blocking the field’s own “Click to add...” action?
    3. Is there a reliable way to tell a real Submit/Update click apart from the field’s internal action inside onSubmit?
    4. 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?








0 REPLIES 0