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

Sai Anna
ServiceNow Employee
ServiceNow Employee

Can you tell us variable type of contract, is it Yes/No ? if it is try



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



replace 'yes' with 'Yes'




Thanks,


Sai


Thanks, it was a Yes/No field and your suggestion did help. It's prompting if the value is "Yes", but it's not letting us add the item to the cart even after an attachment is added.



It's no longer working on the Service Portal either.


venkatiyer1
Giga Guru

Hi Alexander,




I dont see an issue with the syntax or anything. But i would recommend you adding a paranthesis around the following condition




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;  


  }  


 


  The reason being you are checking whether it is hidden or display for you to know element is not present.


Thanks, we're getting somewhere. It's prompting if the value is "Yes", but it's not letting us add the item to the cart even after an attachment is added.