Saturday, 6 August 2016

Create Lead Record using Lightning Component

Hi,

This example explains you how to create lead record from Lightning component.

here we did the following things

  • one apex class with aura enabled methods  which has lead insert.
  • one  Lightning component you can see previous example how to create a component
  • included lightning styles from lightning design system.
  • include controller with lightning component to invoke apex method and process
  • created one app to execute component
Apex class:


public class LightningLeadCreatecls {
   @auraenabled
    public static Id creatLeadRecord(Lead leadObj){
        upsert leadObj;
        return leadObj.id;
    }
}

Component:leadCreation

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="LightningLeadCreatecls">
 <!--including lightning styles-->
    <ltng:require styles="{!$Resource.Lightning_slds+'/assets/styles/salesforce-lightning-design-system.css'}"/>
     <ltng:require styles="{!$Resource.Lightning_slds+'/assets/styles/salesforce-lightning-design-system.min.css'}"/>
<!-- Preparation of lead object with fields-->
    <aura:attribute name="leadObj" type="Lead"  default="{'sobjectType':'lead',
                                                           'FirstName':'',
                                                            'LastName':'',
                                                          'Company':'',
                                                      'Email':''}"/> 
   <div class="slds">
       <div class="slds-form-element">
          <label class="slds-form-element__label" for="FirstName">First Name</label>
          <div class="slds-form-element__control">
            <ui:inputText aura:id="FirstName" class="slds-input" value="{!v.leadObj.FirstName}" placeholder="First Name" />
          </div>
        </div>
       <div class="slds-form-element">
          <label class="slds-form-element__label" for="LastName">Last Name</label>
          <div class="slds-form-element__control">
            <ui:inputText aura:id="LastName" class="slds-input" value="{!v.leadObj.LastName}" placeholder="Last Name" />
          </div>
        </div>
       <div class="slds-form-element">
          <label class="slds-form-element__label" for="Company">Company</label>
          <div class="slds-form-element__control">
            <ui:inputText aura:id="Company" class="slds-input" value="{!v.leadObj.Company}" placeholder="Company" />
          </div>
        </div>
       <div class="slds-form-element">
          <label class="slds-form-element__label" for="Email">Email</label>
          <div class="slds-form-element__control">
            <ui:inputText aura:id="Email" class="slds-input" value="{!v.leadObj.Email}" placeholder="Email" />
          </div>
        </div>
       <div class="slds-form-element">        
          <div class="slds-form-element__control">
            <ui:button label="Save" press="{!c.save}"/>
          </div>
        </div>
</div>
</aura:component>

Controller:
-----------
({
save : function(component, event, helper) {    
    var action = component.get("c.creatLeadRecord");
            action.setParams({"leadObj":component.get("v.leadObj")});
            action.setCallback(this,function(result){
            component.set("v.isShow",false);
            var leadId = result.getReturnValue();
            alert('leadId'+leadId);
        });
         $A.enqueueAction(action);
}
})

App:
------
<aura:application >
    <c:LeadCreation />
</aura:application>


output:
--------



12 comments:

  1. https://salesforce-walker.blogspot.in/2016/08/create-lead-record-using-lightning.html?showComment=1478174941430#c959419562638357894

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. How do we save custom field(check box with multiple options) on lead object on this lightning component or page ?

    ReplyDelete
  4. it is returning a null value for me- somebody please assist.
    i am trying to create a record through a form in custom object. using the similar approach . but it is not creating any record and it is returning me a null value.

    ReplyDelete
    Replies
    1. Could you please help me with code.

      Delete
    2. Did you ever solve this?

      Delete
  5. This code is very helpful but i want to get the detail page when we click on save button .
    Please let me know
    Thank you.

    ReplyDelete

What’s the difference between Einstein Article Recommendations and Suggested Articles?

How Does Einstein Article Recommendations Work? Einstein Article Recommendations helps support agents resolve customer cases efficiently by ...