Tag Archives: Record Type

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:

Record Type Selector Custom Lightning Component

Apex Controller:

public class SampleAuraController {
    
    @AuraEnabled
    public string defaultRecordTypeId {get; set;}
    @AuraEnabled
    public Map<Id, String> contactRecordTypes {get; set;}
    
    @AuraEnabled        
    public static SampleAuraController getRecordTypeValues(){
        SampleAuraController obj = new SampleAuraController();
        Map<Id, String> recordtypeMap = new Map<Id, String>();
        //Get all record types of Contact object
        List<Schema.RecordTypeInfo> recordTypeInfoList = Contact.SObjectType.getDescribe().getRecordTypeInfos();
        for(RecordTypeInfo info: recordTypeInfoList) {
            //Check record type is available for current user profile
            if(info.isAvailable()) {
                //Check master record type
                if(info.getName() != 'Master' && info.getName().trim() != ''){
                    recordtypeMap.put(info.getRecordTypeId(), info.getName());
                }
                //Get the default record type for current user profile
                if(info.isDefaultRecordTypeMapping()){
                    obj.defaultRecordTypeId = info.getRecordTypeId();
                }
            }
        }    
        obj.contactRecordTypes = recordtypeMap;
        return obj;
    }
}

Lightning Component:

<aura:component controller="SampleAuraController" implements="flexipage:availableForAllPageTypes,force:appHostable">
    
    <!--Declare Attributes-->
    <aura:attribute name="recordTypeMap" type="Map"/>
    <aura:attribute name="selectedRecordTypeId" type="String"/>
    <!--Declare Handler-->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>  
    
    <!--Component Start-->
    <div class="slds-m-around--xx-large">
        <div class="container-fluid">
            <lightning:radioGroup name="radioGroup"
                                  label="Select Record Type"
                                  required="true"
                                  options="{!v.recordTypeMap}"
                                  value="{!v.selectedRecordTypeId}"
                                  type="radio"/>
        </div>
        <br/>
        <lightning:button variant="brand" label="Submit" onclick="{!c.handleCreateRecord}" />  
    </div>
</aura:component>

JS Controller:

({
    doInit: function(component, event, helper) {        
        var action = component.get("c.getRecordTypeValues");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var result = response.getReturnValue();
                var recordTypes = result.contactRecordTypes;
                var recordtypeMap = [];
                for(var key in recordTypes){
                    recordtypeMap.push({label: recordTypes[key], value: key});
                }
                component.set("v.recordTypeMap", recordtypeMap);
                component.set("v.selectedRecordTypeId", result.defaultRecordTypeId);
            }
        });
        $A.enqueueAction(action);
    },
    
    handleCreateRecord: function(component, event, helper) { 
        var selectedRecordTypeId = component.get("v.selectedRecordTypeId");
        if(selectedRecordTypeId){
            var createRecordEvent = $A.get("e.force:createRecord");
            createRecordEvent.setParams({
                "entityApiName": 'Contact',
                "recordTypeId": selectedRecordTypeId,
            });
            createRecordEvent.fire();
        }
    }
})

Output:

Retrieve the Record Type Which are Accessible by User’s Profile only

The below code will add only the record type of “Contact” object accessible to the user.

List <SelectOption> rtList = new List <SelectOption>();
for (RecordTypeInfo rtInfo: Contact.SObjectType.getDescribe().getRecordTypeInfos() ) {
	if(rtInfo.isAvailable()) {
		rtList.add(new SelectOption(rtInfo.getRecordTypeId(), rtInfo.getName()));
	}
}

Get Default Record Types For Current User Profile On Specific Object Using Apex

Sample Code:

//Get all record types on Account object
List<Schema.RecordTypeInfo> recordTypeInfoList = Account.SObjectType.getDescribe().getRecordTypeInfos();
String defaultRecordTypeId = '';
for(RecordTypeInfo info: recordTypeInfoList) {
    //Check default record type
    if(info.isDefaultRecordTypeMapping()){
        defaultRecordTypeId = info.getRecordTypeId();
        break;
    }
}
System.debug('DefaultRecordTypeId-' + defaultRecordTypeId);

Select Record Types For Current User Profile On Specific Object

Sample Code:

//Get all record types on Account object
List<Schema.RecordTypeInfo> recordTypeInfoList = Account.SObjectType.getDescribe().getRecordTypeInfos();
List<SelectOption> recordTypes = new List<SelectOption>();
for(RecordTypeInfo info: recordTypeInfoList) {
    //Check record type is available for current user profile
    if(info.isAvailable()) {
        if(info.getName() != 'Master' && info.getName().trim() != ''){
            recordTypes.add(new SelectOption(info.getRecordTypeId(), info.getName()));
        }
    }
}
System.debug('recordTypes-' + recordTypes);