• Mangadivya kasani
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Here is my code:
BoatSearchForm.cmp:

<aura:component controller="BoatSearchForm" implements="force:appHostable,flexipage:availableForAllPageTypes" >   
    
    <aura:registerEvent name="launchNewBoatForm" type="c:launchNewBoatForm"/>

    <!-- Handle component init in a client-side controller -->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:handler name="launchNewBoatForm" event="c:launchNewBoatForm" action="{!c.handleNewBoatForm}"/>
    <aura:handler name="formsubmit"
                  event="c:FormSubmit"
                  action="{!c.onFormSubmit}"
                  phase="capture"/>
    
    <!-- dynamically load the BoatTypes -->
    <aura:attribute name="BoatTypes" type="BoatType__c[]" />
    <aura:attribute name="selectedType" type="String" default="foo"/>
    <aura:attribute name="renderNew" type="Boolean" default="true"/>
    
    <lightning:layout horizontalAlign="center" verticalAlign="end">        
        
        <lightning:layoutItem padding="horizontal-medium" >
            <lightning:select aura:id="boatTypes" label="" name="selectType" onchange="{!c.handleChange}">
                <option value="">All Types</option>
                <aura:iteration items="{!v.BoatTypes}" var="boatType">
                    <option value="{!boatType.Id}" text="{!boatType.Name}"/>
                </aura:iteration>
            </lightning:select> 
        </lightning:layoutItem>
        <div class="slds-button-group" role="group">
            <lightning:button class="slds-button" variant="brand" label="Search" onclick="{!c.search}"/>        
                <aura:if isTrue="{!v.renderNew}">
                    <lightning:button class="slds-button" variant="neutral" label="New" onclick="{!c.createBoat}"/>
                </aura:if>
        </div> 
    </lightning:layout>
</aura:component>

BoatSearchFormCOntroller.js:
({
    // Load boat types from Salesforce
    doInit: function(component, event, helper) {
         helper.loadBoatTypes(component);
    },
    handleChange : function(component, event, helper){
        console.log(component.find("boatTypes").get("v.value"));
        component.set("v.selectedType", component.find("boatTypes").get("v.value"));
    },

    search : function(component, event, helper){
        var selectedType = component.get("v.selectedType");
        console.log("Search button pressed " + selectedType)
    },

    createBoat : function(component, event, helper){
        var boatTypeId = component.get("v.selectedType");
        console.log("New button pressed " + boatTypeId);
        var requestNewBoat = component.getEvent("launchNewBoatForm");
        requestNewBoat.setParams({"boatTypeId": boatTypeId});
        requestNewBoat.fire();
    },

    handleNewBoatForm: function(component, event, helper){
        console.log("handleNewBoatForm handler called.")
        var boatTypeId = component.get("v.selectedType");

        console.log(boatTypeId);
        var createNewBoat = $A.get("e.force:createRecord");
        createNewBoat.setParams({
            "entityApiName": "Boat__c",
        })
        if(! boatTypeId==""){
            console.log('=====');
            createNewBoat.setParams({
                "defaultFieldValues": {'BoatType__c': boatTypeId}
           })
        }
        createNewBoat.fire();
    },
    onFormSubmit : function(component, event, helper){
        var boatTypeId = component.get("v.selectedType");
        console.log("Search button pressed " + boatTypeId);
        var formsubmit = component.getEvent("FormSubmit");
        formsubmit.setParams({"formData":
                            {"boatTypeId" : boatTypeId}
        });
        formsubmit.fire();
    },
})

BoatSearchResults.cmp:

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes" access="global" controller="BoatSearchResults">
    <aura:handler name="init" action="{!c.doSearch}" value="{!this}"/>
    <aura:handler name="BoatSelect" event="c:BoatSelect" action="{!c.onBoatSelect}"/>

    <aura:attribute name="boatTypeId" type="String" />
    <!--boats attribute stores the search results-->
    <aura:attribute name="boats" type="Boat__c[]" /> 
    <aura:attribute name="selectedBoatId" type="Id" />
    <aura:method name="search" description="Sample method with parameter">
        <aura:attribute name="boatTypeId" type="String"  />
    </aura:method>
    <aura:if isTrue="{!not(empty(v.boats))}">
        <lightning:layout multipleRows="true" horizontalAlign="center">
                <aura:iteration items="{!v.boats}" var="boatVar">
                    <lightning:layoutItem flexibility="grow"  class="slds-m-right_small" >   
                        <c:BoatTile boat="{!boatVar}"/>
                    </lightning:layoutItem>
                </aura:iteration>
        </lightning:layout>
         <aura:set attribute="else">
            <div class="slds-align_absolute-center">No boats found</div>
        </aura:set>
    </aura:if>
    
    <aura:set attribute="else">
            <lightning:layout horizontalAlign="spread" multipleRows="true">
                <aura:iteration var="boat" items="{!v.boats}">
                    <lightning:layoutitem >
                        <!--<c:BoatTile boat="{!boat}" selected="{! boat.Id == <b>v.</b>selectedBoatId ? true : false }"/>-->
                        <c:BoatTile boat="{!boat}" selected="{!boat.Id == v.selectedBoatId ? true : false }"  />
                    </lightning:layoutitem>
                </aura:iteration>
            </lightning:layout>
    </aura:set>
    
</aura:component>

BoatSearchResultsController.js:

({
    doSearch : function(component, event, helper) {
        component.get("v.boatTypeId");
        console.log("boatTypeId====: " + component.get("v.boatTypeId"));
        helper.onSearch(component, event, helper);
    },
    
    search: function(component, event, helper){
        var params = event.getParam('arguments');
        console.log("boatTypeId extracted: " + params.boatTypeId);
        component.set("v.boatTypeId", params.boatTypeId);
        helper.onSearch(component);
        return "search complete.";
    },
    onBoatSelect : function(component, event, helper) {
        var boatId = event.getParam("boatId");
        console.log(boatId);
        component.set("v.selectedBoatId",boatId);
    }
})

BoatSearchResultsHelper.js:

({
    onSearch : function(component, event, helper) {
        var boatTypId = component.get("v.boatTypeId");
        console.log("boatTypId----> " + boatTypId);
        // create an action
        var action = component.get("c.getBoats");
        action.setParams({boatTypeId:boatTypId});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state == "SUCCESS") {
                console.log("===response.getReturnValue() " + response.getReturnValue());
                console.log(component.set("v.boats", response.getReturnValue()));                
                component.set("v.boats", response.getReturnValue());
            } else {
                console.log("Failed with state: " + state);
            }
        });
        // Send action off to be executed
        $A.enqueueAction(action);
    },
    
})

BoatSearch.cmp:

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes" access="global">
    <aura:handler name="formsubmit"
                  event="c:FormSubmit"
                  action="{!c.onFormSubmit}"
                  phase="capture"/>
    <lightning:card title="Find a Boat" class="slds-m-bottom_10px">
        <c:BoatSearchForm />
    </lightning:card>
    
    <lightning:card title="Matching Boats">        
        <c:BoatSearchResults aura:id="BSRcmp"/>
    </lightning:card>
    
</aura:component>

BoatSearchControlle.js:
({
    onFormSubmit : function(component, event, helper){
        console.log("event received by BoatSearchController.js");
        var formData = event.getParam("formData");
        var boatTypeId = formData.boatTypeId;
        console.log("event received by BoatSearchController.js"+ boatTypeId);
        var BSRcmp = component.find("BSRcmp");
        var auraMethodResult = BSRcmp.search(boatTypeId);
        console.log("auraMethodResult: " + auraMethodResult);
    }
})

In debugs BoatTypeId is null in BoatsearchResults. Please help me on this