How to validate attachment file name and its format in Service Catalog?

Farooq
Kilo Contributor

Hello everyone,

I have a requirement to validate the attachment filename and the format, when submitting a request in Service Catalog. It should validate the name as, "filename.xlsx", if it's not the correct filename or format. It should give error message to user and user should not be able to submit a request without proper filename convention. How can I accomplish this using Catalog Client Script? Thank you

5 REPLIES 5

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi @Farooq 

for doing this, you need to use DOM manipulation, which is highly not recommended by ServiceNow.

Please refer to the following thread:

https://community.servicenow.com/community?id=community_question&sys_id=6b10f79edb5fd0144aa5d9d96896...

Regards

Alberto

SanjivMeher
Kilo Patron
Kilo Patron

sachin_namjoshi
Kilo Patron
Kilo Patron

Use below solution

 

https://community.servicenow.com/community?id=community_question&sys_id=47164f61db1cdbc01dcaf3231f961904

 

But, please make sure that you do not do GlideRecord() in client script.

Instead, use glide ajax.

 

Regards,

Sachin

Sailesh4
Giga Expert

Hi Farooq,

 

You can use the below Business rule to complete your requirement.

 

Before-Insert (Business rule)

Table: Item(sc_cart_item)

In condition give your catalog item name. Just like the below Screenshot

find_real_file.png

Use the below script.

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

    var gr = new GlideRecord('sys_attachment');
    gr.addQuery("table_sys_id", current.sys_id);
    gr.addQuery('file_name', 'STARTSWITH', 'filename.xlxs');
    gr.addQuery('content_type', 'application/xlsx');
    gr.query();
    if (!gr.next()) {
        current.setAbortAction(true);
        gs.addErrorMessage("Attachment filename should start with FILENAME and Attachment type should be XLXS format.");
    }
})(current, previous);

Please mark as correct if this solves your issue or mark as helpful.

 

Thanks,

Sailesh J