Conditional Mandatory Attachment for Catalog Item

nameisnani
Mega Sage

Hi Team,

 

I have a requirement on a Service Catalog Item where an attachment should be mandatory based on a

variable selection.

 

Requirement

 

  • Catalog Item has a Select Box variable: request_type

  • Values:

    • Bank Account Maintenance

    • Scheduler Maintenance

    • Other

  • When request_type = Bank Account Maintenance OR Scheduler Maintenance, an attachment must be mandatory

  • For Other, attachment is not required


    What I Tried

    I implemented a Catalog Client Script (onSubmit) to validate attachments using g_form.getAttachments().



function onSubmit() {

    var requestType = g_form.getValue('request_type');

    // Check only for required options
    if (requestType == 'Bank Account Maintenance' || requestType == 'Scheduler Maintenance') {

        var attachments = g_form.getAttachments();

        if (!attachments || attachments.length === 0) {
            alert('Attachment is mandatory for Bank Account Maintenance or Scheduler Maintenance requests.');
            return false; // Stop submission
        }
    }

    return true;
}

 

 

Not working - any one please help me 

 

3 REPLIES 3

Viraj Hudlikar
Tera Sage

Hello @nameisnani 

function onSubmit() {

    var requestType = g_form.getValue('request_type');

    // Check only for required options
    if (requestType == 'Bank Account Maintenance' || requestType == 'Scheduler Maintenance') {

	if (this.document.getElementsByClassName('get-attachment').length == 0)	{    
            alert('Attachment is mandatory for Bank Account Maintenance or Scheduler Maintenance requests.');
            return false; // Stop submission
        }
    }

    return true;
}

 

If my response has helped you, hit the helpful button, and if your concern is solved, do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Ankur Bawiskar
Tera Patron

@nameisnani 

you want this validation in native or portal OR Both?

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