Delete Attachment if the correct value is not selected với Client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 08:01 PM
Hi,
I am having the following problem, my field choice list has values 1 and 2. I use Client Script [onChange] when I select the value 2, there will be an icon [Manage Attachment] to add files. When I select the value 1, [Manage Attachment] will disappear and no more files will be added, but I want when I select the value 1, the existing files will be deleted and not displayed anymore. Does anyone know how to do it.
Thanks,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2023 11:57 PM
Here is how I implemented this requirement on my incident table, if the incident impact is high or medium a user can upload the attachment and if the incident impact changes to low, all the attachments so far get deleted from the current incident record.
Here is the onChange client script on the impact field.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
//impact is low
if (newValue == '3') {
var glideDelete = new GlideAjax('MyClient');
glideDelete.addParam('sysparm_name', 'deleteAttachments');
glideDelete.addParam('sysparam_incident', g_form.getUniqueValue());
glideDelete.getXML(deleteCallback);
}
function deleteCallback(response) {
alert('attachments deleted successfully');
}
}
Here is the script include script
var MyClient = Class.create();
MyClient.prototype = Object.extendsObject(AbstractAjaxProcessor, {
deleteAttachments: function() {
var incident = this.getParameter('sysparam_incident');
var glideAttachment = new GlideRecord('sys_attachment');
glideAttachment.addQuery('table_sys_id',incident);
glideAttachment.query();
while(glideAttachment.next()){
glideAttachment.deleteRecord();
}
},
type: 'MyClient'
});
You can convert this implementation according to your on need. Also, make sure to refresh the form once you see the attachment deleted alert otherwise the existing form would give you an impression that the attachment has not been deleted.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2023 04:38 AM
Hi Chronos,
Yes, it is possible to achieve this functionality using a Client Script in ServiceNow. You can use the following steps to implement this:
- Add an "onChange" Client Script to your field.
- Inside the Client Script, add a condition to check if the value of the field is "1".
- If the value is "1", you can use the "g_form.deleteAttachment()" function to delete any existing attachments. You can also use the "g_form.hideRelatedList()" function to hide the attachment related list.
- If the value is "2", you can use the "g_form.showRelatedList()" function to show the attachment related list.
Here is an example Client Script that implements this functionality:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue == "1") {
g_form.deleteAttachment("attachment_field_name"); // replace "attachment_field_name" with the actual name of your attachment field
g_form.hideRelatedList("attachment_field_name"); // replace "attachment_field_name" with the actual name of your attachment field
} else if (newValue == "2") {
g_form.showRelatedList("attachment_field_name"); // replace "attachment_field_name" with the actual name of your attachment field
}
}
Note: Make sure to replace "attachment_field_name" with the actual name of your attachment field in the code.
Thanks,
Rahul Kumar
Thanks,
Rahul Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2024 08:49 AM
FYI, I tried this in Vancouver for a similar exercise, and it looks like the deleteAttachment() function is no longer available.