Friday 24 April 2020

Contact and Related List Using Salesforce Lightning Component Event

Requirement


Create a Lightning Component for creating contact record and show that contacts on the Account page using Component Event.

cmpAccountContactRelatedList.cmp


<aura:component controller="cmpContactListController"
                implements="force:hasRecordId,flexipage:availableForAllPageTypes"> 

    <aura:attribute name="contactList" type="Contact[]"/>
 
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:handler name="QuickContact" event="c:QuickContactEvent"
                                                                 action="{!c.handleCompEvent}"/>
 
    <div>
        <c:cmpCreateContactRecord AccountId="{!v.recordId}"/>
    </div>
 
    <div class="slds-p-around_small">   

        <div class="slds-grid slds-wrap">
     
            <aura:iteration items="{!v.contactList}" var="objContact">

                <div class="slds-col slds-size_1-of-3 slds-p-around_small">
               
                    <lightning:card title="{!objContact.LastName}"  footer="{!objContact.Email}"
                                    iconName="standard:contact">

                        <aura:set attribute="actions">

                            <lightning:button
                                              label="View Details"
                                              variant="brand"
                                              name="{!objContact.Id}"
                                              onclick="{!c.doRedirect}"/> 
                     
                        </aura:set>

                        <p class="slds-p-horizontal_small">

                            {!objContact.FirstName}&nbsp;&nbsp; {!objContact.LastName} <br/>
                            {!objContact.Phone}

                        </p>

                    </lightning:card> 
               
                </div>

            </aura:iteration>

        </div>

    </div>

</aura:component>

cmpAccountContactRelatedListController.js


({

    doInit : function(component, event, helper) {

        var action = component.get('c.getContactList');
        action.setParams({
            accountId : component.get('v.recordId'),
        });

        action.setCallback(this, function(response){
            var responseValue = response.getReturnValue();
            component.set('v.contactList', responseValue);
        },'SUCCESS');

        $A.enqueueAction(action);

    },

    doRedirect : function(component, event, helper) {

        var eventSource = event.getSource();
        var id = eventSource.get('v.name');

        var navEvt = $A.get("e.force:navigateToSObject");

        navEvt.setParams({
            "recordId": id,
            "slideDevName": "detail"
        });

        navEvt.fire();
    },

    handleCompEvent : function(component, event, helper) {

        var availableContact = component.get('v.contactList');
        var contactRecord = event.getParam('contactRecord');
        availableContact.push(contactRecord);
        component.set('v.contactList',availableContact);

    }
})


Component Event

QuickContactEvent.evt


<aura:event type="COMPONENT" description="Event template">
    <aura:attribute name="contactRecord" type="Contact"/>
</aura:event>

cmpCreateContactRecord.cmp


<aura:component controller="cmpContactListController">
 
    <aura:attribute name="AccountId" type="string"/>

    <aura:registerEvent name="QuickContact" type="c:QuickContactEvent"/>

<aura:attribute name="createContact" type="Contact" default="{
                                                                 sObject : 'Contact',
                                                                 FirstName : '',
                                                                 LastName : '',
                                                                 Email : '',
                                                                 Phone : ''
                                                                 }"/>

    <div class="slds-var-p-around_x-small">

    <lightning:input label="First Name" type="text" value="{!v.createContact.FirstName}"
                          required="true"/>
        <lightning:input label="Last Name" type="text" value="{!v.createContact.LastName}"
                          required="true"/>
        <lightning:input label="Email" type="email" value="{!v.createContact.Email}"
                          required="true"/>
        <lightning:input label="Phone" type="phone" value="{!v.createContact.Phone}"
                          required="true"/>
     
        <lightning:button label="Create Contact" variant="brand" onclick="{!c.doSave}"/>

    </div>
   
</aura:component>

cmpCreateContactRecordController.js

({

doSave : function(component, event, helper) {

var action = component.get('c.createContact');

        action.setParams({
            objContact : component.get('v.createContact'),
            AccountId : component.get('v.AccountId'),
         
        });

        action.setCallback(this,function(response){

            var state = response.getState();     
 
            if(state === 'SUCCESS' || state === 'DRAFT'){
                var responseValue = response.getReturnValue();
                var componentEvent = component.getEvent('QuickContact');
                componentEvent.setParams({
                    contactRecord : responseValue
                });

                componentEvent.fire();

            }
        },'ALL');

        $A.enqueueAction(action);

}
})

Controller

cmpContactListController.cls


public with sharing class cmpContactListController {
     
    @AuraEnabled

    public static Contact createContact(contact objContact , Id AccountId){

        objContact.accountid = AccountId;
        insert objContact;
        return objContact;

    }
}

Add this component on the Account record page.

Output:

Before Creating Contact :


After Creating Contact :


Other Useful Blog
Lightning Web Component


Happy Learning !!

Calculator Using Salesforce Lightning Components

Requirement :
Create a Lightning Component having two input fields which will take two number as input and show result. Addition, Subtraction, Multiplication and Division  happen on button click.
Or
In other words , create a Calculator Lightning Component.

cmpCalculator.cmp

<aura:component >
 
    <aura:attribute name="firstNumber" type="Integer" />
    <aura:attribute name="secondNumber" type="Integer" />
    <aura:attribute name="result" type="Integer" />
    <aura:attribute name="isAdd" type="Boolean" default="false"/>
    <aura:attribute name="isSub" type="Boolean" default="false"/>
    <aura:attribute name="isMul" type="Boolean" default="false"/>
    <aura:attribute name="isDiv" type="Boolean" default="false"/>
    <aura:attribute name="isRefresh" type="Boolean" default="false"/>
 
    <div style="width: 485px;margin-left:20px;">
     
        <lightning:input label="First Number" value="{!v.firstNumber}"/>
        <lightning:input label="Second Number" value="{!v.secondNumber}"/>
     
        <br/>
     
        <lightning:button label="Addition" onclick="{!c.doAddition}"/>
        <lightning:button label="Subtraction" onclick="{!c.doSubtraction}"/>
        <lightning:button label="Multiplication" onclick="{!c.doMultiplication}"/>
        <lightning:button label="Division" onclick="{!c.doDivision}"/>
     
        <aura:if isTrue="{!v.isRefresh}">
            <lightning:button label="Refresh" onclick="{!c.doRefresh}"/>
        </aura:if>
     
        <br/><br/>
        <aura:if isTrue="{!v.isAdd}">
            Addition of two number is : {!v.result}     
        </aura:if>
     
        <aura:if isTrue="{!v.isSub}">
            Subtraction of two number is : {!v.result}     
        </aura:if>
     
        <aura:if isTrue="{!v.isMul}">
            Multiplication of two number is : {!v.result}     
        </aura:if>
     
        <aura:if isTrue="{!v.isDiv}">
            Division of two number is : {!v.result}     
        </aura:if>
     
    </div>
 
</aura:component>

cmpCalculator.js

({
 
doAddition : function(component, event, helper) {
     
var fNumber = component.get('v.firstNumber');
        var sNumber = component.get('v.secondNumber');
     
        var addition = parseInt(fNumber) + parseInt(sNumber);
     
        component.set('v.result' , addition);
        component.set('v.isAdd' , true);
        component.set('v.isRefresh' , true);
        component.set('v.isSub' , false);
        component.set('v.isMul' , false);
        component.set('v.isDiv' , false);     
},
 
    doSubtraction : function(component, event, helper) {
     
var fNumber = component.get('v.firstNumber');
        var sNumber = component.get('v.secondNumber');
     
        var subtraction = parseInt(fNumber) - parseInt(sNumber);
     
        component.set('v.result' , subtraction);
        component.set('v.isAdd' , false);
        component.set('v.isSub' , true);
        component.set('v.isRefresh' , true);
        component.set('v.isMul' , false);
        component.set('v.isDiv' , false);     
},
 
    doMultiplication : function(component, event, helper) {
     
var fNumber = component.get('v.firstNumber');
        var sNumber = component.get('v.secondNumber');
     
        var multiplication = parseInt(fNumber) * parseInt(sNumber);
     
        component.set('v.result' , multiplication);
        component.set('v.isAdd' , false);
        component.set('v.isSub' , false);
        component.set('v.isMul' , true);
        component.set('v.isRefresh' , true);
        component.set('v.isDiv' , false);     
},
 
    doDivision : function(component, event, helper) {
     
var fNumber = component.get('v.firstNumber');
        var sNumber = component.get('v.secondNumber');
     
        var division = parseInt(fNumber) / parseInt(sNumber);
     
        component.set('v.result' , division);
        component.set('v.isAdd' , false);
        component.set('v.isSub' , false);
        component.set('v.isMul' , false);
        component.set('v.isDiv' , true);
        component.set('v.isRefresh' , true);
},
 
    doRefresh : function(component, event, helper) {
     
        component.set('v.firstNumber' , '');
        component.set('v.secondNumber' , '');
        component.set('v.isAdd' , false);
        component.set('v.isSub' , false);
        component.set('v.isMul' , false);
        component.set('v.isDiv' , false); 
        component.set('v.isRefresh' , false); 
}         
})

Output:





Other Useful Blog
Lightning Web Component


Happy Learning !!

Lightning Component Interfaces

There are some important Lightning Component Interfaces are :


1. flexipage:availableForRecordHome : This interface means that we can use this component inside the record page of any object.

2. force:hasRecordId : It gives the record id of the record inside which we have put our component.

3. force:hasSObjectName : This interface is useful for whenever we are using the same component inside the different objects.
For ex :  If we have a custom component that is useful for creating case and we are using that component inside two objects that are Account and Contact , so that using this interface we can check under which objects that particular component has been placed.

4. flexipage:availableForAllPageTypes : This interface is used for if we want to make component available for in a Lightning App Builder , in a Record Page or in a Home Page.

5. forceCommunity:availableForAllPageTypes : If we want to use component in the Lightning Communities then we have to implemented this interface.

6. force:appHostable : If we want to use our Lightning Component as a Tab , for that we have to implemented this interface.

7. force:lightningQuickAction or force:lightningQuickActionWithoutHeader : If we want to use our lightning component as a quick action , then we have to implement this interface.

The basic difference between force:lightningQuickAction and force:lightningQuickActionWithoutHeader is that if we are using force:lightningQuickAction it will give us some standard action like Cancel and Close button on the modal but this is not available in force:lightningQuickActionWithoutHeader.

8. ltng:allowGuestAccess : If we want our component to be visible in the lightning community for unauthorized user like guest user , then we have to use this interface.



Other Useful Blog
Lightning Web Component


Happy Learning !!

Lighnting Component Framework Bundle

The Lightning Component framework is a UI framework for developing single-page web apps for mobile and desktop devices.The Lightning Component framework is a UI framework for developing single-page web apps for mobile and desktop devices.

Component Bundles

A component bundle contains a component or an app and all its related resources.

1. Component : Component contains UI Parts.

2. Controller : Its a controller for the component. Its contains JavaScript to make the server call to get the data from apex and show on to the component. Its a mediator between component and server.

3. Helper : It is used for reusable purpose , its means we can create some common method inside single helper in a different different JavaScript controller inside a single application.

4. Style : It's a CSS file and we can put custom CSS in this tag.

5. Documentation : It contains some specific documentation of the application, that wanted to show to the user to use this application like these are the documents you need to read first after reading these documents you will get the complete idea about this app.

6. Renderer : If we want to override the default behaviour of the component , so we need to put the code inside the Renderer JS.

7. Design : Design basically contains attribute for the pre-requist form any particular component.

8. svg : svg  is used for creating the custom icon.



Other Useful Blog
Lightning Web Component


Happy Learning !!