Restrict submit if there are no attachments attached on the form in Custom Portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2025 11:52 AM
Hi All,
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.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2025 05:48 PM - edited ‎05-01-2025 05:50 PM
See my answer for How can i get the atttachments sys_id before Submit a form? for ideas.
Accessing the record sys_id to be created:
Yes, getUniqueValue() just returns the sys_id of the current cat_item/record_producer you're on. The only way that I know of to get the sys_id of the record that's going to be created is via accessing the angular scope of your form (thus this relies on the DOM). This is getting into undocumented territory so use this at your own risk (although this property seems to have been around for years, so should be fairly safe):
var $scope = this.angular.element("#sc_cat_item").scope();
var toBeTableName = $scope.data._attachmentTable;
var toBeSysId = $scope.data._generatedItemGUID;
The toBeSysId is what you'd send as your param, and you can sent the toBeTableName if you wish to query that also: gr.addParam("sys_attach", toBeSysId);
* Note: Returning false from an asynchronous callback as you're doing in your code above will not stop form submission as you're not returning from the onSubmit handler anymore, but rather from the callback. See KB0783579 on how you can perform asynchronous validation if you continue down this path.
Using a client script to get the attachment count:
Alternatively, you can skip the GlideAjax call and read the attachment data from the current form scope, no need to contact the server:
var $scope = this.angular.element("#sc_cat_item").scope();
var hasAttachments = $scope.attachments.length > 0;
if(!hasAttachments) {
alert('Please attach Photo(s) or add URL to proceed with the submission of your request');
return false;
}
Use an attachment variable:
Use the OOTB attachment variable type that you can make mandatory and visible when the file_upload_url variable is empty. This is probably the easiest, most robust and maintainable solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2025 07:45 PM
I already answered your question yesterday
Restrict submit if there are no attachments attached on the form depending on certain conditions
sharing here again
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:
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-07-2025 09:21 PM
Hope you are doing good.
Did my reply answer 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