Hi Friends,
I am trying to display opportunity record in the list based on the user entered input. below is my code. Kindly help me where i did wrong.
1) MyOpportunityListComponent.cmp : ********************************* <aura:component controller ="MyOpportunityListController" implements="flexipage:availableForAllPageTypes"> <!-- Declaring client-side attributes to be used in component --> <aura:attribute name="lstOpportunity" type="Opportunity[]" /> <aura:attribute name="columnsToDisplay" type="List" /> <aura:attribute name= "searchKeyword" type="String"/> <!-- Display message on the page--> <aura:attribute name= "newMessage" type="String" Default="Hello there!"/> <!-- Method to be called on component initialization--> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <h2>{!v.newMessage}</h2> <lightning:card iconName="standard:opportunity" title="Opportunity List"> <!-- Search box--> <lightning:layout> <lightning:layoutItem size="3" padding="around-small"> <lightning:input aura:id="searchField" label="Opportunity Name" value="{!v.searchKeyword}" placeholder="Search Opportunities" onchange="{!c.searchOpportunities}"/> </lightning:layoutItem> </lightning:layout> <!-- Opportunity list goes here--> <lightning:datatable data="{!v.lstOpportunity}" columns="{!v.columnsToDisplay}" KeyField="Id" hideCheckboxColumn="true" /> </lightning:card> </aura:component> 2) MyOpportunityListComponentController.js : *************************************** ({ //Method to be called on component initialization doInit: function(component, event, helper){ helper.fetchOppHelper(null, component); }, // Method to perform search on Opportunities searchOpportunities: function(component, event, helper){ var searchValue = component.find("searchField").get("v.value"); console.log("User entered - ",searchValue); helper.fetchOppHelper(searchValue, component); } }) 3) MyOpportunityListComponentHelper.js : *********************************** ({ //Helper method to fetch opportunity information from controller fetchOppHelper: function(searchVal, component){ component.set("v.columnsToDisplay",[ { label: "Opportunity Name", fieldName: "Name", type: "text"}, { label: "Account Name", fieldName: "Account_Name__c", type: "text" }, { label: "Close Date", fieldName: "CloseDate", type: "date"}, { label: "Amount", fieldName:"Amount", type: "currency",cellAttribute: {alignment:"Right"}} ]); // Creating the action var action = component.get("c.fetchOpportunity"); action.setParams({"searchKeyWord" : searchVal }); // If we pass more parameter by using comma i.e. "param2":searVal,"param3" :amtVal etc.. // Calling server side Method action.setCallback(this, function(response){ var state = response.getState(); if (state === "SUCCESS"){ debugger; component.set("v.lstOpportunity", response.getReturnValue()); }else{ alert("An error occurred while fetching the data"); } }); $A.enqueueAction(action); } }) 4) MyOpportunityListController.apxc : ********************************* public class MyOpportunityListController{ // Get the list of opportunities based on search value @AuraEnabled public static List<Opportunity> fetchOpportunity(String searchKeyWord){ List<Opportunity> returnList = new List<Opportunity>(); // If search value is null then return all opps.Else search accorgingly String searchKey = String.isBlank(searchKeyWord) ? '%%' : searchKeyword + '%'; returnList = [SELECT Id, Name, Account_Name__c,CloseDate,Amount FROM Opportunity WHERE Name Like :searchKey LIMIT 15]; return returnList; } } 5) MyAuraApplication.app : ********************** <aura:application extends="force:slds"> <!-- Include MyOpportunityComponent component inside application. Here newMessage is passing parameter Parent to Child. This newMessage is optional if required pass otherwise no need newMessage--> <c:MyOpportunityListComponent newMessage="This is a new message"/> </aura:application>
Thank you very much
Regards,
Srinivas
- Have you given apex class access to profile?
Srinivas : I already enabled the Apex Classes. My profile is system administrator
- Do you have Opp and field accesss?
Srinivas : I have opp and field access. because my profile system administrator
Thanks for your reply
Regards,
Srinivas