Require attachment if a field is "Yes" and attachment is missing.

galavodasal
Giga Expert

Hi all,

We have an existing script to require an attachment upon submission which works,   but we're trying to tweak the same script for a new catalog item.

The new catalog item will require an attachment upon submission if a certain field's value is "yes". Here's the script:

function onSubmit() {

  try {

  var attachments = document.getElementById('header_attachment_list_label');

var contract = g_form.getValue('signed_pricing_contract');  

  if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none' && contract == 'yes') {

  alert("Please be sure to attach all required documentation prior to submitting your request");

  return false;

  }

} catch(e) {

  if ((this.document.getElementsByClassName('get-attachment').length == 0) && contract == 'yes') {

  alert('Please add the required attachment before submitting this request.');

  return false;

}

}

}

The && contract == 'yes' part isn't working as expected. Can someone point out what is incorrect?

1 ACCEPTED SOLUTION

amlanpal
Kilo Sage

Hi Alexander,



Hope you are following this thread: Require attachment for catalog item in Service Portal. If so, please create a new UI Script as below and modify your existing onSumbit Catalog Client Script.



UI Script:


API Name:GlobalCatalogItemFunctions


Script:


function getSCAttachmentCount() {


  var length;


  try {


  length = angular.element("#sc_cat_item").scope().attachments.length;


  } catch(e) {


  length = -1;


  }


  return length;


}



onSubmit Catalog Client Scrip:


function onSubmit() {


  var contract = g_form.getValue('signed_pricing_contract');


  if(contract == '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('Please attach atleast one attachment to proceed');


  return false;


  }


  }


  catch(e) { //For Service Portal


  var count = getSCAttachmentCount();


  if(count <= 0) {


  alert('Please attach atleast one attachment to proceed');


  return false;


  }


  }


  }


}



Although I would like to encourage you to follow this thread along with: Script Include & Catalog Client script Working properly in GLOBAL application but doesn't work in SC...



I hope this helps. Please mark correct/helpful based on impact


View solution in original post

12 REPLIES 12

Perfect, got it. Thanks for the info.


Priyanshu Praj1
Tera Contributor

Hi, I tried above code but I'm getting an error " Error while running Client Script "check attachment": ReferenceError: getSCAttachmentCount is not defined"

Bgsf2233
Kilo Contributor

Make the global checkbox 'True' in the getSCAttachmentCount script include.

It worked for me!