4

We have about three different record types for a custom object, for which I am trying to create a lightning component that will override the standard behaviour. In lightning experience, we are presented with the "Record Type selection screen" as shown below.

Record Type Selection Screen

Once we select a record type the lightning component shows up however I am unable to find any standard way to get the recordTypeId from the previous selection window.

It is available in the url as /new?recordTypeId=012360000008N3z&additionalParams....

My question is regarding any standard technique for getting the record type Id. Maybe, something similar to force:hasRecordId interface like force:hasRecordTypeId.

I have looked through the lightning documentation to no avail and I might have to fallback to URL parsing.

<!-- My Component implements the following interface -->
<aura:component implements="force:hasRecordId,lightning:actionOverride">
 ...
</aura:component>
11

Summer'18 update provided a new interface 'lightning:hasPageReference'. You can now access the recordTypeId in your controller as below:

var recordTypeId = component.get("v.pageReference").state.recordTypeId;

https://developer.salesforce.com/docs/component-library/bundle/lightning:hasPageReference/documentation

| improve this answer | |
  • Remember to add implements="lightning:isUrlAddressable" in aura:component tag. – rmatusiak May 10 '19 at 13:05
  • 1
    Why does mine come out undefined – SallyRothroat May 15 '19 at 21:56
  • Is there any way to achieve this in lightning web components? – Ayush Goyal Mar 4 at 10:59
  • You can wrap the LWC in an aura component that implements the interface "lightning:hasPageReference" with a variable to store the record type Id and pass it to the LWC. You have to explicitly fetch and assign the value to the record type Id variable in the aura component's controller init method. – Vinod Rondla Mar 6 at 1:14
1

Try using force:createRecord which has a recordTypeId attribute.

var createAcountContactEvent = $A.get("e.force:createRecord");
createAcountContactEvent.setParams({
    "entityApiName": "Contact",
    "defaultFieldValues": {
        'Phone' : '415-240-6590',
        'AccountId' : '001xxxxxxxxxxxxxxx',
        'recordTypeId': "1234455566"
    }
});
createAcountContactEvent.fire();
| improve this answer | |
  • Thanks Saroj, but I am overriding the object's 'New' action and have no control over the event and default values Salesforce will be forwarding. – Learner Apr 26 '18 at 8:08
  • 1
    You will need to select "Skip record type selection page", override your new button with your custom component where you create custom record type selection, capture the id of the selection and then proceed further. If you are using the standard page layout for record creation, you can use what Saroj has mentioned here once you have captured the record type. – Jayant Das Apr 26 '18 at 13:07
  • Thanks Jayant. I think that is the only safe option available. As the url parameter isn't documented anywhere formally and is subject to change by Salesforce. – Learner Apr 30 '18 at 6:58
0

I have faced same issue. I have found very simple solution. I may be late in answering but it can help someone in need.

Component:

<aura:attribute name="recordTypeIdVar" type="String"/>
    <lightning:recordEditForm aura:id="recordEditForm" 
                              objectApiName="Case"
                              recordId="{!v.recordId}" 
                              recordTypeId="{!v.recordTypeIdVar}"
                              onload="{!c.showRequiredFields}"
                              onsuccess="{!c.handleSuccess}">
                <h3 class="slds-section-title--divider">
                    <p>RecordType: {!v.recordTypeIdVar}</p>
                    <lightning:outputField fieldName="RecordTypeId"/> 
                </h3>

blabla code here.. and close the component

Controller:

showRequiredFields: function(component, event, helper){
       // To get the record type from URL as it is not happening by default.
        var recordTypeIdParam = helper.getJsonFromUrl().recordTypeId;
         //alert("recordTypeId= " + recordTypeIdParam);
        component.set("v.recordTypeIdVar", recordTypeIdParam);
    },

Helper:

({
 getJsonFromUrl : function () {
        var query = location.search.substr(1);
        var result = {};
        query.split("&").forEach(function(part) {
            var item = part.split("=");
            result[item[0]] = decodeURIComponent(item[1]);
        });
        return result;
    }
})
| improve this answer | |

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.