The Zurich release has arrived! Interested in new features and functionalities? Click here for more

About drag and drop of attachments

bonsai
Mega Sage

I want a popup to appear when I drag and drop an attachment onto a record.

 

I can operate the attachment screen on the UI page, but which record should I customize when attaching by drag and drop?

When attaching a file by drag and drop, I want to display a popup saying "Caution: Do not attach files with the same file name."

5 REPLIES 5

Bert_c1
Kilo Patron

You can achieve that by defining a business rule on the 'sys_attachment' table that runs on Insert and Before. with script logic:

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var fileName = current.file_name;
	var attachmentTable = new GlideRecord('sys_attachment');
	attachmentTable.addQuery('file_name', fileName);
	attachmentTable.query();
	gs.info("Found " + attachmentTable.getRowCount() + " attachment records with " + fileName);
	if (attachmentTable.next()) {
		gs.info("Duplicate file name");
		gs.addErrorMessage("Do not add duplicate file: " + fileName + "!");
		current.setAbortAction(true);
	}

})(current, previous);

 

The user must refresh the incident form though to see the attachment is missing.

Is it possible to display a popup?

 

I understand that this can be handled using business rules.

If possible, I would like to display a popup so that the file is saved when OK is pressed.

I'm not sure how that would be done in a business rule. I don't know how to alter that aspect of OOB behavior.

M Talha
Tera Expert

Steps to Create a Popup for Drag-and-Drop Attachments in ServiceNow

  1. Identify Target Table: Determine which table (e.g., Incident) you want to customize for attachments.

  2. Create Client Script:

    • Navigate to System Definition > Client Scripts.
    • Click New and fill in:
      • Name: Enter a descriptive name.
      • Table: Select the target table.
      • Type: Set to onLoad.
  3. Add Script: Insert the provided JavaScript code to check for duplicate filenames and display a popup.

  4. Test the Functionality:

    • Open a record in the target table.
    • Drag and drop a file to see if the popup appears for duplicates.
  5. Submit: Save your Client Script and verify its functionality.

This will enable a popup warning for duplicate filenames during drag-and-drop uploads. Let me know if you need anything else!

If you find this helpful, please mark it as helpful!