0

I have a lightning Component to display the Account and its Contact relationship in tree view using Accordion SLDS. On this page, I want to put "New Contact" button to create a contact record.

Is it possible to get the standard lightning experience popup to create a record from New button on lightning component?

I tried the below one, but no luck,

<ui:button label="New" press="{!c.createRecord}"/>

Controller

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

getting error:

$createRecord [Cannot read property 'setParams' of undefined]

Is there any way out there to get the standard lightning experience record creation page layout by clicking the New button in lightning component?.

Thanks in advance!

  • why the visualforce and Apex tags? – glls Jan 5 '18 at 19:24
  • Am very new to lightning experience. Since visualforce is a successor of lightning components, tagged it. – Prince Jan 5 '18 at 19:26
  • please remove them, as well as the apex tag, as they are unrelated for your use case (or so it would seem). One last question, where are you using this component, in communities or lightning experience? – glls Jan 5 '18 at 19:28
  • Lightning experience – Prince Jan 5 '18 at 19:29
  • I honestly cant spot anything wrong with your component, did you create an app throught the app builder , drag and drop it, and this is the e rror you get every time you click your button? – glls Jan 5 '18 at 19:53
2

I resolved it. Am posting here to save other's time,

I was getting that error if am viewing the page from application preview. Then I placed a lightning component tab and try to click on the button, it is opening the record creation popup.

  • make sure you accept your answer =) – glls Jan 12 '18 at 0:35
1
 var createLeadEvent = $A.get("e.force:createRecord");
        createLeadEvent.setParams({
            "entityApiName": "Lead",
            "defaultFieldValues": {
                    'Title' : 'TestRecord',
                    'FirstName' : 'TestFirstName',
                    'LastName' : 'TestSecondName',
        });

Here, createRecord - is an inbuilt event to force create a new record, entityApiName - it defines an Object where you go to create a record, defaultFieldValues - defines prepopulate data.

  • This approach works with hard-coded default field values. Is there some way of passing variables for the fields? – abhi Jun 11 at 18:49

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.