[SalesForce] Use aura:attribute to filter Sobject records for lightning component

I am building a lightning component that is supposed to show a record based on the user's language. I've gotten this part to work but I haven't been able to figure out how to compare a component attribute to a value of a Sobject record. If I set the component Property Editor value for Subject to "value1", then it should only show DynamicContent__c records where field Subject__c = "value1". How do I achieve this?

Apex Controller:

public class DynamicContentController {

public String currentLanguage {get; set;}

public DynamicContentController() {
    // get user’s language
    currentLanguage = UserInfo.getLanguage();
}

@AuraEnabled
public static List<DynamicContent__c> getDynamicContent(User communityUser) {

    // get user’s language
    String currentLanguage = UserInfo.getLanguage();

    // return content matching user's language
    return [SELECT Id, Content__c, Language__c, Subject__c FROM DynamicContent__c WHERE Language__c = :currentLanguage];
}

}

Lightning Component:

<aura:component controller="DynamicContentController" implements="forceCommunity:availableForAllPageTypes">   

<aura:attribute name="section" type="String" access="global" />
<aura:attribute name="subject" type="String" access="global" />
<aura:attribute name="contents" type="DynamicContent__c[]" />
<aura:handler name="init" value="{!this}" action="{!c.getContents}"/>   

<div class="container">
    <aura:iteration items="{!v.DynamicContent__c}" var="c">
        <p>{!c.Content__c}</p>
    </aura:iteration>
</div> 

</aura:component>

Lightning Component Controller:

({
getContents : function(component, event, helper) {        
    var action = component.get("c.getDynamicContent");
    action.setCallback(this, function(data) {
        component.set("v.DynamicContent__c", data.getReturnValue());
    });
    $A.enqueueAction(action);                
}
})

Lightning Component Design:

<design:component>
    <design:attribute name="section" label="Section" description="Section value" />
    <design:attribute name="subject" label="Subject" datasource="value1,value2,value3" default="value1" />
</design:component>

Best Answer

The value that User chooses in the design file will be available as an attribute .You will pass only that value which user chooses .

Here is how your controller.js will look like

({
getContents : function(component, event, helper) { 
   var selectedSubject = component.get("v.subject"); //Observe how i get value out of the attribute which is bound to design file     
   var action = component.get("c.getDynamicContent");
   action.setParams({
            "selectedSubject": selectedSubject 
        });
   action.setCallback(this, function(data) {
    component.set("v.DynamicContent__c", data.getReturnValue());
  });
   $A.enqueueAction(action);                
  }
})

Your server code will have parameter name same as one which is defined in your action

@AuraEnabled
public static List<DynamicContent__c> getDynamicContent(String selectedSubject){

// get user’s language
String currentLanguage = UserInfo.getLanguage();

// return content matching user's language
return [SELECT Id, Content__c, Language__c, Subject__c FROM    DynamicContent__c WHERE Language__c = :currentLanguage and Subject__c =:selectedSubject];
 }
Related Topic