Showing or Hiding a Field if the Current User is a Member of a Role in a Creatio Freedom UI Page

Creatio has been expanding the capabilities of business rules on Freedom UI pages with each release. However, as of 8.0.10, you cannot add a rule to show or hide a field on a Freedom UI page based on whether the current user is a member of a specific role or not. However, it is possible to do this with code on the page.

First of all, make sure you add “@creatio-devkit/common” to the page as sdk.

Next, add an attribute to the viewModelConfig. I’ll call the attribute “IsUserInRole since we’ll set it to true/false if the user is in the role.

viewModelConfig: /**SCHEMA_VIEW_MODEL_CONFIG*/{
    "attributes": {
        "IsUserInRole": {}
    }
}/**SCHEMA_VIEW_MODEL_CONFIG*/

Now, bind the attribute to the visible property of the control by adding the following to the control in the viewConfigDiff:

"visible": "$IsUserInRole"

When the view model is initialized, basically the Freedom UI equivalent of the onEntityInitialized for classic pages, do a query using the model to determine if the current user is in the role. We’ll use that result to set the attribute. The code would look as follows:

{
    request: "crt.HandleViewModelInitRequest",
    handler: async (request, next) => {
        await next?.handle(request);

        // get current user
        const sysValuesService = new sdk.SysValuesService();        
        const sysValues = await sysValuesService.loadSysValues();
        const currentUserContact = sysValues.userContact;
 
        // create model query and add filters
        userRoleModel = await sdk.Model.create("SysUserInRole");
        const filter = new sdk.FilterGroup();
        await filter.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "SysRole.Name", "The Role Name Here");
        await filter.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "SysUser.Contact", currentUserContact.value);
 
        // workaround for filters, will be fixed in 8.1
        const newFilter = Object.assign({}, filter);
        newFilter.items = filter.items;
 
        const results = await userRoleModel.load({
            attributes: ["Id"],
            parameters: [{
                type: sdk.ModelParameterType.Filter,
                value: newFilter
            }]
        });
 
        // now set the attribute
        request.$context.IsUserInRole = (results.length > 0);
    }
}

Note, don’t forget to change “The Role Name” in the code above to the actual role name. The end result will be that the field will only show if the current user is in that role.

ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) and Creatio (bpm'online) news and product updates!

You have Successfully Subscribed!