Handle custom permissions in a Salesforce Aura component

Access Custom Permissions in Aura Components

Access Custom Permissions in Aura Components

This guide explains how to access custom permissions in a Salesforce Aura component using Apex.

Step 1: Create a Custom Permission

Navigate to SetupCustom Permissions and create a custom permission (e.g., Manage_Custom_Action). Assign it to the required profiles or permission sets.

Step 2: Apex Controller

Use the following Apex code to check if the user has the custom permission:


public with sharing class CustomPermissionController {
    @AuraEnabled
    public static Boolean hasCustomPermission(String customPermissionName) {
        return FeatureManagement.checkPermission(customPermissionName);
    }
}
    

Step 3: Aura Component

Below is the code for the Aura component:

Component Markup (customPermissionComponent.cmp)


<aura:component controller="CustomPermissionController"
                 implements="flexipage:availableForAllPageTypes,force:hasRecordId">
    <aura:attribute name="hasPermission" type="Boolean" default="false" />

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <div>
        <lightning:button label="Perform Action"
                              onclick="{!c.handleAction}"
                              disabled="{!not(v.hasPermission)}" />
    </div>
</aura:component>
    

JavaScript Controller (customPermissionComponentController.js)


({
    doInit: function (component, event, helper) {
        helper.checkCustomPermission(component);
    },

    handleAction: function (component, event, helper) {
        alert('Action performed successfully!');
    }
});
    

JavaScript Helper (customPermissionComponentHelper.js)


({
    checkCustomPermission: function (component) {
        const action = component.get("c.hasCustomPermission");
        action.setParams({ customPermissionName: "Manage_Custom_Action" });

        action.setCallback(this, function (response) {
            const state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.hasPermission", response.getReturnValue());
            } else if (state === "ERROR") {
                const errors = response.getError();
                console.error(errors);
            }
        });

        $A.enqueueAction(action);
    }
});
    

How It Works

1. The Apex controller checks if the user has the specified custom permission.
2. The Aura component uses the result to enable or disable actions dynamically.
3. Replace Manage_Custom_Action with your custom permission API name.

Handle custom permissions in a Salesforce Aura component Handle custom permissions in a Salesforce Aura component Reviewed by dasfrogpractice on 07:00 Rating: 5

No comments:

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe