Attachment in Catalog Item restriction

harshi_ramesh
Tera Expert

Hi all

Had a query on attachment restrictions. Have a catalog item developed and it uses the OOB attachment paper icon.

Need to keep this, but want to restrict the user from submitting more than 3 attachments. 

If user submits more than 3 attachments, need to pop up a error message and restrict submission. Tried 'onSubmit' catalog client script, but we cannot do any DOM manipulation. 'this.document.getElementsByClassName.length', is where the DOM manipulation caused. This is the script used:

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var getAttach = this.document.getElementsByClassName('get-attachment').length;
    //alert(getAttach+"length is");
    if (getAttach > 3) {
        g_form.addErrorMessage("Maximum number of attachment is 3");
        return false;
    }
}

 Any other solution which can be used for this use case. Please provide if any.

Thanks  

5 REPLIES 5

Sourabh Tarlekr
Kilo Sage

Hi @harshi_ramesh 

 

You can use Glide Ajax in Client Script and Script Include to get the attachment count from sys_attachment form.

Please use below code and let me know if this works for you.

 

Client Script

function onSubmit() {

    var cartId = g_form.getParameter('sysparm_item_guid'); // finds the card id
    var ga = new GlideAjax('AZFCMassHelper');
    ga.addParam('sysparm_name', 'getAttachmentCount');
    ga.addParam('sysparm_cartID', cartId);
    ga.getXMLWait();

    var count = ga.getAnswer();
    if (count > 3) {
        g_form.addErrorMessage("Maximum number of attachment is 3");
        return false;
    } else
        return true;
}

 

Script Include

var AZFCMassHelper = Class.create();
AZFCMassHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getAttachmentCount:function()

	{

		var cartid=this.getParameter('sysparm_cartID');
		var gr=new GlideRecord('sys_attachment');
		gr.addQuery('table_sys_id',cartid);
		gr.query();
		return gr.getRowCount();
	},

    type: 'AZFCMassHelper'
});

 

Regards,

Sourabh