Clear value of an attachment field

andrea318lu
Tera Contributor

Hi!

I have an issue with managing a file attachment field. I created an onChange client script that should delete the file when another field's value changes. I'm using an AJAX call to connect to a script include, which deletes the file from the sys_attachment table.

The deletion works, but until I reload the page, the attachment field still appears populated even though the file no longer exists in the sys_attachment table.

Is there a way to refresh the attachment field or clear the value correctly without requiring a full page reload?

Thanks!

2 REPLIES 2

Mark Manders
Giga Patron

Not if you don't put a refresh of the page in your script. Attachments are stored somewhere else and related to your record. Only when your record is reloaded, it won't show that relationship anymore. But why not just update the field itself to '' within your client script? That should work as well, since the attachment is being deleted.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Lucas Silva dos
Tera Contributor

 Hi,

 

I was able to solve this by reusing the same client-side function that ServiceNow triggers when an attachment is manually deleted from the attachment field.

 

After inspecting the page behavior, I noticed that the platform calls a client-side function responsible not only for deleting the attachment, but also for refreshing the attachment field UI. Because of that, using only GlideAjax deleted the record from sys_attachment, but did not update the field/widget state on the form.

 

In my scenario, calling the existing client-side delete function solved the issue, and I no longer needed the GlideAjax call.

 

Here is the example:

 

// Calls the same function triggered by the manual delete action in the UI
var attId = g_form.getValue('additional_documents');
var table = g_form.getTableName(); // Example: x_brssa_proc_hiring
var field = 'additional_documents';
var recId = g_form.getUniqueValue();

if (attId && typeof window.deleteFileAttachment === 'function') {
    // This updates the attachment field/widget UI correctly in most cases
    window.deleteFileAttachment(attId, table, field, recId);
}
Just replace "additional_documents" with the name of your attachment field.