Filter Lookup by Role

Hi,

is it possible to filter a Lookup according the user role? I see in the Academy that is possible for Contact Type but nothing found for roles.

 

We want to show different values for each user or orole.

 

Thanks!

Like 0

Like

1 comments

Dear Alfredo, 

If you want records from the lookup be accessible only for certain user roles throughout the system you can turn in Managed by records for the lookup and set up access rights for the lookup records. 

If you want to restrict shown values only on a certain page/pages in the dropdown list, you can do the following: 

First, in the OnEntityInitialized determine if user belongs to a certain role and set the bool attribute. To determine if the user has a certain role you can see the article below:

https://customerfx.com/article/determining-if-a-user-has-a-specific-role-in-bpmonline/

Here is an example: 

         attributes: {

            "IsSysAdmin": {

                "dataValueType": this.Terrasoft.DataValueType.BOOLEAN,

                "type": this.Terrasoft.ViewModelColumnType.VIRTUAL_COLUMN,

            }

        },

methods: {

            

            onEntityInitialized: function(){

                this.callParent(arguments);  

                var roleName = "System administrators";

                

                var esq = Ext.create("Terrasoft.EntitySchemaQuery", {

                    rootSchemaName: "SysUserInRole"

                });

                esq.addColumn("SysRole");

                 

                esq.filters.add("UserFilter", Terrasoft.createColumnFilterWithParameter(

                    Terrasoft.ComparisonType.EQUAL, "SysUser", Terrasoft.SysValue.CURRENT_USER.value

                ));

                esq.filters.add("RoleFilter", Terrasoft.createColumnFilterWithParameter(

                    Terrasoft.ComparisonType.EQUAL, "SysRole.Name", roleName

                ));

                esq.getEntityCollection(function(result) {

                    if (!result.success || result.collection.getItems().length === 0) {

                        this.set("IsSysAdmin", false); 

                    }

                    else {

                        this.set("IsSysAdmin", true); 

                    }

                }, this);

            },

In the diff section you would need to add property controlConfig for the field and "list" and "prepareList" inside it: 

            {

                "operation": "merge",

                "name": "AccountType",

                "values": {

                    "layout": {

                        "colSpan": 24,

                        "rowSpan": 1,

                        "column": 0,

                        "row": 9

                    }, 

                    "controlConfig": {

                        "list": {

                            "bindTo": "UsrAccountTypeCollection"

                        },

                        "prepareList": {

                            "bindTo": "getAccountTypeValues"

                        }

                    }

                }

            },



The list is bond to the attribute of the Collection type: 

        attributes: {

            "UsrAccountTypeCollection": {

                "dataValueType": Terrasoft.DataValueType.COLLECTION,

                "value": Ext.create("Terrasoft.Collection")

            }

        }

prepareList is bond to the method which returns the AccountType list using ESQ, where you can add the condition on if user is in a certail role: 

            getAccountTypeValues: function(searchValue, list) {

                var esqAccountType = Ext.create("Terrasoft.EntitySchemaQuery", {

                    rootSchemaName:

                    "AccountType"

                });

                esqAccountType.addMacrosColumn(Terrasoft.QueryMacrosType.PRIMARY_COLUMN, "value");

                esqAccountType.addMacrosColumn(Terrasoft.QueryMacrosType.PRIMARY_DISPLAY_COLUMN, "displayValue");

                

                var positionColumn = esqAccountType.addColumn("UsrTestSort");

                positionColumn.orderDirection = Terrasoft.OrderDirection.ASC;

                esqAccountType.getEntityCollection(function(result) {

                    if (result.success) {

                        list.clear();

                        var preObject = {};

                        result.collection.each(function(item) {

                            if (item.values.displayValue.startsWith("C")){

                                if (this.get("IsSysAdmin") === true){

                                    preObject[item.get("value")] = item.values;

                                }

                            }

                            else 

                            {

                                preObject[item.get("value")] = item.values;

                            }

                        }, this);

                        list.loadAll(preObject);

                    }

                }, this);

            }



As a result, in my case, all users without SysAdmin role don't see Account types on the Account page that start with "C".

Best regards, 

Dennis 

Show all comments