0

Here is my controller:

public with sharing class PKB_DataForScraping_MobileController extends PKB_ComponentControllerBase{
    public Id artId {get;set
        {
            artId = value;
        }
    }
    public String artTitle {get;set;}
    public String artSummary {get;set;}
    public FAQ__kav faqAnswer {
        get {
            List<FAQ__kav> faq;
            try {
                faq = [select Answer__c from FAQ__kav where PublishStatus = 'Online' and KnowledgeArticleId = :artId limit 1];
            } catch (Exception e) {}
            return faq[0];
        }        
    }
}

Here is my test:

@isTest
public class PKB_Tests_Mobile {
    public static Id faqId = 'kA0C0000000CgQtKAK';
    static testMethod void retriveFaqAnswer() {
        System.debug('Retriving FAQ answer: ');
        PKB_DataForScraping_MobileController dsm = new PKB_DataForScraping_MobileController(); 
        dsm.artId = faqId;
        FAQ__kav faq = dsm.faqAnswer;
        System.debug('answer: ' + faq.Answer__c);
    }
}

The test runs successfully and produces a query result. But when I use the same controller code in Visualforce component provided below, I get an error page.

Visualforce component:

<apex:component controller="PKB_DataForScraping_MobileController">
<site:googleAnalyticsTracking />
    <apex:attribute name="articleId" description="Article Id" type="Id" assignTo="{!artId}" required="true"/>
    <apex:attribute name="key" type="String" assignTo="{!Key}" default="articleView" description="The key given to this component so the page can easily get access to it" />
    <apex:attribute name="pageController" type="PKB_PageControllerBase" assignTo="{!pageController}" description="The controller for the page." />
    {!faqAnswer.Answer__c}
</apex:component>

The component is used in a page that passes the artId. This error is killing me.. please help. Controller can be tested using a test class, but how do I test the component? I don't even know what is causing the error is the component.

Thanks.

4
  • What error are you getting? Also why do you have a static Id in the test? (Generally bad practise).
    – Francis
    Dec 14 '14 at 19:20
  • Francis: The static id is just to check if my controller code is working. I get Authorization Required (401) when I try this from Visualforce
    – XurajB
    Dec 14 '14 at 19:27
  • 1
    See How do I start to debug my own Apex code? for how to add debug output so you can figure this out yourself. (Instead of return faq[0] which will throw an exception, the best you can do is return null. Or better still remove the try/catch.)
    – Keith C
    Dec 14 '14 at 19:34
  • Keith: Thank you for the suggestion. I was not getting debug info because I was using my name.. I had to put the site name to get debug from the site user.. Ultimately found that I was using too many SOQL query in the page
    – XurajB
    Dec 14 '14 at 20:14
1

I learned the hard way. I was using too many SOQL query in my page (max of 100), this prevented running another SOQL in my component that caused Unauthorized access. Debug logs helped me find this issue. To get debug log for the site, I used the site name under Monitored Users instead of my name to get the guest user. You can also find the guest user from here: Setup > Develop > Sites > [click on site label] > Public Access Settings (button) > Assigned Users. I hope this will be helpful to someone going through the same issue.

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.