function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sharmila Sahu 12Sharmila Sahu 12 

I want to set record type while creating record in lightning component through e.force:createRecord. But I am not able to do so

I want to set record type while creating record in lightning component through e.force:createRecord. But I am not able to do so.
I tried using custom label and even I tried hardcoding still It is not showing correct record type and it is showing default record type for record creation.
Please find below code for javascript controller:
({
    doInit: function (component, event, helper) {
        var recId = component.get("v.recordId");
        console.log(recId);

        var action = component.get("c.getdetails");
        action.setParams({
            "sId": recId
        });
        action.setCallback(this, function (response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                $A.get("e.force:closeQuickAction").fire();
                console.log("Success" + JSON.stringify(response.getReturnValue()));
                component.set("v.sampleRequest", response.getReturnValue());
                var sampProd = component.get("v.sampleRequest");
                
                 if(sampProd.Lead__c === sampProd.Contact__c){
                    var Name = sampProd.Contact__r.Name;
                }
                else
                {
                   var Name = sampProd.Lead__r.Name; 
                }
                


                var createSampleProduct = $A.get("e.force:createRecord");
                var staticRecordLabel = $A.get("$Label.c.UsCanSampleProductRecordType");
                createSampleProduct.setParams({
                    "entityApiName": "Sample_Product__c",
                    "RecordTypeId": '0121b0000004aeSAAQ',
                    "defaultFieldValues": {
                        'Sample_Request__c' : component.get("v.recordId"),
                        'Name__c': Name,                    
                    }
                });
                createSampleProduct.fire();
            } else {
                console.log("Fail");
            }
        });
        $A.enqueueAction(action);
    }
})

I tried hardcoding:
              var staticRecordLabel = $A.get("$Label.c.UsCanSampleProductRecordType");
                createSampleProduct.setParams({
                    "entityApiName": "Sample_Product__c",
                    "RecordTypeId": '0121b0000004aeSAAQ',

I have tried using:

            var staticRecordLabel = $A.get("$Label.c.UsCanSampleProductRecordType");
                createSampleProduct.setParams({
                    "entityApiName": "Sample_Product__c",
                    "RecordTypeId": staticRecordLabel ,

it is just showing for 1 sec 
UsCanSampleProductRecordType and then it is taking me to record creation page with default record type.

Please help. 
Best Answer chosen by Sharmila Sahu 12
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sharmila,

Greetings to you!

You need to use recordTypeId instead of RecordTypeId as lightning is based on aura framework which is a type of JavaScript and JavaScript is case sensitive so lightning is case sensitive.

Please refer to the below links which might help you further.

http://sfdcmonkey.com/2017/05/09/custom-record-type-selection-lightning-component/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Sharmila,

Greetings to you!

You need to use recordTypeId instead of RecordTypeId as lightning is based on aura framework which is a type of JavaScript and JavaScript is case sensitive so lightning is case sensitive.

Please refer to the below links which might help you further.

http://sfdcmonkey.com/2017/05/09/custom-record-type-selection-lightning-component/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Sharmila Sahu 12Sharmila Sahu 12
Thanks. It worked. Issue was due to case sensitive nature of lightning only.It got resolved when I used recordTypeId instead of RecordTypeId
Nirdesh_BhattNirdesh_Bhatt
Hi,
If you use 'recordTypeId' then it will be Hardcoding ID so it's not salesforce standard so can you please help me to use the developer name? 
I tried 'recordTypeName' also tried Developer name but not working. Can you please help me for this