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
Jayant Kumar JaiswalJayant Kumar Jaiswal 

Error in calling Lightning Component from VisualForce Page

Hi,
I have to simply call a lightning component from a VisualForce Page. Later this VF page is configured as a list button in Opportunity.
VF Page::
<apex:page standardController="Opportunity" recordSetVar="opp"  tabStyle="Opportunity"> 
<apex:includeLightning />
 <script>
    //    var recordid = "{!$CurrentPage.parameters.id}";    
         $Lightning.use("c:LightningOutContainerApp", function() {
         $Lightning.createComponent("c:RelatedListNewRecordPOC",
         { 
         oppRecordId : "0069000000x6KWSAA2" 
         },
         "RelatedListNewRecordPOCContainer",
         function(cmp) {
         console.log('Component created, do something cool here');
         });
         });
 </script>
</apex:page>
Lightning App:
<aura:application access="GLOBAL" extends="ltng:outApp">
 	<aura:dependency resource="c:RelatedListNewRecordPOC"/>	
</aura:application>

Component:
<aura:component controller="RelatedListNewRecordPOCController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,forceCommunity:availableForAllPageTypes" access="global" >
    <aura:attribute name="oppRecordId" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>

JS Controller:
({

    doInit: function(component, event, helper) {
        var myRecordId = "0069000000x6KWSAA2";//component.get("v.recordId");
        var getParameters = component.get('c.getParameters');
        getParameters.setParams({
            "myRecordId": myRecordId
        });

        getParameters.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var Opportunity = response.getReturnValue();               
                var createAcountContactEvent = $A.get("e.force:createRecord");
                createAcountContactEvent.setParams({
                    "entityApiName": "Opportunity",
                    "RecordTypeId": Opportunity.RecordTypeId,
                    "defaultFieldValues": {
                        'AccountId': Opportunity.AccountId,
                        'Name': Opportunity.Name,
                        'StageName':Opportunity.StageName,
                        'CloseDate':Opportunity.CloseDate
                    }
                });
                createAcountContactEvent.fire();

            } else {
                var closeAction = $A.get("e.force:closeQuickAction");
                closeAction.fire();
            }
        });
        $A.enqueueAction(getParameters);
        $A.get("e.force:closeQuickAction").fire();
    },
})

Apex Controller:
public class RelatedListNewRecordPOCController {
    
    @AuraEnabled
    public static Opportunity getParameters(Id myRecordId){
        return [SELECT Id, AccountId, RecordTypeId, Name, Description, StageName, Amount
                , TotalOpportunityQuantity, CloseDate, Type, LeadSource, IsClosed FROM Opportunity WHERE Id =: myRecordId ];    }
	
}
Here I'm getting an error while calling the getParameters() function from LC only when from VF page which is configured in list button.
If I run the LC from actions, it is working.
Error:  $A.getCallback() [Unable to get property 'setParams' of undefined or null reference] Callback failed: apex://RelatedListNewRecordPOCController/ACTION$getParameters
Can anyone please help. As per requirement I have to configure it in list button and I'm not able to dibug this.
 
Shanu Kumar 18Shanu Kumar 18
Hi  Jayant,
you have to replace your apex controller with the following code:
 This the error showing in your apex controller.
Error:- No such column 'RecordTypeId' on entity 'Opportunity'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

public class RelatedListNewRecordPOCController
{
@AuraEnabled
public  sattic Opportunity getParameters(Id myRecordId)
{
return [SELECT Id, AccountId,  Name, Description, StageName, Amount , TotalOpportunityQuantity, CloseDate, Type, LeadSource, IsClosed FROM Opportunity WHERE Id =: myRecordId ];
}
}
Ravi Dutt SharmaRavi Dutt Sharma
Problem is in your JS controller at line 14. $A.get("e.force:createRecord") only works in one.app (when you are in lightning org). If you are in classic UI, and you have included your LC inside a VF page, then $A.get("e.force:createRecord") will always return null.