Restrict submit if there are no attachments attached on the form depending on certain conditions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2025 11:27 PM - edited ‎04-30-2025 11:28 PM
In the form: If the url value is empty it should check if there are any attachments attached. if attachment is there it should submit otherwise it should not submit instead throw an error message.
In the below script that I am writing:
g_form.getUniqueValue() is not giving the record value that is going to be created.( It is giving me sys_id of constant record all the time).
Client Script:
function onSubmit() {
var url = g_form.getValue('file_upload_url');
g_form.addInfoMessage(url);
if (!url) {
g_form.addInfoMessage("alert2:");
var sysid = g_form.getUniqueValue();
alert("Check id = " + sysid);
var gr = new GlideAjax("sn_customerservice.getAttachmentCount");
gr.addParam('sysparm_name', 'getCount');
gr.addParam("sys_attach", sysid);
gr.getXMLAnswer(validate);
}
function validate(answer) {
g_form.addInfoMessage("answer" + answer);
if (answer == false || answer == 'false') {
alert('Please attach Photo(s) or add URL to proceed with the submission of your request');
return false;
} else {
return true;
}
}
}
Script Include:
var getAttachmentCount = Class.create();
getAttachmentCount.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getCount: function() {
var valid = false;
var attachId = this.getParameter("sys_attach");
gs.info("Harika"+attachId);
var gr = new GlideRecord("sys_attachment");
gr.addQuery('table_sys_id', attachId);
gr.query();
if(gr.getRowCount()>1){
if (gr.next()) {
gs.info("Harika"+gr.table_sys_id);
valid = true;
}
}
return valid;
},
type: 'getAttachmentCount'
});
Please help me on how should I achieve this requirement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2025 11:59 PM
it should work fine
try this
function onSubmit() {
//Type appropriate comment here, and begin script below
if (g_form.getValue('file_upload_url').toString() != '') {
try {
if (window == null) {
// portal
if (this.document.getElementsByClassName('get-attachment').length == 0) {
alert('Please attach Photo(s) or add URL to proceed with the submission of your request');
return false;
}
}
} catch (ex) {
// native view
var length = $j("li.attachment_list_items").find("span").length;
if (length == 0) {
alert('Please attach Photo(s) or add URL to proceed with the submission of your request');
return false;
}
}
}
}
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-01-2025 12:03 AM
I don't think the below piece of code is working correctly. I tried multiple scripts and then using the script include where it passes the uniqueValue of the record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2025 12:16 AM
that's what I already shared and this will work fine in Portal
For native I am still checking
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-01-2025 12:27 AM
this worked for both native and portal
Ensure Isolate Script = False for your client script
function onSubmit() {
//Type appropriate comment here, and begin script below
if (window == null) {
// portal
var count = this.document.getElementsByClassName('get-attachment').length;
if (count == 0) {
alert('Please attach Photo(s) or add URL to proceed with the submission of your request');
return false;
}
} else {
// native view
var length = $j("li.attachment_list_items").find("span").length;
if (length == 0) {
alert('Please attach Photo(s) or add URL to proceed with the submission of your request');
return false;
}
}
}
Output:
I believe I have answered your question.
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-01-2025 12:31 AM
I am doing this on custom portal where we have custom widget for attachments