+ Start a Discussion
Prasan De AlwisPrasan De Alwis 

How to Show/Hide button based on Profile in Salesforce LWC?

Hi,

I have develop LWC component which is normally used by normal users. In that lightning web component button is there as below.
<lightning-button slot="actions" label="Deactivate" onclick={DeactivateProcess}> </lightning-button>
The above button should be only be show to System Admin Profile and Business Admin Profile. How to show the button only to those two Profiles without using record types or different page layouts?
Best Answer chosen by Prasan De Alwis
CharuDuttCharuDutt
Hii Prasan De Alwis 
Try The Below Code 
LWC
<template if:true={ShowBtn}>
<lightning-button label="ProfileButton" ></lightning-button>
</template>


JS
import { LightningElement,track,wire } from 'lwc';
import getProfile from '@salesforce/apex/ProfileBased.getProfile';
export default class Tester2 extends LightningElement {
  @track data;
  ShowBtn = false;
  error;
  wiredActivities;
  @wire(getProfile)
  wiredActivities({ error, data }) {
    if (data){
      console.log('Data==> '+JSON.stringify(data));
      
      this.data = data;
      if(this.data == 'System Administrator' || this.data == 'Business Administrator'){
        this.ShowBtn = true;
      }else{
      this.ShowBtn = false;
}
      console.log(this.data);
      this.error = undefined;
     } else if (error) {
      this.error = error;
      this.data = undefined;
     
  }
}
}

APEX
public class ProfileBased {
      @AuraEnabled(cacheable = true)
    public static string getProfile(){
        Id profileId = UserInfo.getProfileId();
    String profileName =[Select Id, Name from Profile where Id=:profileId].Name;
    system.debug('Profile Name'+profileName);
        return profileName;
    }
}

Please Mark It As Best Answer If It Helps
Thank You! 

 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Prasan,

>> https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.get_current_user
>> https://rajvakati.com/2019/02/11/get-current-user-details-in-lightning-web-components/

The above link shows how to access current user details.

You can use the above in conjunction with this link[ https://www.infallibletechie.com/2019/06/simple-hide-and-show-in-lightning-web.html ] which has an example of show or hide text on button click you need to modify it to fit your scenario.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
CharuDuttCharuDutt
Hii Prasan De Alwis 
Try The Below Code 
LWC
<template if:true={ShowBtn}>
<lightning-button label="ProfileButton" ></lightning-button>
</template>


JS
import { LightningElement,track,wire } from 'lwc';
import getProfile from '@salesforce/apex/ProfileBased.getProfile';
export default class Tester2 extends LightningElement {
  @track data;
  ShowBtn = false;
  error;
  wiredActivities;
  @wire(getProfile)
  wiredActivities({ error, data }) {
    if (data){
      console.log('Data==> '+JSON.stringify(data));
      
      this.data = data;
      if(this.data == 'System Administrator' || this.data == 'Business Administrator'){
        this.ShowBtn = true;
      }else{
      this.ShowBtn = false;
}
      console.log(this.data);
      this.error = undefined;
     } else if (error) {
      this.error = error;
      this.data = undefined;
     
  }
}
}

APEX
public class ProfileBased {
      @AuraEnabled(cacheable = true)
    public static string getProfile(){
        Id profileId = UserInfo.getProfileId();
    String profileName =[Select Id, Name from Profile where Id=:profileId].Name;
    system.debug('Profile Name'+profileName);
        return profileName;
    }
}

Please Mark It As Best Answer If It Helps
Thank You! 

 
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47

Hi  Prasan De Alwis,

You can do this through your component:

<template if:true={btnVisibility }>

<lightning-button label="ProfileButton" ></lightning-button> </template>

 

@track data;

   btnVisibility = false;

  this.data = data;
   if(this.data == 'System Administrator' || this.data == 'Business Administrator'){
      this.btnVisibility = true;
    }
    else{
    this.btnVisibility = false;
     }

 

Please Mark it as Best Answer,if it helps!

Thanks