How to make attachment mandatory based on condition for record producer in service portal?

Srinivas Goud K
Tera Contributor

I need to make attachment is mandatory based on condition. There is a check box in the form If I select that one then it should become attachment mandatory how to implement that please help me out.

I have used below Script but it is become mandatory for entire form I want it for particular variable only

Please check attachment 

function onSubmit() {

    var cat_id = g_form.getValue('sysparm_item_guid');
    var gr = new GlideRecord("sys_attachment");
    gr.addQuery("table_name", "sc_cart_item");
    gr.addQuery("table_sys_id", cat_id);
    gr.query();
    if (!gr.next()) {
        alert("Attachment is mandatory if you select Preapproved");
        return false;
    }
}

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron

Hi,

I would suggest to use the attachment type variable for attaching the attachment and us UI policy to make it mandatory based on your required conditions.

You can achieve it with out scripting and It is recomanded as a best practices.

 

Thanks,

Sagar Pagar

The world works with ServiceNow

View solution in original post

6 REPLIES 6

Ghulam Gose Ala
Tera Contributor

Write an on submit Catalog Client script and put the backend value of your checkbox variable in place of "YOUR_Check_box".

 

 

function onSubmit() {
    //Type appropriate comment here, and begin script below  
    var catalogItemID = g_form.getValue('sysparm_item_guid');
    var test = g_form.getValue('YOUR_Check_box');

    if (test == 'true') {

 

        var count = getSCAttachmentCount();
        if (count <= 0) {
            getMessage('mandatory_attachment', function(msg) {
                g_form.addErrorMessage(msg);
            });
            return false;

        }
    } else {

        return true;
    }
}

 

Mark ✅ Correct if this solves your issue and also mark ???? Helpful if you find my response worthy based on the impact.
Thanks,

Community Alums
Not applicable

Getting error in console getSCAttachmentCount is not defined.