Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Attachment name Validation on Service portal Before Catalog Item Submission

Natraj S
Tera Expert

Hello All,

 

We have a requirement where we have to validate the attachment name in service portal before catalog item name submission.

 

Example:

The attachment  name should be "variance access form.xlsx", if user is trying to attach some other attachment we have to restrict the catalog item submission.

 

Thanks in Advance!

2 REPLIES 2

Community Alums
Not applicable

@Natraj S To validate the attachment name before submitting a catalog item in the ServiceNow Service Portal, you can use a client script to check the attachment name and prevent submission if it doesn't match the expected format. Here’s a solution using a onSubmit catalog client script, something like this and remove loop if there are not multiple attachments to verify,

function onSubmit() {
var expectedFileName = "variance access form.xlsx";
var isValid = false;

// Get all attachments associated with the catalog item
var ga = new GlideAjax('AttachmentAjax');
ga.addParam('sys_id', g_form.getUniqueValue());
ga.addParam('sysparm_name', 'getAttachments');
ga.getXMLAnswer(function(response) {
var attachments = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));

// Check if any attachment matches the expected file name
for (var i = 0; i < attachments.length; i++) {
if (attachments[i].file_name === expectedFileName) {
isValid = true;
break;
}
}

// If not valid, show error and prevent submission
if (!isValid) {
g_form.addErrorMessage("Please attach the correct file: " + expectedFileName);
return false;
}
});
}

Hope this will help you.

Natraj S
Tera Expert

Hi @Community Alums ,

Thanks for your response!

I have tried the above code, but it did not work.