0

I want to be able to show and hide a button base on the user Role. I think using code can fix this problem. But I'm new to Salesforce. Im thinking of create a div and hide/show the button using "slds-hide" and using aura:if to check the role. What should I do, if you can have an example code of how to check the role using aura:if it would be great.

Below is my code right now:

<lightning:button class="slds-button_brand slds-align_absolute-center slds-size_2-of-2 " label="New Development Project" onclick="{!c.openmodal}"   /> 

Thank you,

1
  • 2
    Welcome to SFSE! Please take a quick look at How to Ask and explore our tour, then edit your post to add some more context and show us what you've done so far. It's not clear where you're working or where you're stuck.
    – David Reed
    Oct 4 '19 at 3:54
0

you need to get the logged in User role Name from Apex Controller, then bind it to Aura attribute then based on value you can hide and show like below example code snippet.

cmp

<aura:component controller="Apexcontroller" implements="flexipage:availableForAllPageTypes" access="global">
    <aura:attribute name="rolename" type="String"/>
    <aura:handler name="init" value="{!this}" action ="{!c.doInit}"></aura:handler>
    <lightning:button class="{!v.rolename == 'abc'? 'slds-button_brand slds-align_absolute-center slds-size_2-of-2':'slds-hide'} " label="New Development Project" onclick="{!c.openmodal}"   />
</aura:component>

Controller

doInit :  function(c,e,h){
var action = c.get("c.getUserroleName");
action.setCallback(this,function(response){
            var state = response.getState();
            if(state === "SUCCESS"){
                c.set("v.rolename",response.getReturnValue());
            }

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

ApexController:

public without sharing class ApexController{
 @AuraEnabled
    public static  string getUserroleName(){


 User loggenuser = [select id, name, userrole.name from user Where Id=: UserInfo.getUserId()];
    return loggenuser.userrole.name;
    }
}
3
  • Thank you so much for your answer, but they give me an error when i tried to save the Component class. Error "Cannot mix expression and literal string in attribute value, try rewriting like {!'foo' + v.bar}: Source"
    – An Le
    Oct 4 '19 at 18:09
  • please update your Question with what you have tried, other wise it's very hard to give solution based on error message
    – sdandamud1
    Oct 4 '19 at 18:15
  • Hi, So I tried to change the button like you suggested and on this line "<lightning:button class="{!v.rolename == 'abc'? 'slds-button_brand slds-align_absolute-center slds-size_2-of-2':'slds-hide'} " label="New Development Project" onclick="{!c.openmodal}" />" the error appear
    – An Le
    Oct 4 '19 at 18:17
0

Update Thank you sdandamud1 for your answer, so I fixed the error "Cannot mix expression and literal string in attribute value, try rewriting like {!'foo' + v.bar}: Source " by putting a bracket in the if part in Cmp class. Code:

<lightning:button class="{(!v.rolename == 'Development Manager') ? 'slds-button_brand slds-align_absolute-center slds-size_2-of-2':'slds-hide'} " label="New Development Project" onclick="{!c.openmodal}"   />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.