How to make " Add attachments" mandatory and prevent from submission based on Variable selection

Naresh_5120
Tera Contributor

Dear Community,

 

I need some suggestion to make " Add attachments" mandatory in catalog item and prevent from submission the catalog item form  based on Yes/NO Type Variable selection. If Variable called "do_have_approval" is Yes then make the " Add attachments" mandatory in catalog item and prevent from submission the catalog item form, if attachment is attached then allow to submit the form. 

Naresh_5120_0-1764659524713.png

 



I am trying to achieve this through catalog client script but its not working.

1 ACCEPTED SOLUTION

Sarthak Kashyap
Mega Sage

Hi @Naresh_5120 ,

 

I tried your problem in my PDI and it works for me please check solution below 

 

I created 2 variables on my Catalog item 1. Is Approval(Yes/No), 2. Attachment (attachment type)

SarthakKashyap_0-1764661617249.png

 

 

I created one OnSubmit Client script and add below code 

SarthakKashyap_1-1764661645940.png

 

function onSubmit() {
   //Type appropriate comment here, and begin script below

   var approval = g_form.getValue('is_approval');  // Yes/No variable

    if (approval === 'Yes') {

        var attCount = g_form.getControl('add_here').attachments
            ? g_form.getControl('add_here').attachments.length
            : 0;

        if (attCount === 0) {
            g_form.addErrorMessage("Attachment is required when approval is Yes.");
            return false;  
        }
    }

    return true;
   
}

 

Result

When I select yes and try to submit it shows me error

SarthakKashyap_2-1764661693151.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regard,

Sarthak

View solution in original post

3 REPLIES 3

Sarthak Kashyap
Mega Sage

Hi @Naresh_5120 ,

 

I tried your problem in my PDI and it works for me please check solution below 

 

I created 2 variables on my Catalog item 1. Is Approval(Yes/No), 2. Attachment (attachment type)

SarthakKashyap_0-1764661617249.png

 

 

I created one OnSubmit Client script and add below code 

SarthakKashyap_1-1764661645940.png

 

function onSubmit() {
   //Type appropriate comment here, and begin script below

   var approval = g_form.getValue('is_approval');  // Yes/No variable

    if (approval === 'Yes') {

        var attCount = g_form.getControl('add_here').attachments
            ? g_form.getControl('add_here').attachments.length
            : 0;

        if (attCount === 0) {
            g_form.addErrorMessage("Attachment is required when approval is Yes.");
            return false;  
        }
    }

    return true;
   
}

 

Result

When I select yes and try to submit it shows me error

SarthakKashyap_2-1764661693151.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regard,

Sarthak

vaishali231
Tera Contributor

You are using a strict mode catalog client script, so attachment APIs like getAttachments or getEncodedRecordAttachments are blocked. Because of this, we cannot validate attachments on the client side.

The correct solution is to use a simple onSubmit client script for the Yes or No check, and then enforce attachment validation on the server side.

Step 1. Client Script onSubmit

function onSubmit() {
var needApproval = g_form.getValue('do_have_approval');
if (needApproval == 'Yes') {
g_form.addErrorMessage('Attachment is required Please upload before submitting.');
return false;
}
return true;
}

Step 2. Server Side Validation (Business Rule)

This ensures the form cannot be submitted without an attachment if the user selects Yes.

Table sc_req_item
When Before insert

(function executeRule(current,previous) {

var needApproval = current.variables.do_have_approval + '';

if (needApproval == 'Yes') {
    var att = new GlideAggregate('sys_attachment');
    att.addQuery('table_sys_id', current.sys_id);
    att.addAggregate('COUNT');
    att.query();

    var count = 0;
    if (att.next()) {
        count = parseInt(att.getAggregate('COUNT'), 10);
    }

    if (count === 0) {
        gs.addErrorMessage('Attachment is required when approval is Yes.');
        current.setAbortAction(true);
    }
}


})(current,previous);

This combination works in strict mode and is fully supported. Even if the user skips the client message, the server will block the submission until an attachment is added.

vaishali231_2-1764662129581.png

 

vaishali231_1-1764662080647.png

 

vaishali231_0-1764662052188.png

 

soumyadeep10
Tera Guru

Hi @Naresh_5120 ,

This can be done without any script as well if you want to. What you need to do is:

1) Create a UI Policy for the Yes/No field and in the 'When' condition mention the value to be 'Yes'

2) In the UI policy action, select the attachment variable and make 'mandatory' option as true.

soumyadeep10_0-1764662747674.pngsoumyadeep10_1-1764662764214.png

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.