Monday, June 25, 2018

How To Display Records Data Based On Enter Value Using Lightning Component and SOSL Query(Account, Contact, Opportunity)

In This Example, we Used Account, Contact, Opportunity and filter the Data based on Account Name, we display the contact, Opportunity Data based Account Name.



Apex Class
public class AdvancedLtngWrks {  
   @AuraEnabled  
   Public static WraperForStorage getSearchRelated(String searchName){  
     WraperForStorage wrperObj = new WraperForStorage();  
     if(searchName != null && searchName != ' ') {       
       // builiding the SOSL Query  
       String searchQuery = 'FIND \'' + searchName + '\' RETURNING Account(Id,Name,Type), Contact(Id,Name,Account.Name where Account.Name =:searchName),Opportunity(Id,Name,Account.Name where Account.Name =:searchName)';  
       list<list<Sobject>> totalResults = search.query(searchQuery);  
       Account[] accList = (Account[])totalResults[0];  
       Contact[] conList = (Contact[])totalResults[1];  
       Opportunity[] oppList = (Opportunity[])totalResults[2];  
       wrperObj.acntsLst = accList;  
       wrperObj.cntsLst = conList;  
       wrperObj.OpportunitysLst = oppList;        
     }   
     return wrperObj;   
   }  
   Public class WraperForStorage{  
     @AuraEnabled Public List<Account> acntsLst {get;set;}  
     @AuraEnabled Public List<Contact> cntsLst {get;set;}  
     @AuraEnabled Public List<Opportunity> OpportunitysLst {get;set;}  
     Public WraperForStorage(){}    
   }    
 }
Lightning Component
<aura:component controller="AdvancedLtngWrks" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="searchName"  type="String" />
    <aura:attribute name="acntsList" type="Account[]" />
    <aura:attribute name="contsList" type="Contact[]" />
    <aura:attribute name="OpportunityList" type="Opportunity[]" />
    <aura:attribute name="wrapperList" type="object"/>
    <div class="slds-theme_default" >
        <div class="slds-card slds-p-around--large">
            <lightning:input type="search" label="Please Enter Account Name" name="search"  value="{!v.searchName}" required="true" placeholder="SearchInAll" onkeyup="{!c.searchResult}"/>
        </div>
        <aura:if isTrue="{!v.wrapperList.acntsLst.length > 0}">
            <div class="slds-card">
                <div class="slds-text-heading_medium">
                    <strong>Search Result Based On Acount Name</strong>
                </div>
                <lightning:accordion >
                    <lightning:accordionSection name="Accounts" label="Accounts">
                        <aura:set attribute="body">
                            <table class="slds-table slds-table--bordered slds-table--cell-buffer">
                                <thead>
                                    <tr class="slds-text-title--caps">
                                        <th scope="col">
                                            <div class="slds-truncate" title="Id">Id</div>
                                        </th>
                                        <th scope="col">
                                            <div class="slds-truncate" title="Name">Name</div>
                                        </th>
                                        <th scope="col">
                                            <div class="slds-truncate" title="Type">Type</div>
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <aura:iteration items="{!v.wrapperList.acntsLst}" var="con">
                                        <tr>
                                            <th scope="row">
                                                <div class="slds-truncate" title="{#con.Id}">{#con.Id}</div>
                                            </th>
                                            <th scope="row">
                                                <div class="slds-truncate" title="{#con.Name}">{#con.Name}</div>
                                            </th>
                                            <th scope="row">
                                                <div class="slds-truncate" title="{#con.Type}">{#con.Type}</div>
                                            </th>
                                        </tr>
                                    </aura:iteration>
                                </tbody>
                            </table>
                        </aura:set>
                    </lightning:accordionSection>
                    <!-- second section -->
                    <lightning:accordionSection name="Contacts" label="Contacts">
                        <aura:set attribute="body">
                            <table class="slds-table slds-table--bordered slds-table--cell-buffer">
                                <thead>
                                    <tr class="slds-text-title--caps">
                                        <th scope="col">
                                            <div class="slds-truncate" title="Id">Id</div>
                                        </th>
                                        <th scope="col">
                                            <div class="slds-truncate" title="Name">Name</div>
                                        </th>
                                        <th scope="col">
                                            <div class="slds-truncate" title="AccountName">AccountName</div>
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <aura:iteration items="{!v.wrapperList.cntsLst}" var="con">
                                        <tr>
                                            <th scope="row">
                                                <div class="slds-truncate" title="{#con.Id}">{#con.Id}</div>
                                            </th>
                                            <th scope="row">
                                                <div class="slds-truncate" title="{#con.Name}">{#con.Name}</div>
                                            </th>
                                            <th scope="row">
                                                <div class="slds-truncate" title="{#con.Account.Name}">{#con.Account.Name}</div>
                                            </th>
                                        </tr>
                                    </aura:iteration>
                                </tbody>
                            </table>
                        </aura:set>
                    </lightning:accordionSection>
                    <!-- 3rd section -->
                    <lightning:accordionSection name="Opportunitys" label="Opportunitys">
                        <aura:set attribute="body">
                            <table class="slds-table slds-table--bordered slds-table--cell-buffer">
                                <thead>
                                    <tr class="slds-text-title--caps">
                                        <th scope="col">
                                            <div class="slds-truncate" title="Id">Id</div>
                                        </th>
                                        <th scope="col">
                                            <div class="slds-truncate" title="Name">Name</div>
                                        </th>
                                        <th scope="col">
                                            <div class="slds-truncate" title="AccountName">AccountName</div>
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <aura:iteration items="{!v.wrapperList.OpportunitysLst}" var="con">
                                        <tr>
                                            <th scope="row">
                                                <div class="slds-truncate" title="{#con.Id}">{#con.Id}</div>
                                            </th>
                                            <th scope="row">
                                                <div class="slds-truncate" title="{#con.Name}">{#con.Name}</div>
                                            </th>
                                            <th scope="row">
                                                <div class="slds-truncate" title="{#con.Account.Name}">{#con.Account.Name}</div>
                                            </th>
                                        </tr>
                                    </aura:iteration>
                                </tbody>
                            </table>
                        </aura:set>
                    </lightning:accordionSection>
                </lightning:accordion>
            </div>
        </aura:if>
    </div>
</aura:component>
JavaScript Controller
({
    searchResult: function(component, event, helper) {
        var action = component.get("c.getSearchRelated");
        action.setParams({
            "searchName": component.get("v.searchName")
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var res = response.getReturnValue();
                component.set("v.wrapperList", res);
            }
        });
        $A.enqueueAction(action);
    }
})
Please Let Us Know If You have any Queries.

Happy Learning!!!!

No comments:

Post a Comment