How to check attachments upon submitting catalog item?

Lakshmiprasann6
Tera Contributor

Hi,

I have a query regarding attachments its always having a confusion with attachments. While submitting form we need to check if user has attached 2 attachments we should allow to submit else abort the action.

How can we achieve this requirement and what is the best practice in Servicenow? I even referred DOM manipulation and some sort of leads whereas using GlideRecord in onSubmit which is not preferrable.

Please help me to get this requirement.

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron
5 REPLIES 5

asifnoor
Kilo Patron

Hello Lakshmi,

Technically you should try to avoid validations in onSubmit which requires an dependency from the server side script. Because by the time you try to fetch data from SN, your form will be already submitted.

The best approach is to do this in before insert BR on sc_req_item table and add the script

var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id",current.sys_id.toString());
gr.query();
if(gr.getRowCount()<2) {
 gs.addErrorMessage("please upload atleast 2 attachments");
 current.setAbortAction(true);
}

Mark the comment as a correct answer and also helpful if this helped to solve the problem.

Hi ,

As You recommended I made changes in PDI instance and still I can able to submit the form.

find_real_file.png

Hi,

I assume request is submitted, but not the ritm. 

If you want to restrict this on form itself, then instead of using default attachments, tryto use a custom field of type attachment and make it mandatory. and then on load make g_form.submitted as false so that the form is not submitted and then write your logic in on Sumbit and all good make submitted attribute as true and submit the from through script using g_form.submit();

Mark the comment as a correct answer and also helpful if this helped to solve the problem.