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

raphaelcrv
Kilo Guru

Hi Lean,

 

Please refer below link and adjust it as per your req.

 

Mandatory Attachment Before State Change

If my response was helpful and/or provided a solution, please consider marking it as such. Thank you!

Harshal Gawali
Giga Guru

Hello Lean,

You can also write before insert and update business rule to check mandatory attachment.

For reference check following script.

Before insert BR :

var attachRec = new GlideRecord("sys_attachment");



attachRec.addQuery("table_name", current.getTableName());



attachRec.addQuery("table_sys_id", current.sys_id);



attachRec.query();



if (!attachRec.next()) {



      gs.addErrorMessage("You must attach a file before submitting.");



      current.setAbortAction(true);



}

 

Regards,

Harshal

 

Hi Harshal,

Thanks for the response on this. If I want to make UI action script stop triggering before any attachment is attached. Is that BR will stop that UI action running, in my testing now I saw UI action is running backend. Kindly suggest

 

 

Thanks,

Lean S

 

@Lean S 

what I could get from question is that you want attachment to be checked before UI action code runs

So the above code would stop the UI action code from running.

You can handle it in other way. Show UI action only when there is some attachment to the current record

In the UI action condition add this

current.hasAttachments()

Regards
Ankur

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