3

I have this lightning component that successfully gets knowledge articles from KnowledgeArticleVersion and I have verified the correct data coming through dev console logs... However, I am having some trouble using aura atrributes. I am unable to get the component to actually render the data. I am guessing my issue has something to do with trying to return list data to SObject[] type, thus not allowing {!article.Id} etc to render any data in the component.

Any ideas?

.cmp

<aura:component implements="forceCommunity:availableForAllPageTypes" controller="GetHBArticles">
    <aura:attribute name="articles" type="SObject[]"/>

    <ui:button label="Get Articles" press="{!c.getArts}"/>
    <aura:iteration var="articles" items="{!v.articles}">
        <p>{!articles.Id} : {!articles.Title} : {!articles.ArticleType}</p>
    </aura:iteration>

</aura:component>

lightning controller

({
    getArts: function(cmp){
        var action = cmp.get("c.getArticlesList");
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set("v.articles", response.getReturnValue());
            }
        });
     $A.enqueueAction(action);
    }
})

apex controller

public with sharing class GetHBArticles {

    @AuraEnabled
    public static List<List<SObject>> getArticlesList(){
        List<List<SObject>> articles = [FIND :searchVar RETURNING KnowledgeArticleVersion
                                                     (Id, Title, ArticleType WHERE PublishStatus='online' AND Language = 'en_US' AND ArticleType IN ('Troubleshooting__kav', 'How_To__kav', 'FAQ__kav'))
                                                     WITH DATA CATEGORY Topics__c AT 'DataCategoryName];
        return articles;
    }



}

As always any help from this awesome group is greatly appreciated.

  • Seems like you're missing a helper class, but that's just with taking a cursory look. I'm confident that someone who writes more Lightning will come along and give you more direction. – crmprogdev Apr 6 '16 at 15:28
  • I was unaware that the helper class is necessary to load the data. Thanks for the tip I will read into it more. This is the example I was referencing developer.salesforce.com/docs/atlas.en-us.lightning.meta/… – Allen Mann Apr 6 '16 at 15:30
6

@AllenMann you could fix this in two ways:

1.Return List<KnowledgeArticleVersion> instead of List<List<SObject>> to do so you have to change getArticlesList method as below:

 @AuraEnabled
 public static List<KnowledgeArticleVersion> getArticlesList(){
    List<KnowledgeArticleVersion> articles = [FIND :searchVar RETURNING KnowledgeArticleVersion
                                                 (Id, Title, ArticleType WHERE PublishStatus='online' AND Language = 'en_US' AND ArticleType IN ('Troubleshooting__kav', 'How_To__kav', 'FAQ__kav'))
                                                 WITH DATA CATEGORY Topics__c AT 'DataCategoryName'][0];
    return articles;
 }

2.Use the first element(i.e index = 0) of the response which is an array containing an array of KnowledgeArticleVersion data in lightning controller

  var result = response.getReturnValue();
  if(result.length && result[0].length)
     cmp.set("v.articles",result[0]);
| improve this answer | |
  • Wasn't able to get the first one working right. Would the aura attribute type need to be changed also? I tried to change it to KnowledgeArticleVersion[] with no luck. Trying your second suggestion now. Thanks for your time @Praveen. – Allen Mann Apr 6 '16 at 15:55
  • @AllenMann aura:attribute's type is fine no need to change it. – Praveen Apr 6 '16 at 15:56
  • @AllenMann Happy to help :-) – Praveen Apr 6 '16 at 16:04
  • In the lightning controller you did if{result.length && result[0].length before sending the data to the component. Is the length variable assumed to have been created and given an integer used for limiting results? – Allen Mann Apr 12 '16 at 13:31
  • Since List<T> or even nested list (List<List<T>>) ( i.e your case) in Apex(server side) is treated as Javascript Array in Lightning component(client side) which has length property by default and whose values is the size of the JS Array. The IF condition checks if the response has result,if it had data in it ,then the result.length would have value greater than 0 inturn the If block would have executed(in your setting the aura:attribute value) T - Any Apex Supported Data Type – Praveen Apr 14 '16 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.