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

Hi the attachment is only under 1 circumstance, so this option will not work for me, I did try it but it asked for it for every request, and I only want if they select a specific option

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

this worked

function onSubmit() {
var myFieldValue = g_form.getValue('polygon_details_attached');
//Check if meant to have an attachment
if (myFieldValue == "Yes") {
if(this.document.getElementsByClassName('get-attachment').length == 0) {
alert('You must add an attachment before submitting this request.');
return false;
}
}
}

with the isolate script not ticked thank you