Lightning : Issue while calling createRecord Event Component

I am trying to embed a lightning component inside a visualforce page –

MytestVFPage visualforce page-

<apex:outputPanel >
       <div id="lightning" />
        <script>
            $Lightning.use("c:createAccountGlobalApp", function() {     
            $Lightning.createComponent("c:CreateAccount",
                                       {},
                                       "lightning",
                                       function(component) {});
        });

        </script>
  </apex:outputPanel>


**createAccountGlobalApp** container -

<aura:application access="GLOBAL" extends="ltng:outApp"> 
    <aura:dependency resource="c:CreateAccount"/>
</aura:application>

**createAccount** component -
<aura:component >
    <div class = "container">
        <ui:button label="Lightning button" press="{!c.createAccountButton}">    </ui:button>
    </div>
</aura:component>

createAccountController.js controller

({
    createAccountButton : function(component, event, helper) {
        helper.createRecord(component);
    }
})

createAccountHelper.js helper-

({
    createRecord:function(component) {
            var createRecordEvent = $A.get("e.force:createRecord");
            createRecordEvent.setParams({
            "entityApiName": "Account"

            });
    createRecordEvent.fire();
    }
})

When I click on the UI button inside visualforce page it throws an error –

“Something has gone wrong. Action failed: c$CreateAccount$controller$createAccountButton [TypeError: Cannot read property ‘setParams’ of undefined] Failing descriptor: {c$CreateAccount$controller$createAccountButton}. Please try again.

Can you please help what is wrong here?

Answer

I’m afraid you cannot use force:createRecord outside of LEX/SF1, below is the note from the doc.

force:createRecord event is handled by the one.app container. It’s
supported in Lightning Experience and Salesforce1 only. If used
outside of Lightning Experience or Salesforce1, this event won’t be
handled automatically. To use this event outside of one.app, create
and wire up an event handler of your own.

Looks like you have to build an custom component to get the account details input and create it.

Attribution
Source : Link , Question Author : Hitesh , Answer Author : Praveen

Leave a Comment