Play Games

Search This Blog

Tuesday, September 1, 2020

Create Salesforce record using aura component Salesforce lightning - Salesforce Globe For You

 Create Salesforce record using aura component Salesforce lightning  - Salesforce Globe For You 

In this example,we will be creating form to create Lead Record.

Apex Class:

public class LeadCreationController {

    @AuraEnabled

    public static void createNewLead(Lead objLead) {

        insert objLead;

        system.debug('objLead:'+objLead);

    }

    @AuraEnabled

    public static Lead initializeLead() {

        Lead objLead = new Lead();

        system.debug('objLead:'+objLead);

        return objLead;

    }

}

LeadCreationForm.cmp

<aura:component controller="LeadCreationController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">

    <aura:attribute name="newLead" type="Lead"/>

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <div aria-labelledby="newLeadForm">

        <fieldset class="slds-box slds-theme--default slds-container--small">

            <legend id="newexpenseform" class="slds-text-heading--small 

                                               slds-p-vertical--medium">

                Create Lead

            </legend>

            <form class="slds-form--stacked">

                <lightning:input aura:id="firstName" label="First Name"

                                 name="FirstName"

                                 value="{!v.newLead.FirstName}"/>

                

                <lightning:input aura:id="lastName" label="Last Name"

                                 name="LastName"

                                 value="{!v.newLead.LastName}"

                                 required="true"/>

                

                <lightning:input aura:id="company" label="Company"

                                 name="Company"

                                 value="{!v.newLead.Company}"

                                 required="true"/>

                <lightning:button label="Create Lead" 

                                  class="slds-m-top--medium"

                                  variant="brand"

                                  onclick="{!c.createLead}"/>

            </form>

        </fieldset>

    </div>

</aura:component>

LeadCreationFormController.js

({

  doInit : function(component, event, helper) {

        

        var action = component.get("c.initializeLead");

        action.setCallback(this, function(response) {

            var state = response.getState();

            if (state === "SUCCESS") {

                console.log('Success');

              component.set("v.newLead", response.getReturnValue());

            }

        });

        $A.enqueueAction(action);

    },

    

    createLead : function(component, event, helper) {

        var action = component.get("c.createNewLead");

        action.setParams({

            objLead:component.get("v.newLead")

        });

        action.setCallback(this, function(response) {

            var state = response.getState();

            if (state === "SUCCESS") {

                console.log('Success');

                alert("Lead Record Created Successfully");

                var action1 = component.get("c.initializeLead");

                action1.setCallback(this, function(response) {

                    var state = response.getState();

                    if (state === "SUCCESS") {

                        console.log('Success');

                        component.set("v.newLead", response.getReturnValue());

                    }

                });

                $A.enqueueAction(action1);

                }

        });

        $A.enqueueAction(action);

    

  }

})

TestApp.app

<aura:application extends="force:slds">

  <c:LeadCreationForm/>

</aura:application>

Output:





No comments:

Post a Comment