The CreatorCon Call for Content is officially open! Get started here.

How can i limit the number of attachments in a catalog item?

Alberto27
Tera Contributor

Hello,

We have a catalog item in which we are using the OOTB attachment and we need lo limit that the user can only attach one field but the client don“t allow us to use DOM methods.

So we need to create a Catalog Client Script or something like this to limit the number of attachments.

Anyone have do this?

Thanks to all.

14 REPLIES 14

Hi Filipe, 

This could be a solution but we need to use the OOTB attachment

Regards

Hi Alberto,

Then I have a solution for you.

Create an after insert business rule in the sys_attachment table with the following code:

(function executeRule(current, previous /*null when async*/) {

	var gr = new GlideRecord("sys_attachment");
	gr.addQuery("table_name", "sc_cart_item");
	gr.addQuery("table_sys_id", current.table_sys_id);
	gr.addQuery("sys_id", "!=", current.sys_id);
	gr.query();
	if(gr.next()){
		//abort action
		current.deleteRecord();
		gs.addErrorMessage("you can only insert one attachment!");
	}

})(current, previous);

find_real_file.png

 

Then, when you try to add more than 1 attachment you will see an error message:

find_real_file.png

 

Small explanation: I tried to do this with a before insert business rule. I was validating the number of attachments and doing a current.setAbortAction(true). This works, but adds an additional error message on the screen due to the fact that the portal is expecting to receive a JSON with information of the attachment.
The only way to prevent that error message was to do an after insert BR that will delete the attachment after it is inserted if it is the second attachment being added.

Please, if this answer solves your issue, please mark it as correct and helpful.

Thanks,

Filipe

Hi Filipe

Its looks fine, i have tried it and limit the number of attachments, but this business rule limit the attachment of all the existing catalog items.

How can i set that this Business Rule apply only to one especific item?

Because in the table sys_attachment i dont kwon how can i chenk of which catalog item provide the attachments

Best regards

Hi Alberto,

In this case I cannot give you a fast solution for this.
I would say that if you want to apply it to only one specific item, the best option is, as I mentioned in my initial post, to restrict attachments on that item and create a variable of type item. That variable will only receive one attachment, so it will be exactly what you need.

You need to consider that what you want is a customized behavior, so from my point of view is acceptable to show that difference to the end-user, otherwise they will not understand the difference!

Please, if this answer is relevant for you, please mark it as correct and helpful.

Thanks,

Filipe

Hi Alberto!!

I was doing an additional search on this topic!!

Just for you to know: when you open a catalog item in the portal and you add an attachment, the attachment is inserted in the sys_attachment table with the table_name = sc_cart_item. 
But, if I go to the sc_cart_item table, nothing is there! Which is strange!

We can do an onSubmit catalog client script to perform an AJAX call to check the number of attachments that the user added, but for that I need an id for the sc_cart_item (to use while querying the sys_attachment table).

I saw this thread where a user customized the client controller of widget "SC Catalog Item" in order to generate the id of the cart and get it inside the catalog item client scripts!

Basically you need to clone the "SC Catalog Item" widget and in the new version add the last line:

g_form.generatedItemGUID = $scope.data._generatedItemGUID;

Here is an example: (in the OOB widget you add the previous statement in line 528)

var g_form;
	$scope.$on('spModel.gForm.initialized', function(e, gFormInstance){
		if (gFormInstance.getSysId() != -1 && gFormInstance.getSysId() != c.getItemId())
			return;
		g_form = gFormInstance;
		
		$timeout(function() {
				$rootScope.$emit('spModel.gForm.rendered', g_form);
		}, 175);
		
		// This runs after all onSubmit scripts have executed
		g_form.$private.events.on('submitted', function(){
			if ($scope.data.sc_cat_item.item_action === "order")
				getOne();
			else if ($scope.data.sc_cat_item.item_action === "add_to_cart")
				addToCart();
			else if ($scope.data.sc_cat_item.item_action == "update_cart")
				updateCart();
		});
		
		//2019-03-04 James McWhinney
		g_form.generatedItemGUID = $scope.data._generatedItemGUID;
		
		
	});

Then, you can use  g_form.generatedItemGUID in your catalog client script.
This gives the possibility to capture the sc_cart_item records inside the catalog item.


Next step is to configure the service portal page "sc_cat_item" in order to replace the OOB widget by the one you created previously.

After that, if you create an onSubmit Catalog Client script with the following code:

function onSubmit() {
   //Type appropriate comment here, and begin script below
   alert(g_form.generatedItemGUID);
}

You will be able to pick the sc_cart_item!!!

Last step is to put in this onSubmit Catalog client script some logic to perform AJAX call that will retrieve the number of attachments related with a specific sc_cart_item record.

If that retrieves more than 1 attachment:
- cancel the submit and throw an error message. The user needs to delete attachments
- delete automatically all attachments except the first one that was added.

Man, I really hope this helps you achieve what you want without the usage of DOM manipulation!

Again, I kindly ask that, if this answer is relevant for you, please mark it as correct and helpful.

Thanks,

Filipe