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

I had tried the one in the link but it just gives me a Java error, will try a few more variations in the new year 😞 

 

function onSubmit() {
//Works in non-portal ui
try {
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' ) {
alert('You must attach the completed form before submitting this request.');
return false;
}
}
//For Service Portal
catch(e) {
var count = getSCAttachmentCount();
if(count <= 0) {
alert('You must attach the completed form before submitting this request.');
return false;
}
}
}

 

 

Did you add the UI Script and JS theme as detailed in the link?  The catalog client script(s) need both of these to work. 

Hi so i have added point 1 and 3 but point 2 I don't understand? I have searched for widgets but they all seem to have scripts inside. Where do I find no 2? Should say we have only been live on SN for a few months and I am new to admin so still learning my way around it all 🙂 

Mandeep Karan
Tera Guru

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 in the SC catalog item widget used for your portal.

Use this URL for reference: https://docs.servicenow.com/bundle/newyork-servicenow-platform/page/build/service-portal/task/widget...

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.

Sajilal
Mega Sage

Hello,

After reading through the reviews i understand this is required for Service Portal, you can find a OOB widget already available in the Servicenow Store for your requirement which checks for Attachment validations.

You can use this widget and it to as a Macro variable in your Catalog Item. so that it will only work for your specified catalog item. 

What this widget does is, without adding the attachment, the Order/Submit button is greyed out. The Macro label can be renamed as per your own customization to inform the users to add attachment. As soon as the attachment is added, the order/submit button becomes available for submit.

This works in Service Portal without any issues.

Mark Correct if this solves your issue and also mark 👍 Helpful if it helps resolve your problem

Thanks,
Saji