[SalesForce] Selecting Record Type at run time when using force:createRecord

I am using force:createRecord() event to open a new record creation window form lightning component.The functionality that I observed is :

  • If I mention recordTypeId Attribute, it is opening record creation window and deafulting the record type to the Id that I have mentioned.This is fine.

  • If I dont mention any record type Id attribute then it is opeing record creation window with the default recordtype for that profile which is also fine from logical perspective.

I am actually trying to show the record type selection screen for the users when I don't mention any recordTypeId attribute and pre-populate some values using defaultFieldValues Attribute.I would like to know if this is possible currently in lightning. I am using lightning component for showing record creation window because I want to create record conditionally based on some checks.

Best Answer

This should be possible. I've modified the example from the doc here just slightly. Assume yourRecordTypeId is populated with the right Id.

var createRecordEvent = $A.get("e.force:createRecord");
createRecordEvent.setParams({
   "entityApiName": "WhatEverObjectWithRecordTypes__c",
   "defaultFieldValues": {
         'RecordTypeId' : yourRecordTypeId,
    }
 });
 createRecordEvent.fire();

I would use some lines in an apex controller to get the available Record Type Ids via a SOQL query together with their Names and possibly show that in a custom picklist dynamically. Or use any logic in JavaScript.

Be careful while deploying the code to different Org: Record Type Ids might be different even if the Name is the same.