2

Problem Statement - Lightning form in force.com sites not inserting lead records. I had to create a Web to lead form in lightning and host it in force.com sites. Since force.com sites can include only visualforce pages, I created a VF page and included a lightning component in it. When I submit the form via force.com sites, it does not insert the record and no error is returned. However, if I host this same lightning component as a standalone component on Account layout, the records are getting inserted.

Visualforce page -

<apex:page standardStylesheets="false" showHeader="false" sidebar="false" standardController="Lead" >
   <apex:includeLightning />
    <div id="lightning" />
    <script>
    $Lightning.use("c:InquiryFormApp",    // name of the Lightning app
        function() {                  // Callback once framework and app loaded
            $Lightning.createComponent(
                "c:createLeadRecord", // top-level component of your app
                { },                  // attributes to set on the component when created
                "lightning",   // the DOM location to insert the component
                function(cmp) {
                    // callback when component is created and active on the page
                }
            );
        },
        '/AppInquiry'  // Community endpoint
    );
</script>                                              
</apex:page>

Lightning App -

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

Lightning component -

<!-- Include Static Resource-->

<!-- Define Attribute-->
<aura:attribute name="candidate" type="Lead" default="{'sobjectType': 'Lead',
                     'LastName': ''
                   }"/>
<div class="container-fluid">
    <h3>Please Enter The Candidate Information</h3>

    <div class="form-group">
        <label>Last Name</label>
        <ui:inputText class="form-control" value="{!v.candidate.LastName}"/>
    </div>


</div>
<div class="col-md-4 text-center">
    <ui:button class="btn btn-default" press="{!c.create}">Create</ui:button>

Lightning component controller -

({
    create : function(component, event, helper) {
        console.log('Create record');
        //getting the candidate information
        var candidate = component.get("v.candidate");
        //Calling the Apex Function
        var action = component.get("c.createRecord");
        //Setting the Apex Parameter
        action.setParams({
            candidate : candidate
        });

        //Setting the Callback
        action.setCallback(this,function(a){
            //get the response state
            var state = a.getState();
            //check if result is successfull
            if(state == "SUCCESS"){
                //Reset Form
                var newCandidate = {'sobjectType': 'Lead',
                                    'LastName': ''
                                   };
                //resetting the Values in the form
                component.set("v.candidate",newCandidate);
                alert('Record is Created Successfully');
            } else if(state == "ERROR"){
                alert('Error in calling server side action');
            }
        });

        //adds the server-side action to the queue        
        $A.enqueueAction(action);

    }
})

Apex controller -

global class CreateLeadRecord {
    /**
   * Create a new candidate Record
   *
   * @param Candidate__c candidate  candidate record to be inserted
   * 
   */  
    @AuraEnabled
    public static void createRecord (Lead candidate){

        try{
            System.debug(': CreateCandidateRecord::createRecord::candidate'+candidate);

            if(candidate != null){
                insert candidate;
            }

        } catch (Exception ex){
            system.debug(': exception is-'+ex.getMessage());
        }
        //return candidate.Id;
    }
}

Please suggest something by which I'll be able to create a Web to Lead form using Lightning and Force.com Sites.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.