logo

NJP

To allow attachments of specific file type only - Eg: Allow only image attachment

Import · Feb 03, 2020 · article

Hi Guys,

The following script will allow only image attachments(.jpg, .jpeg, .png) on the service catalog.

>>onSubmit catalog client script:

function onSubmit() {
try
{
//for non-portal
var sysid =g_form.getElement('sysparm_item_guid').value;
var rowcount=0;
var attachment = new GlideRecord("sys_attachment");
attachment.addQuery("table_name", "sc_cart_item");
attachment.addQuery("table_sys_id", sysid);
attachment.query();
while (attachment.next()) {
rowcount++;
}

if(rowcount==0){ // Missing {}
alert("Please attach Image for submitting this request(.jpg/.jpeg/png format)!");
return false; 
}
if(rowcount == 1 && (attachment.content_type.indexOf("jpg") != -1 || attachment.content_type.indexOf("jpeg") != -1 || attachment.content_type.indexOf("png") != -1)){
//alert(attachment.content_type.indexOf("jpg") + ' ,'+attachment.content_type.indexOf("jpg")+'...'+' ,'+attachment.content_type.indexOf("jpg")+ value);
return true; 
}
else
{
    alert("Attachment should be in only .jpg/.jpeg/png format!");
    return false;
}

}

catch(e)
{ 
//for-portal
if(this.document.getElementsByClassName('get-attachment').length ==1) 
{
    var value = (this.document.getElementsByClassName('get-attachment')[0].innerHTML).toLowerCase();
    //alert(value.indexOf('jpg') + ' ,'+value.indexOf('jpeg')+'...'+' ,'+value.indexOf('png')+ value);
    if(this.document.getElementsByClassName('get-attachment').length <1)
    {
    alert( "Please attach Image for submitting this request(.jpg/.jpeg/png)!");
    return false;
    }
    else if ((value.indexOf('jpg') != -1 || value.indexOf('jpeg')  != -1 || value.indexOf('png')  != -1))
    {
    return true;
    }    
    else
    {
    alert ("Attachment should be in only .jpg/.jpeg/png format!"); 
    return false;
    }
}
}
}

You can also refer this article - Possible Ways for Making an Attachment Mandatory : Service Portal/Native UI

Mark this article helpful, if its helpful.

Regards,

Snehal

View original source

https://www.servicenow.com/community/developer-articles/to-allow-attachments-of-specific-file-type-only-eg-allow-only/ta-p/2323687