attachment check on UI action

Lean S
Mega Expert

Hi,

How can we make attachment mandatory if not attached to form on clicking UI action. Basically Before any action after clicking button, it has to check attachment and a field value should be filled, if not i has to alert user and stop UI action functionality going further. Kindly help with this scenario.

 

Thanks,

Lean S

13 REPLIES 13

Hi Ankur,

Thanks for your suggestion, it is useful. But in this case the button is visible only after I attach and save, actually I am looking for something below:

I have a field Refer to (reference field), if Refer to field is empty and no attachment attached then I should to allow user to click on button or make no action in the backend and alert user.

Kindly suggest.

 

 

Thanks,

Lean S

@Lean S 

It is not recommended to show/hide the UI actions using client scripts etc.

Usually they are show/hidden based on the condition it satisfies.

In your case you can add condition to your UI action as per your requirement.

current.<refer> == '' && !current.hasAttachments()

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

Can there be a way to use Onclick() and check if attachment is not attached and alert user and then if it satisfies then rest of UI action code should run.

 

 

Thanks,

Lean S

Hi @Lean S ,

 

You can create a Client Script of type "onSubmit", so that it gets triggered when your form is submitted by the button click.

In the client script, you can use the code sample below (make sure to replace the table and the ref field names with your names):

function onSubmit() {
    //get your ref_field value
    var ref_field = g_form.getValue('your_ref_field_name');
	
    var attachments = new GlideRecord("sys_attachment");
    attachments.addQuery("table_name", "your_table_name");
    attachments.addQuery("table_sys_id", g_form.getUniqueValue());
    attachments.query();

    //if ref_field is empty and no attachments are there alert user
    if (!ref_field && !attachments.hasNext()) {
        g_form.addErrorMessage("Please add an attachment before submitting.");
        return false;
    }

}

 

Thanks & Regards,

Rishabh Jha

Aavenir (https://www.aavenir.com/)

Hi Rishabh,

Thanks, this is working but how we can make this work for my custom UI action button instead of Submit button. 

Kindly suggest.

 

Thanks,

Lean S