Make an attachment mandatory based on a variable selection on a catalog item

heidiyablonski
Tera Contributor

I'm trying to make an attachment required on a catalog item (we use the portal) which I've done before using Catalog Client script.  The one I did that works is very simple because the variable has specific options however, now I need to make attachments mandatory and can't figure out how I can do this.  For this catalog item  I have a checkbox variable and I want to code it so that if the specific checkbox is selected, an attachment will be mandatory but I don't know what the value of a checked checkbox is.

This is my code:

function onSubmit() {
if (g_form.getValue('tass_merged_update_LUR') == 'true') {
//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;
}
}

}

}

 

Currently, I have the variable value as true because I want it to apply when the checkbox on the variable identified is checked.  True does not work.  Is it possible to have a catalog client script apply when a checkbox variable is selected?  What does the value need to be for the checkbox variable? I have other variables I can use and I was thinking I could use a "is not empty" value but I don't know how to code that.  

 

Suggestions greatly appreciated!

11 REPLIES 11

p_espinar
Kilo Guru

Hello,

I use the following in a similar situation:

onSubmit();

function onSubmit()  {
	
	// Verify que hay fichero y solo uno
	var gr2 = new GlideRecord("sys_attachment");
	gr2.addQuery("table_sys_id", current.sys_id);
	var oC = gr2.addQuery("table_name", "sys_data_source");
	oC.addOrCondition("table_name", "sc_cart_item");
	gr2.query();
	if (!gr2.next()) {
		gs.addErrorMessage("Error: debe adjuntar un fichero.");
		current.setAbortAction(true);
		producer.redirect="com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=0ef7d527db1f9f407c0de0a1ca961967";
		return;
	}
	else {
		if(gr2.getRowCount() > 1){
			gs.addErrorMessage("Error: debe adjuntar un único fichero.");
			current.setAbortAction(true);
			producer.redirect="com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=0ef7d527db1f9f407c0de0a1ca961967";
			return;
		}
	}
}

 

Un saludo,
Pablo Espinar
Consultant at Econocom Spain

Please mark this response correct if I've answered your question. Thanks!

xiaix
Tera Guru

So, I found myself in a similar situation where I had this:
find_real_file.png

 

 

So default, the catalog item has this checked:
find_real_file.png

 

And that's fine, because I created the following Widget Client Script (which I then put as a MACRO variable on the catalog item):

$scope.f_attachmentLinks = function(option) {
	try {
		if (!option) {
			console.warn("f_hideAttachmentOption was called without passing an option.");
			return;
		}
		var ab = document.getElementsByTagName('sp-attachment-button')[0];
		if (ab) {
			if (option == 'remove')
				ab.parentNode.removeChild(ab);
			else if (option == 'hide')
				ab.parentNode.style.display = "none";
			else if (option == 'show')
				ab.parentNode.style.display = "";
		}
		var span = document.getElementsByTagName('span');
		for (var s = 0; s < span.length; s++) {
			if (span[s].textContent == "Add attachments") {
				if (option == 'remove')
					span[s].parentNode.parentNode.removeChild(span[s].parentNode);
				else if (option == 'hide')
					span[s].parentNode.style.display = "none";
				else if (option == 'show')
					span[s].parentNode.style.display = "";
			}
		}
	}catch(err){console.warn("f_hideAttachmentOption ERROR: "+err.message);}
};

$rootScope.$on("field.change", function(evt, parms) {
	var i = 0;
	if (parms.field.type == "boolean") {
		if (parms.field.name == 'no_documentation_to_submit') {
			if (parms.field.value == 'true') {
				$scope.f_attachmentLinks('hide');
				angular.element("#sc_cat_item").scope().data.sc_cat_item.mandatory_attachment = false;
			}
			else {
				$scope.f_attachmentLinks('show');
				angular.element("#sc_cat_item").scope().data.sc_cat_item.mandatory_attachment = true;
			}
		}
	}
});

 

Bam, this makes a dynamic way to make attachments mandatory and even shows/hides the attachment buttons/icons.

 

The real power is being able to dynamically set this:

angular.element("#sc_cat_item").scope().data.sc_cat_item.mandatory_attachment

to either true or false, depending on your needs.