0

I want to create a custom lightning component which would allow me to create a record of a specific object. I decided to use quick action for this.

I googled the documentation over here and found the following example.

<!--quickAdd.cmp-->
<aura:component implements="force:lightningQuickAction">

    <!-- Very simple addition -->

    <lightning:input type="number" name="myNumber" aura:id="num1" label="Number 1"/> +
    <lightning:input type="number" name="myNumber" aura:id="num2" label="Number 2"/>

    <br/>
    <lightning:button label="Add" onclick="{!c.clickAdd}"/>

</aura:component>

The example does not answer the question of how could I tell the component to display fields for a specific object.

2

Use e.force:createRecord to create object specific record by changing the entityApiName in controller and also you can prepopulate the values for a particular field in defining defaultfieldvalues. For example if you want to work on contact, put contact in entityApiName . After that you can create a quick action and override with this lightning component and add it to any page.

CreateAccountComponent.cmp

<aura:component implements="flexipage:availableForAllPageTypes,force:lightningQuickAction">
    <lightning:button label="Create Account" variant="brand" onclick="{!c.createAccount}"/>
</aura:component>

CreateAccountComponentcontroller.js

({
createAccount: function (component) {
        var createRecordEvent = $A.get('e.force:createRecord');
        if ( createRecordEvent ) {
            createRecordEvent.setParams({
                'entityApiName': 'Account',
                'defaultFieldValues': {
                    'Type' : 'Prospect',
                    'Industry' : 'Apparel',
                    'Rating' : 'Hot'
                }
            });
            createRecordEvent.fire();
        } else {
            /* Create Record Event is not supported */
            alert("Account creation not supported");
        }
    }
})

Or else you can visit this link :- How to insert a record via lightning compenent?

You just need to add implements force:lightningQuickAction in the code in above link and make the object changed as per your need. You can also use this as quick action. This one will provide more flexibility to achieve any other custom functionality.

1
  • e.force:createRecord is not applicable to custom lightning apps (every time you click save you get redirected to the record page). force:lightningQuickAction requires manual custom lightning component updates each time the object changes. – iloveseven Aug 16 '18 at 15:46

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.