Attachment mandatory not working in Serviceportal

salu
Mega Guru

Hello All,

I am trying to make attachment mandatory in catalog item and service portal. Below script working in catalog item but not working in serviceportal.

 

We are using Add to cart button in service portal.

 

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;
}
}
}

Can some please help??

 

Thanks

Saranya

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@salu

Refer blog below

Verify Mandatory Attachments Count on Catalog Item

Sharing the steps and script here

Script: onSubmit

UI Type - ALL

Applies to Catalog Item - True

Note: Ensure Isolate Script field is set to False for this Catalog Client Script to ensure DOM manipulation works

function onSubmit() {
	//Type appropriate comment here, and begin script below

var countRequired = 1;
	if(window == null){
		// portal
		if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
			alert('You must attach the completed form before submitting this request.');
			return false;
		}
	}
	else{
		// native view
         var length = $j("li.attachment_list_items").find("span").length;
		if(length != countRequired){
			alert('You must attach the completed form before submitting this request.');
			return false;
		}
	}
}

Isolate Script: Set to False

image

 

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

21 REPLIES 21

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 Script:

 

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;

 

  }

 

  }

 

  }

 

}

here if you see there is method getSCAttachmentCount()  which has defined in UI Script, so make sure you have created ui script and then called in your catalog client script. 

UI script is there

 

find_real_file.png

is it still giving error in console ? 

Yes it was giving the same error

 

I have change the onsubmit script to this and it got worked..

 

 

Thank you very much for the help!!!

 

 

function onSubmit() {
	//Type appropriate comment here, and begin script below

var countRequired = 1;
	if(window == null){
		// portal
		if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
			alert('You must attach the completed form before submitting this request.');
			return false;
		}
	}
	else{
		// native view
         var length = $j("li.attachment_list_items").find("span").length;
		if(length != countRequired){
			alert('You must attach the completed form before submitting this request.');
			return false;
		}
	}
}