Get Selected Record Type Id in Standard Button Overrides Lightning Component

Implementing lightning:hasPageReference interface, we can access the recordTypeId in lightning JS controller. lightning:hasPageReference provides access to the pageReference attribute.

The pageReference attribute can be populated only for the following page types:

  • standard__component
  • standard__navItemPage (for Lightning component tab only)
  • standard__recordPage
  • standard__objectPage

Example:

Lightning Component:

<aura:component implements="force:hasRecordId,lightning:actionOverride,lightning:hasPageReference">
    <!--Declare Attributes-->
    <aura:attribute name="selectedRecordId" type="Id" />
    
    <!--Declare Handler-->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>  
    
    <!--Component Start-->
    <div class="slds-m-around--xx-large">
        Selected Record Type Id : {!v.selectedRecordId}
    </div>
    <!--Component End-->
</aura:component>

Lightning JS Controller:

({
    doInit: function(component, event, helper) {
        
        //get record type Id
        var recordTypeId = component.get("v.pageReference").state.recordTypeId;
        component.set("v.selectedRecordId", recordTypeId);
        
        //get action name edit/new
        var actionName = component.get("v.pageReference").attributes.actionName;
        console.log('actionName-' + actionName);
        
        //get object API name
        var objectApiName = component.get("v.pageReference").attributes.objectApiName;
        console.log('objectApiName-' + objectApiName);
    },
})

Output:

Get Available Lightning Components In Your Org

  1. To get all available lightning components in your org, My Domain should be enabled in your Org.
  2. Log in to your org.
  3. Add componentReference/suite.app in your org domain URL.
  4. It should look like this :  https://yourorgdomain.lightning.force.com/lightning.force.com/componentReference/suite.app
  5. It will open Salesforce Lightning Component Library.
  6. Here you can see all the available components of your Org along with Salesforce Lightning Component examples.

Enforce Field-Level Security Permissions for SOQL Queries

  • In Spring ’19 Release Salesforce has introduced WITH SECURITY_ENFORCED clause, you can use this to enable checking for field- and object-level security permissions on SOQL SELECT queries, including subqueries and cross-object relationships.
  • Currently if you include a field in a SQOL query (Without WITH SECURITY_ENFORCED Clause) and a user doesn’t have access to that field, the field will be returned and can be used by the code without any exception, but the data to that user should not have access.
  • If you use WITH SECURITY_ENFORCED clause for same SOQL Select query, it will throw exception and no data will be returned.
  • This feature is tailored to Apex developers who have minimal development experience with security and to applications where graceful degradation on permissions errors isn’t required. The WITH SECURITY_ENFORCED clause is only available in Apex.
  • The WITH SECURITY_ENFORCED clause is only available in Apex. Using WITH SECURITY_ENFORCED in Apex classes or triggers with an API version earlier than 45.0 is not recommended.

Example:

If the Contact Email & Phone fields permission is not accessible to the user, it will throw an exception insufficient permissions and no data will return.

SELECT Id, Name, (SELECT Email, Phone FROM Contacts) FROM Account WITH SECURITY_ENFORCED

If the Account Website filed permission is not accessible to the user, it will throw an exception insufficient permissions and no data will return.

SELECT Id, Name, Website FROM Account WITH SECURITY_ENFORCED

Salesforce Custom Object to Save Logs

As a Salesforce developer sometimes we face different scenarios, where we need to track production error logs or failed records. To save custom logs, here I have created an object Log__c and a helper class LogHandler, which will help to save custom logs for future references.

Object (Log__c) Fields:

Field Label Field API Name Field Type
Class Class__c Text(255)
Method Method__c Text(255)
Description Description__c Long Text Area(32768)
Line Number Line_Number__c Number(8, 0)
HTTP Response HTTP_Response__c Long Text Area(32768)
HTTP Status Code HTTP_Status_code__c Number(6, 0)
Object Object__c Text(255)
Record Id Record_Id__c Text(255)
Type Type__c Picklist(Success, Error, Information)

Apex Class:

public class LogHandler
{
    //Save information log
    public static void logInformation(String className, String methodName, String description){
        Log__c log = new Log__c();
        log.Class__c = className;
        log.Method__c = methodName;
        log.Description__c = description;
        log.Type__c = 'Information';
        Insert log;
    }
    
    //Save success log
    public static void logSuccessData(String className, String methodName, String description){
        Log__c log = new Log__c();
        log.Class__c = className;
        log.Method__c = methodName;
        log.Description__c = description;
        log.Type__c = 'Success';
        Insert log;
    }
    
    //Save exception log
    public static void logException(String className, String methodName, String description, Integer lineNumber){
        Log__c log = new Log__c();
        log.Class__c = className;
        log.Method__c = methodName;
        log.Description__c = description;
        log.Type__c = 'Error';
        log.Line_Number__c = lineNumber;
        Insert log;
    }
    
    //Save HTTP response log
    public static void logHTTPResponse(String className, String methodName, String description, HttpResponse response){
        Log__c log = new Log__c();
        log.Class__c = className;
        log.Method__c = methodName;
        log.Description__c = description;
        log.Type__c = 'Information';
        if(response != null){
            log.HTTP_Response__c = response.getBody();
            log.HTTP_Status_code__c = response.getStatusCode();			
        }
        Insert log;
    }
    
    //Save result log
    public static void logSaveResult(String className, String methodName, List<Database.SaveResult> saveResultList){
        List<Log__c> logList = new List<Log__c>();
        for (Database.SaveResult sr: saveResultList) {        
            if (sr.isSuccess()) {                    
                Log__c log = new Log__c();
                log.Class__c = className;
                log.Method__c = methodName;
                log.Type__c = 'Success';
                if(sr.getId() != null){
                    log.Object__c = sr.getId().getSObjectType().getDescribe().getName();
                    log.Record_Id__c = sr.getId();
                }
                logList.add(log);                    
            }else{
                Log__c log = new Log__c();
                log.Class__c = className;
                log.Method__c = methodName;
                log.Type__c = 'Error';
                log.Description__c = String.valueOf(sr.getErrors()[0].getMessage());
                logList.add(log);  
            }
        }
        if(!logList.isEmpty()){
            Insert logList;
        }	
    }
    
    //Upsert result log
    public static void logUpsertResult(String className, String methodName, List<Database.UpsertResult> upsertResultList){
        List<Log__c> logList = new List<Log__c>();
        for (Database.UpsertResult ur: upsertResultList) {        
            if (ur.isSuccess()) {                    
                Log__c log = new Log__c();
                log.Class__c = className;
                log.Method__c = methodName;
                log.Type__c = 'Success';
                if(ur.getId() != null){
                    log.Object__c = ur.getId().getSObjectType().getDescribe().getName();
                    log.Record_Id__c = ur.getId();
                }
                logList.add(log);                    
            }else{
                Log__c log = new Log__c();
                log.Class__c = className;
                log.Method__c = methodName;
                log.Type__c = 'Error';
                log.Description__c = String.valueOf(ur.getErrors()[0].getMessage());
                if(ur.getId() != null){
                    log.Object__c = ur.getId().getSObjectType().getDescribe().getName();
                    log.Record_Id__c = ur.getId();
                }
                logList.add(log);  
            }
        }
        if(!logList.isEmpty()){
            Insert logList;
        }	
    }
}

Lightning Formatted URL

A lightning:formattedUrl component displays a read-only representation of a URL as a hyperlink with an href attribute. The link can be a relative or absolute URL.

Example:

<aura:component>  
    <lightning:formattedUrl value="https://www.google.com" tooltip="Google" label="Click to Open" target="_blank" />
</aura:component>

Output: