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

shloke04
Kilo Patron

Hi,



Can you try something like this as mentioned below:



Script:



function onSubmit() {


  //Type appropriate comment here, and begin script below



  var value= g_form.getValue('vnet');       //Replace your Variable Name here in place of vnet


  if(value=='true')


  {



  var cat_id = gel('sysparm_item_guid').value;


  var rowcount=0;


  //alert(request_type);


  //alert(category);


  var gr = new GlideRecord("sys_attachment");


  gr.addQuery("table_name", "sc_cart_item");


  gr.addQuery("table_sys_id", cat_id);


  gr.query();


  while(gr.next())


  {


  rowcount++;


  }



  if(rowcount<1){


  alert("Attachment(s) is Missing!!");


  return false;


  }




  }


}



Hope this helps.Mark the answer as correct/helpful based on impact.




Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Thank you, this works well in the normal platform but it doesn't seem to work in the Service Portal. Are you using this script in the new Service Portal?


Sai Anna
ServiceNow Employee
ServiceNow Employee

for service portal use callback function in gr.query()



Example:



var que = new GlideRecord('incident');


que.addQuery('number','IN','INC00001,INC00002');


que.query(queResponse);



function queResponse(que) {


while(que.next()) {


//do something....


}


}





Thanks,


Sai


Thanks for your help, once I saw this thread I got it going.



Require attachment for catalog item in Service Portal


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