Restrict attachments to specific catalog items via BR

KB15
Giga Guru

I'm looking to create a BR to exclude file attachments with certain characters that get to sc_cart_item. It works but can I make this more specific so it only applies to specific catalog items?

1 ACCEPTED SOLUTION

Hi KB,

I have an idea for your problem statement. Please follow below steps and I think, this will work for selective catalog item.

- Create a script include with below details

Name: catalogAttachmentUtils

Client callable: True/checked

Script:

var catalogAttachmentUtils = Class.create();
catalogAttachmentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    restrictAttachment: function() {
        var cartID = this.getParameter("sysparm_restrictid");
		gs.log(cartID, "cartID");
        if (cartID)
            gs.getSession().putClientData('catalog.attachment.restriction.id', cartID);

    },
	checkRestriction: function(id){
		return (gs.getSession().getClientData('catalog.attachment.restriction.id').toString() == id.toString())?true:false;
	},
    type: 'catalogAttachmentUtils'
});

Client script

Name: Restrict Attachment

Type: Onload

Isolate script : false/ unchecked(this is very important, if not available on form, please add this field on form )

Script:

function onLoad() {
		var cart_id = gel('sysparm_attachment_cart_id').value;
var ga = new GlideAjax('catalogAttachmentUtils');
ga.addParam('sysparm_name', 'restrictAttachment');
ga.addParam('sysparm_restrictid', cart_id);
ga.getXML(callbackFunction);
function callbackFunction(response) {
// just a placeholder
}
   
}

 

Finally, the business rule(on sys_attachment table with condition, table name = sc_cart_item) condition

new catalogAttachmentUtils().checkRestriction(current.table_sys_id)

Above steps will make sure, the business rule will only apply if, the catalog item has above onload script enabled. 

 

Hopefully, this will work.

View solution in original post

17 REPLIES 17

Hey KB,

- create a system property named "control.catalog.list" and put all applicable items there

- create a script include named "attachmentRestrictionUtils"

- add Below method in the script include

validCatalog: function(table, id){
    var targetCatalog = gs.getProperty("control.catalog.list");
    var checkItem = new GlideRecord(table);
    checkItem.addQuery("cat_item", "IN", targetCatalog);
    checkItem.addQuery("sys_id", id);
    checkItem.query();
    return (checkItem.hasNext())?true:false;
}

- In Business rule condition add this line new attachmentRestrictionUtils().validCatalog(current.table_name.toString(), urrent.table_sys_id.toString());

 

Above will restrict your BR to items which are mentioned in system property control.catalog.list

How should I add the items in the property? By sys_id?

Yes,

It should have sys_ids

The script include doesn't seem to work. Are there substitutions  and where should the code be added from the pre-filled script section?

Hi,

Script Include

Name: attachmentRestrictionUtils

Code:

var attachmentRestrictionUtils = Class.create();
attachmentRestrictionUtils.prototype = {
    initialize: function() {},
    validCatalog: function(table, id) {
        var targetCatalog = gs.getProperty("control.catalog.list");
        var checkItem = new GlideRecord(table);
        checkItem.addQuery("cat_item", "IN", targetCatalog);
        checkItem.addQuery("sys_id", id);
        checkItem.query();
        return (checkItem.hasNext()) ? true : false;
    },
    type: 'attachmentRestrictionUtils'
};

Screenshot

 

find_real_file.png

Business Rule Condition

find_real_file.png