Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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 

 

2 ACCEPTED SOLUTIONS

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.

View solution in original post

@nameisnani 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

6 REPLIES 6

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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@nameisnani 

then this will work in both

function onSubmit() {
    //Type appropriate comment here, and begin script below
    try {
        var requestType = g_form.getValue('request_type');
        var count = getRPAttachmentCount();
        if (window == null && (requestType == 'Bank Account Maintenance' || requestType == 'Scheduler Maintenance')) {
            // portal
            if (count == 0) {
                alert('Attachment is mandatory for Bank Account Maintenance or Scheduler Maintenance requests.');
                return false;
            }
        }
    } catch (ex) {
        // native view
        var length = getSCAttachmentCount();
        var requestType1 = g_form.getValue('request_type');
        if (length == 0 && (requestType1 == 'Bank Account Maintenance' || requestType1 == 'Scheduler Maintenance')) {
            alert('Attachment is mandatory for Bank Account Maintenance or Scheduler Maintenance requests.');
            return false;
        }
    }
}

function getRPAttachmentCount() {
    var length;
    try {
        length = angular.element("#sc_cat_item_producer").scope().attachments.length;
    } catch (e) {
        length = -1;
    }
    return length;
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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