Delete Attachment if the correct value is not selected với Client Script

Chronos
Tera Contributor

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,

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@Chronos 

 

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');
    }
}

Screenshot 2023-04-15 at 12.23.45 PM.png

 

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'
});

Screenshot 2023-04-15 at 12.25.16 PM.png

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.

Rahul Kumar17
Tera Guru

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:

  1. Add an "onChange" Client Script to your field.
  2. Inside the Client Script, add a condition to check if the value of the field is "1".
  3. 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.
  4. 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

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

FYI, I tried this in Vancouver for a similar exercise, and it looks like the deleteAttachment() function is no longer available.

g_form_del_attachment_err_Vancouver.PNG