Migrate $User, $Profile, $Label and $Api to Lightning Components

Migrate $User, $Profile, $Label and $Api to Lightning Components
Reading Time: < 1 minute

We can’t use $User, $Profile, $Label, and $Api in Lightning. We need to implement a server-side solution.

Migrating your code to Lightning Components can be a daunting task. Fortunately, with a few simple steps, you can migrate your $User, $Profile, $Label and $Api components to Lightning Components.

The original JS button

alert($User.Email)

Create a Lightning component

Create a Lightning component: JSMUserInfo.cmp

<aura:component implements="force:lightningQuickAction" controller="JSMUserInfoService" >
  <aura:attribute name="user" type="JSMUserInfo" />
  <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
 
  Id: {!v.user.Id}
  <br/>
  First name: {!v.user.FirstName}
  <br/>
  Last name: {!v.user.LastName}
  <br/>
  Email: {!v.user.Email}
 
</aura:component>

Create a js controller

JSMUserInfo.js


({
  doInit : function(component, event, helper) {
    var action = component.get("c.getUserInfo");
    action.setCallback(this, function(response) {
      var state = response.getState();
      if(state == "SUCCESS" && component.isValid()){
        console.log("success") ;
        var result = response.getReturnValue();
        console.log(result);
        console.log(result.FirstName);
        component.set("v.user", result);
 
   }else{
     console.error("fail:" + response.getError()[0].message); 
    }
   });
  $A.enqueueAction(action);
}
})

Create an Apex class

A wrapper class containing the user info we need


public class JSMUserInfo{
  @AuraEnabled
  public String Id {get;set;}
  @AuraEnabled
  public String FirstName {get;set;}
  @AuraEnabled
  public String LastName {get;set;}
  @AuraEnabled
  public String Email {get;set;}
}

Create a service Apex class

The server-side controller that exposes the User’s info


public class JSMUserInfoService {
 
@AuraEnabled
public static JSMUserInfo getUserInfo(){
  try{
    JSMUserInfo info = new JSMUserInfo();
    info.Id = Userinfo.getUserId();
    info.FirstName = Userinfo.getFirstName();
    info.LastName = Userinfo.getLastName();
    info.Email = Userinfo.getUserEmail();
    return info;
  }catch(Exception e){
     throw new AuraHandledException(e.getMessage()); 
  }
 
  } 
}

We can use a similar approach for $Api or any other global variable

$Profile


@AuraEnabled
public static Profile getProfileInfo(){
  try{
    String profileId = UserInfo.getProfileId();
    Profile profile = [SELECT Id, Name FROM Profile WHERE Id =:profileId];
    return profile;
  }catch(Exception e){
    throw new AuraHandledException(e.getMessage()); 
  }
}

$Site


@AuraEnabled
public static JSMSiteInfo getSiteInfo(){
  try{
    JSMSiteInfo info = new JSMSiteInfo();
    info.Prefix = Site.getPathPrefix();
    info.Domain = Site.getDomain();
    info.Name = Site.getName();
    return info;
  }catch(Exception e){
    throw new AuraHandledException(e.getMessage()); 
  }
}

,

About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.


Join 22 other subscribers

Leave a Reply

Discover more from javaniceday.com

Subscribe now to keep reading and get access to the full archive.

Continue reading