Verify a File is Attached to a Record Producer on the Service Portal

jmiskey
Kilo Sage

On our Service Portal, we have a Record Producer that creates a SAFE Epic (sn_safe_epic) record.  Through integration, these records are being transferred over to Azure DevOps upon submission into ServiceNow.

 

The issue is that under certain scenarios, the users are required to attach a file to the Record Producer record on the Service Portal before submitting.  We want to not allow them to submit the request if they have not attached any required files.

 

Unfortunately, it does not appear that we can use the "Attachment" field type on the Record Producer, as for some reason, ServiceNow will not send these attachments over to Azure Dev Ops.  However, if we use the out-of-the-box "Add Attachments" link at the bottom of all forms on the Service Portal, ServiceNow will send over those attachments to Azure Dev Ops.  

 

We are fine with doing that, but how can we create a Catalog Client Script to verify that they have actually attached a file before allowing them to submit?

 

Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@jmiskey 

you can use this onSubmit catalog client script and it will work in both native + portal

Example: if variable ABC has value as XYZ then make the attachment mandatory

Please enhance it as per your requirement

function onSubmit() {
    //Type appropriate comment here, and begin script below
	var variableValue = g_form.getValue('ABC');
    try {
        if (window == null) {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length == 0 && variableValue == 'XYZ') {
                alert('You must add attachment before submitting this request.');
                return false;
            }
        }
    } catch (ex) {
        // native view
        var length = getSCAttachmentCount();
        if (length == 0 && variableValue == 'XYZ') {
            alert('You must add attachment before submitting this request.');
            return false;
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

15 REPLIES 15

Step 3 is not a customization of the out-of-box "things". Adding a JS Include is an out-of-box configuration that was built for that specific purpose (adding functionality or scripts) that isn't present.

That step is adding the ability for the portal to be able to see and use that UI script. It's the same as if a CSS Include was added to pull your company's font for branding.

It actually turns out that someone else here at the company actually added all that stuff five years ago, so it is actually already all there, and works for his Catalog Item.  However, if I test out the code, it does not work.  No matter what I do, I always get the message that I need to include an attachment, even if one is already attached.

 

There are 3 factors at play here, and I am wondering if this process does not work one of these reasons:

1. The Catalog Item is actually a Record Producer

2. The Record Producer is part of an Order Guide

3. The Record Producer is writing to the SAFe Epic table (sn_safe_epic)

 

Perhaps the process in that KB will not work in this scenario?

Ankur Bawiskar
Tera Patron
Tera Patron

@jmiskey 

you can use this onSubmit catalog client script and it will work in both native + portal

Example: if variable ABC has value as XYZ then make the attachment mandatory

Please enhance it as per your requirement

function onSubmit() {
    //Type appropriate comment here, and begin script below
	var variableValue = g_form.getValue('ABC');
    try {
        if (window == null) {
            // portal
            if (this.document.getElementsByClassName('get-attachment').length == 0 && variableValue == 'XYZ') {
                alert('You must add attachment before submitting this request.');
                return false;
            }
        }
    } catch (ex) {
        // native view
        var length = getSCAttachmentCount();
        if (length == 0 && variableValue == 'XYZ') {
            alert('You must add attachment before submitting this request.');
            return false;
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Thank you!

This worked perfectly!

JenniferRah
Mega Sage

I have one like this that works for us. It's a Catalog Client Script.

JenniferRah_0-1740493256467.png

Here is the code:

function onSubmit() {
    try { //Works in non-portal ui
        var attachments = document.getElementById('header_attachment_list_label');
        if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
            g_form.addErrorMessage('You must attach a document.');
            return false;
        } else {
            g_form.clearMessages();
        }
    } catch (e) { //For Service Portal
        var count = getRPAttachmentCount();
        if (count <= 0) {
            g_form.addErrorMessage('You must attach a document.');
            return false;
        } else {
            g_form.clearMessages();
        }
    }
}

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