How to check is an attachment has been added to a request

Nikkikobako
Giga Contributor

Hi I have used a number of suggestions I found online to check if an attachment is attached but most just error. The closest i get is below I check first if a selection is yes then if it is I run the code below: 

This gives me alert 2 msg then alert 3 msg with a value of 0 if attached or not, 

Any help would be very much appreciated, thank you 🙂 

function onSubmit() {
var myFieldValue = g_form.getValue('polygon_details_attached');

//Check if meant to have an attachment
if (myFieldValue == "Yes") {
  var cat_id = 'sysparm_item_guid'.value;
  var gr = new GlideRecord("sys_attachment");
  alert ("its yes 2");
  gr.addQuery("table_name", "sc_cart_item");
  gr.addQuery("table_sys_id", cat_id);
  gr.query();
  alert("its yes3 = " + gr.getRowCount());

  if (!gr.next()) {
     alert("You must attach a file to submit.");
    return false;
  }
}
}

1 ACCEPTED SOLUTION

AbhishekGardade
Giga Sage

This may help:

Possible Ways for Making an Attachment Mandatory : Service Portal/Native UI

Please mark as Correct Answer and Helpful.
Thank You!
Abhishek Gardade

Thank you,
Abhishek Gardade

View solution in original post

17 REPLIES 17

You can use this method for service portal.

1. create a ui script of mobile/service portal type add below code:

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

function getSCAttachment(){
var attach;
try {
attach = angular.element("#sc_cat_item").scope().attachments;
attach = JSON.stringify(attach);
} catch(e) {
attach = 'Empty';
}
return attach;
}

2. Create a widget dependency and it the SC catalog item widget used for your portal.

find_real_file.png

3. create a on submit client script on your item with below code:

function onSubmit() {

var myFieldValue = g_form.getValue('polygon_details_attached');

if (myFieldValue == "Yes") {
try { //Works in non-portal ui


var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
//alert('This item requires an attachement.');
alert("You must attach a file to submit.");
return false;
}
} catch(e) { //For Service Portal
var count = getSCAttachmentCount();
if(count <= 0) {
//alert('This item requires an attachement.');
alert("You must attach a file to submit.");
return false;
}
}

}
}

Note: you can modify on submit script accordingly.

DOM will not work on portal. 

This is not only DOM. In service portal it will call the UI script.

I am using this method in madrid service portal.

Have you tried this approach?

Ashley Snyder1
Giga Guru

As of the Madrid release you should be able to check if an attachment (not a specific attachment) is added within the Service Portal out of the box:

find_real_file.png

Now if you need to check attachments based upon variable values you can script that in, I've used the script from ServiceNow Elite in different instances without an issue and used an if statement to check my variable value, it also works for the platform as well:

https://www.servicenowelite.com/blog/2017/5/12/service-portal-require-attachments

Here's one I've used in the past as a catalog client script, just make sure you've added the UI Script and JS Include:

 var type = g_form.getValue('uvar_typeOfRequest');
    try { //Works in non-portal ui

        if (type =='infochange') {
            var attachments = document.getElementById('header_attachment_list_label');
            if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {

                g_form.addErrorMessage("Please attach the Informational Change Template to this request");

                return false;
            }
        }

    } catch(e) { //For Service Portal

        var count = getSCAttachmentCount();

        if(count <= 0 && type == 'infochange') {

            g_form.addErrorMessage("Please attach the Informational Change Template to this request");

            return false;

        }

    }

If my response was helpful, please mark as Helpful and consider setting the reply as the Correct Answer if it best answered your question, thank you!