File name validation in record producer

yugesh k
Tera Contributor

Hi All,

 

I have created attachment variable "certificate" in record producer if user trying upload file that file name should be "certificate" only if not then file should not upload.

Thanks for you quick response.

1 ACCEPTED SOLUTION

SwarnadeepNandy
Mega Sage

Hello @yugesh k,

 

One possible way to achieve this is to use a client script with an onSubmit function that validates the file name and file type of the attachment variable before submitting the record producer. You can use the GlideSysAttachment API to access the attachment information and compare it with the expected format. You can also use g_form.addErrorMessage() to display an error message if the validation fails and prevent the submission. Here is an example of how such a script might look like:

function onSubmit() {
  // Get the sys_id of the record producer
  var rpSysId = g_form.getUniqueValue();
  // Get the attachment variable name
  var attVarName = "certificate";
  // Get the expected file name and file type
  var expectedFileName = "certificate";
  var expectedFileType = "pem";//Your allowed file type
  // Create a GlideSysAttachment object
  var gsa = new GlideSysAttachment();
  // Get the attachments for the record producer
  var attList = gsa.getAttachments(rpSysId);
  // Loop through the attachments
  while (attList.next()) {
    // Get the attachment variable name
    var attVar = attList.getValue("table_name").split(".")[1];
    // Check if it matches the attachment variable name
    if (attVar == attVarName) {
      // Get the file name and file type
      var fileName = attList.getValue("file_name");
      var fileType = attList.getValue("content_type");
      // Check if they match the expected values
      if (fileName != expectedFileName || fileType != expectedFileType) {
        // Display an error message and cancel the submission
        g_form.addErrorMessage("Invalid file name or file type for " + attVarName + ". Please upload a file with name " + expectedFileName + " and type " + expectedFileType + ".");
        return false;
      }
    }
  }
}

 

Hope this helps.

 

Kind Regards,

Swarnadeep Nandy

View solution in original post

5 REPLIES 5

Hi @yugesh k did this work for you?