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
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4 

Use a standard action in lightinig page.

Hello, 

I need to create a lightining component with a button that calls a standard action to create a case. Is there possible?
Durga PaavanDurga Paavan
Hi Leticia,

Yes, You can create using force:createRecord event JS Controller.

Please find below link for the reference, Hope it helps!!
https://developer.salesforce.com/docs/component-library/bundle/force:createRecord/documentation

Cheers,
Durgapaavan
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4
That way I cant show the standart modal for quick action right?
 
Maharajan CMaharajan C
Hi Leticia,

You can use the force:createRecord Event with Quick Action for this:

http://www.sfdcbox.com/2018/07/sneaky-smart-quick-action-to-open.html
http://amulhai.blogspot.com/2018/12/lightning-component-button-create-child.html
http://www.infallibletechie.com/2017/11/forcecreaterecord-example-in-salesforce.html

Thanks,
Maharajan.C
Durga PaavanDurga Paavan
Yes, The force:createRecord will open standard modal for record creation.

Also, the event has additional option to pre populate the values in the standard modal.


Cheers,
Durgapaavan
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4
Hello, I'm getting this error everytime. My code bellow:
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:appHostable" >
	  <lightning:button label="Criar Caso" variant="brand" onclick="{c.createCase}" />
</aura:component>
 
//JSCOntroller

({
	createCase : function(component, event, helper) {
        var createRecordEvent = $A.get('e.force:createRecord');
        if ( createRecordEvent ) {
            createRecordEvent.setParams({
                'entityApiName': 'Case',
                'defaultFieldValues': {
                   
                }
            });
            createRecordEvent.fire();
        }
    }
})



User-added image
Durga PaavanDurga Paavan
Can you try below code
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:appHostable" >
	  <lightning:button label="Criar Caso" variant="brand" onclick="{!c.createCase}" />
</aura:component>
 
//JSCOntroller

({
	createCase : function(component, event, helper) {
        var createRecordEvent = $A.get('e.force:createRecord');
        if ( createRecordEvent ) {
            createRecordEvent.setParams({
                'entityApiName': 'Case'
            });
            createRecordEvent.fire();
        }
    }
})

Hope it resolves the error!.

Cheers
Durgapaavan
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4
Thank you Durga, it works. Is there anyway to choose what recordType I use?

In that way that seets me for a specifc type, the screen for choose doesen show.

User-added image
Durga PaavanDurga Paavan
Can you try below code and give your record type id after ':'.
//JSCOntroller

({
	createCase : function(component, event, helper) {
        var createRecordEvent = $A.get('e.force:createRecord');
        if ( createRecordEvent ) {
            createRecordEvent.setParams({
                'entityApiName': 'Case',
                'defaultFieldValues': {
                   RecordTypeId : //give record type id here
                }
            });
            createRecordEvent.fire();
        }
    }
})
Please mark answer as best answer

Cheers
Durgapaavan
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4
I'd like to show the screen to choose the recordtype.
Durga PaavanDurga Paavan
Hi ,
I thought you need to populate record type value, Sorry for misunderstanding!!.

record type selection is not available by default using force:createRecord Event and you need to implement extra stuff to provide record type selection. Please find below devloper forum discussion thread for the same.
https://salesforce.stackexchange.com/questions/263875/recordtype-selection-list-not-available-in-a-gete-forcecreaterecord/263892

Hope it helps!!.

Cheers
Durgapaavan
Durga PaavanDurga Paavan
There is an idea around the same and you can take a look at comment from Salesforce as well.
https://success.salesforce.com/ideaView?id=0873A000000CR9sQAG

If you find answer helpful, Please Mark as best answer.

Cheers
Durgapaavan
Maharajan CMaharajan C
Hi Leticia,

Please use the below code reference:

1. You Need to create the Apex Class to Fetch the Record Types.
2. On Component load ( Init ) call the Apex method and display the RT.
3. Based on the Selected Record Type you have to pass the parameter in CreateRecord Event.

Apex Class :

public class RecordTypeSelector{

    public static Map<Id, String> recordtypemap;

@AuraEnabled        

    public static Map<Id, String> fetchRecordTypeValues(String objectName){

        List<Schema.RecordTypeInfo> recordtypes = Schema.getGlobalDescribe().get(objectName).getDescribe().getRecordTypeInfos();    

        recordtypemap = new Map<Id, String>();

        for(RecordTypeInfo rt : recordtypes){

            recordtypemap.put(rt.getRecordTypeId(), rt.getName());

        }        

        return recordtypemap;

    }

}


====================
Component :

<aura:component controller="RecordTypeSelector" implements="force:lightningQuickActionWithoutHeader,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >

    

    <aura:attribute name="lstOfRecordType" type="String[]" />

    <aura:attribute name="mapOfRecordType" type="Map" />


    <aura:handler name="init" value="{!this}" action="{!c.fetchListOfRecordTypes}"/>

    

    <lightning:layout multipleRows="true" horizontalAlign="center">

        <lightning:layoutItem flexibility="auto" padding="around-small"

                              size="12"

                              largeDeviceSize="12"

                              mediumDeviceSize="12"

                              smallDeviceSize="12">

            <lightning:formattedText value="Select Case Record Type" />

        </lightning:layoutItem>

        <lightning:layoutItem flexibility="auto" padding="around-small"

                              size="12"

                              largeDeviceSize="12"

                              mediumDeviceSize="12"

                              smallDeviceSize="12">

            

            <!-- select to hold all available record type names list -->

            <lightning:select aura:id="recordTypePickList" name="selectRecordType" label="Select a Record Type">

                <option value="" text="Select Record Type"/>

                <aura:iteration items="{!v.lstOfRecordType}" var="item">

                    <option value="{!item}" text="{!item}"/>

                </aura:iteration>

            </lightning:select>

            

        </lightning:layoutItem>

        <lightning:layoutItem flexibility="auto" padding="around-small"

                              size="3"

                              largeDeviceSize="3"

                              mediumDeviceSize="3"

                              smallDeviceSize="6">

            <lightning:button variant="brand" label="Next" onclick="{!c.createRecord}"/>

        </lightning:layoutItem>

        <lightning:layoutItem flexibility="auto" padding="around-small"

                              size="3"

                              largeDeviceSize="3"

                              mediumDeviceSize="3"

                              smallDeviceSize="6">

            <lightning:button variant="neutral" label="Cancel" onclick="{!c.closeModal}" />

        </lightning:layoutItem>

    </lightning:layout>

</aura:component>


====================

JS Controller: 

({

    fetchListOfRecordTypes: function(component, event, helper) {

        var action = component.get("c.fetchRecordTypeValues");

        action.setParams({

            "objectName" : "Case"

        });

        action.setCallback(this, function(response) {

            var mapOfRecordTypes = response.getReturnValue();

            component.set("v.mapOfRecordType", mapOfRecordTypes);

            var recordTypeList = [];

            //Creating recordTypeList from retrieved Map

            for(var key in mapOfRecordTypes){

                recordTypeList.push(mapOfRecordTypes[key]);

            }

            

            if(recordTypeList.length == 0){ //Object does not have any record types

                //Close Quick Action Modal here

                helper.closeModal();

                

                //Calling CreateRecord modal here without providing recordTypeId

                helper.showCreateRecordModal(component, "", "Case");

            } else{

            component.set("v.lstOfRecordType", recordTypeList);

            }

            

        });

        $A.enqueueAction(action);

    },


    createRecord: function(component, event, helper, sObjectRecord) {

        var selectedRecordTypeName = component.find("recordTypePickList").get("v.value");

        if(selectedRecordTypeName != ""){

            var selectedRecordTypeMap = component.get("v.mapOfRecordType");

            var selectedRecordTypeId;

            

            //finding selected recordTypeId from recordTypeName

            for(var key in selectedRecordTypeMap){

                if(selectedRecordTypeName == selectedRecordTypeMap[key]){

                    selectedRecordTypeId = key;//match found, set value in selectedRecordTypeId variable

                    break;

                }

            }

            //Close Quick Action Modal here

            helper.closeModal();

            

            //Calling CreateRecord modal here without providing recordTypeId

            helper.showCreateRecordModal(component, selectedRecordTypeId, "Case");

        } else{

            alert('You did not select any record type');

        }

        

    },

    

    /*

     * closing quickAction modal window

     * */

    closeModal : function(component, event, helper){

        helper.closeModal();

    }

})


=====================

JS Helper:

({

    showCreateRecordModal : function(component, recordTypeId, entityApiName) {

        var createRecordEvent = $A.get("e.force:createRecord");

        if(createRecordEvent){ //checking if the event is supported

            if(recordTypeId){//if recordTypeId is supplied, then set recordTypeId parameter

                createRecordEvent.setParams({

                    "entityApiName": entityApiName,

                    "recordTypeId": recordTypeId,
                });

            } else{//else create record under master recordType

                createRecordEvent.setParams({

                    "entityApiName": entityApiName,

                });

            }

            createRecordEvent.fire();

        } else{

            alert('This event is not supported');

        }

    },


    closeModal : function(){

        var closeEvent = $A.get("e.force:closeQuickAction");

        if(closeEvent){

        closeEvent.fire();

        } else{

            alert('force:closeQuickAction event is not supported in this Ligthning Context');

        }

    },

})


https://sfdcfacts.com/apex/record-type-selector-quick-action-in-lightning/

Thanks,
Maharajan.C
SARTHAK GARG 4SARTHAK GARG 4
How to implement this when we have change the record type of existing record
Rajesh andani 4Rajesh andani 4

Hi @ Durga

I am using e.force:createRecord in my component to override standard button but when I click on new button my background component changes. Can you tell me how can I set same background.