How to: Make attachments mandatory in Employee Center

Casey23
Tera Guru

Hello everyone,

 

I was looking around for a way to make the default attachment section on a catalog item mandatory, and found that a lot of the community posts out there seem to be outdated and don't work in Employee Center. I wanted to share the fix that I ultimately settled on in hopes that it saves others some time.

 

Scenario:

A catalog item can be submitted to request something, or to request reimbursement for that item. Attachments are only needed when the request is for reimbursement. This rules out the OOB 'Mandatory Attachment' checkbox since the attachment is based on a condition. We also don't know how many attachments the user will need to submit, so using an attachment variable also doesn't work in this scenario.

 

Solution:

Create an onSubmit client script that will check for the request type (indicated by a variable), and then prompt the user with an error message if no attachments are found when they click 'Order Now'.

function onSubmit() {
   var requestType = g_form.getValue('tr_i_want_to');   // Variable to indicate the type
    if(requestType == 'reimbursement'){     // The specific value you want this script to fire on
        var hasAttachment = false;
        var errorMessage = "Your error message here.";

        try {
            var angularElement = null;
            if (typeof jQuery !== 'undefined' && jQuery('#sc_cat_item').length) {
                angularElement = jQuery('#sc_cat_item');
            } else if (typeof angular !== 'undefined') {
                angularElement = angular.element('#sc_cat_item');
            }

            if (angularElement && angularElement.scope() && angularElement.scope().attachments) {
                // If the array length is greater than 0, something is attached
                if (angularElement.scope().attachments.length > 0) {
                    hasAttachment = true;
                }
            }
        } catch (e) {
            hasAttachment = false;
        }

        // If no attachments are found, show the error message
        if (!hasAttachment) {
            g_form.clearMessages();
            g_form.addErrorMessage(errorMessage);
            return false;
        }
    }   
}

 

Hope this helps!

 

0 REPLIES 0