- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 08:20 PM
Hello,
The following code suppose to checks if the attachment in the service request count exceeds 1, prevents form submission from Self Service Portal if it does. However, the code is not working correctly. If there a better method of checking beside using client script and script include?
CATALOG CLIENT SCRIPT:
function onSubmit() {
// Get the current record sys_id and table name
var recordSysId = g_form.getUniqueValue();
var tableName = g_form.getTableName();
// Call the Script Include using GlideAjax
var ga = new GlideAjax('AttachmentChecker');
ga.addParam('sysparm_name', 'checkAttachmentCount'); // Corrected the parameter name
ga.addParam('sys_id', recordSysId);
ga.addParam('table', tableName);
ga.getXMLAnswer(function(response) {
var attachmentCount = parseInt(response, 10); // Corrected the way to retrieve the answer
if (attachmentCount > 1) {
// Display an error message and prevent form submission
alert(attachmentCount);
g_form.addErrorMessage('You cannot attach more than 1 file to this record.');
g_form.submit(false); // Prevent form submission
} else {
g_form.submit(true); // Allow form submission
}
});
return false; // Prevent the default form submission until we get the response from the server
}
SCRIPT INCLUDE:
var AttachmentChecker = Class.create();
AttachmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkAttachmentCount: function() {
var recordSysId = this.getParameter('sys_id');
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_sys_id', recordSysId);
grAttachment.query();
var attachmentCount = 0;
while (grAttachment.next()) {
attachmentCount++;
}
return attachmentCount;
},
type: 'AttachmentChecker'
});
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 09:24 PM
@Meera_P Here is an article written by Ankur on checking the count of attachment using client script https://www.servicenow.com/community/developer-blog/verify-mandatory-attachments-count-on-catalog-it...
Hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 09:24 PM
@Meera_P Here is an article written by Ankur on checking the count of attachment using client script https://www.servicenow.com/community/developer-blog/verify-mandatory-attachments-count-on-catalog-it...
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 09:51 PM
Thank you very much @Sandeep Rajput . You have saved me a tremendous amount of time and effort by provided me the article written by Ankur. It is working perfectly.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 09:57 PM
Glad to know it helped. Feel free to reach out in future if you need any help on ServiceNow related topics.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 10:08 PM
Learning this new technology has been tough, but the ServiceNow community has been a huge help. I really appreciate their support and how they have helped me overcome obstacles as I learn. Thank you everyone!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2024 09:27 PM
@Meera_P - You can make use of the variable type Attachment or if you want to proceed with the regular attachment option then you got make user of scratchpad and a global variables on top of the attachment count check.
Ref:
https://www.servicenow.com/community/developer-articles/async-validation-in-onsubmit-catalog-client-....