Make attachment field mandatory on selection of a specific value in choice field on incident form

Srinivas K
Tera Contributor

On Incident form there is a choice type (value-A, value-B)

If user selects the value- A, then attachment should be mandatory.

Please guide with the approach.

1 ACCEPTED SOLUTION

Hi @Srinivas K 

You can use below logic in before insert or update BR:

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

    // Add your code here
    var grA = new GlideRecord("sys_attachment");
    grA.addQuery("sys_id", current.u_file_attachment_1);  //replace with your attachment field name
    grA.query();
    if (grA.next()) {
        if (grA.getValue("file_name").indexOf("doc") == -1) {
            current.setAbortAction(true);
            gs.addErrorMessage("Please enter the correct document name");
        }
    }

})(current, previous);

(=tested)

Hope it helps:)

 

Thanks,
Murthy

View solution in original post

3 REPLIES 3

Murthy Ch
Giga Sage

Hi @Srinivas K 

You can watch the below video for making an attachment mandatory in native UI level.
https://www.youtube.com/watch?v=mBCuGw2GhpE&t=436s 

Hope it helps to resolve your query.

 

Thanks,
Murthy

Hi Murthy Ch,

 

My requirement is little bit different.

I am having a field (Ex: Document) with type - File attachment on the form.

 

When choice A is selected I am able to make the Document field mandatory.

But I also want to check whether the attached document contains particular word 'doc' in it.

If it is not there, then it has to abort , else it has to proceed.

How can I achieve it.

I am able to achieve this for the global attachment (on the top) , but not in the custom field that I have created.

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

if (current.operation() == "update") {
checkUpdate();
}
function checkUpdate()
{
if(!gs.nil(current.sys_id))
{
var grAttach = new GlideRecord('sys_attachment');
grAttach.addQuery('table_name', current.getTableName());
grAttach.addQuery('table_sys_id', current.sys_id);
grAttach.addQuery("file_name",'CONTAINS','doc');

grAttach.query();
if (!grAttach.next()) {
gs.addErrorMessage('Missing Document attachment'); // Message to be displayed to user
}
current.setAbortAction(true);
}}

})(current, previous);

 

Please guide here

 

@Ankur Bawiskar 

Hi @Srinivas K 

You can use below logic in before insert or update BR:

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

    // Add your code here
    var grA = new GlideRecord("sys_attachment");
    grA.addQuery("sys_id", current.u_file_attachment_1);  //replace with your attachment field name
    grA.query();
    if (grA.next()) {
        if (grA.getValue("file_name").indexOf("doc") == -1) {
            current.setAbortAction(true);
            gs.addErrorMessage("Please enter the correct document name");
        }
    }

})(current, previous);

(=tested)

Hope it helps:)

 

Thanks,
Murthy