[SalesForce] Lightning event refresh the Component List

On every event I am passing the new siteID to the server side controller to get the list of the records, however I am getting the same list of records, no change. Event firing perfectly fine and also returning the correct siteId too. In the system each siteId has different set of records. Even I tried clearing the List using this "cmp.set("v.Interns", []);" but still same result.

Any suggestion?

CMP:

<aura:handler event="c:FormSubmit" action="{!c.handleApplicationEvent}"/>

    <lightning:layout horizontalAlign="space" multipleRows="true">
        <aura:if isTrue="{!v.errorMessage}">
            <div class="slds-align_absolute-center">
                No Interns found
            </div>
        </aura:if>
        <aura:iteration items="{!v.Interns}" var="int">
            <lightning:layoutItem flexibility="auto" size="12" mediumDeviceSize="6" 
                                  largeDeviceSize="3" padding="around-small">
                <c:InternshipTile intern="{!int}"/>
            </lightning:layoutItem>
        </aura:iteration>
    </lightning:layout>

JS:

({
    /* aeHandlerController.js */
    handleApplicationEvent : function(cmp, event) {
        var message = event.getParam("siteId"); 
        var action = cmp.get("c.getInterns");

        action.setParam({"siteId": message});
        action.setCallback(this, function(response){
        var status = response.getState();
            if(status === "SUCCESS"){

             if(! $A.util.isEmpty(response.getReturnValue())){                   
                    cmp.set("v.Interns",response.getReturnValue()); 
                    console.log(response.getReturnValue());
                } else {
                     cmp.set("v.recordError","No boats found");
                }
            }
        });
        $A.enqueueAction(action);
    }

})

Firing Evevnt CMP:

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:registerEvent name="siteIdEvent" type="c:FormSubmit"/>
    <aura:attribute name="State" type="String" default="All"/> <!-- Set default here because in this use case the initial value is passed into the component -->
    <aura:attribute name="siteOptions" access="public" type="String[]"/>    
    <lightning:layout horizontalAlign="center"   >        
        <lightning:layoutItem class="slds-grid_vertical-align-center" >            
            <lightning:select aura:id="typeSelect"
                              value="{!v.State}"
                              name="state_opt" 
                              label=""
                              >
                <option value="">...Select a Site...</option>
                <aura:iteration items="{!v.siteOptions}" var="st">
                    <option value="{!st.label}" selected="{!st==v.state}">{!st.value}</option>
                </aura:iteration>
            </lightning:select>                    
        </lightning:layoutItem>
        <lightning:layoutItem class="slds-grid_vertical-align-center" > 
            <lightning:button label="Search" variant="brand" onclick='{!c.searchInt}' />

        </lightning:layoutItem>

    </lightning:layout>

JS Controller:

searchInt : function(cmp, event) {

        console.log(cmp.find("typeSelect").get("v.value"));
        var appEvent = $A.get("e.c:FormSubmit");
        appEvent.setParams({
            "siteId" : cmp.find("typeSelect").get("v.value") 
             });
        appEvent.fire();
    }

Apex Class:

@AuraEnabled
    public static list<Apprenticeship__c> getInterns(String siteId ) {
        List<Apprenticeship__c> interns=new List<Apprenticeship__c>();
        if(siteId != ''){
            interns = [SELECT Id,Name,Student_Contact__r.Picture__c,Student_Contact__r.FirstName,Student_Contact__r.LastName,Student__r.Name FROM Apprenticeship__c WHERE Site_Location__c=:siteId  LIMIT 5];
        }else{
            interns = [SELECT Id,Name,Student_Contact__r.Picture__c,Student_Contact__r.FirstName,Student_Contact__r.LastName,Student__r.Name  FROM Apprenticeship__c LIMIT 30];
        }


        return interns;
    }

Screenshot:
enter image description here

Best Answer

It looks like you are not setting the server side controller parameter correctly. This is why you see the value in your JS controller, but it is showing null in your apex controller.

Change this:

action.setParam({"siteId": message});

To this (syntax for array of params):

action.setParams({"siteId": message});

Looks like you are just missing the s at the end of setParams()

I believe you can also set a single param using this syntax:

action.setParam("siteId", message);
Related Topic