+ Start a Discussion
ArchieTechie6ArchieTechie6 

Salesforce Knowledge - Extension to Standard Controller

Hi All,
 
I am new to salesforce and I need help with the code which I have attempted.

I am trying to fetch some fields from Salesforce Knowledge Standard Object. 
I have created a Controller and below is my code:
public class KBController {

 public list<Knowledge__kav> KBArticles {get;set;}
 
 public KBController (ApexPages.StandardController StdCtrller){
 
 this.KBArticles = new list<Knowledge__kav>();
 
 KBArticles = [Select articleNumber, Title from Knowledge__kav where PublishStatus='Online'];  
 
 }
 }

Below is the code in my markup: 
<apex:page showHeader="false" sidebar="false" standardController="Knowledge__kav" extensions="KBController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!KBArticles}" var="articles">
                <apex:column value="{!articles.articleNumber}"/> 
                <apex:column value="{!articles.Title}"/> 
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

With this code, I am getting an error: 
Id value null is not valid for the Knowledge__kav standard controller 

Could anyone please suggest me here?

Thanks!
 
 
Best Answer chosen by ArchieTechie6
devedeve
Yes you have to pass articleNumber in url

All Answers

devedeve
Hi ArchieTechie6,

I think Code is correct but this error is due to record Id which you are fetching in controller class.

I run this sample code and I don't get any error :

Page:

<apex:page showHeader="false" sidebar="false" standardController="Student__c" extensions="studentController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!KBArticles}" var="articles">
                <apex:column value="{!articles.Age__c}"/>
                <apex:column value="{!articles.Email__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller class:

public class studentController {  public list<Student__c> KBArticles {get;set;}    public studentController (ApexPages.StandardController StdCtrller){    this.KBArticles = new list<Student__c>();    KBArticles = [Select Age__c, Email__c from Student__c WHERE AGE__c = 100];      }  }.
ArchieTechie6ArchieTechie6
Thanks for the response!
I see, you are passing a parameter in WHERE clause - WHERE AGE__c = 100

Do I need to pass any article number or any such parameter in the same way? 

Will I not be able to retrive all articles using this code? 
 
I have updated my code in controller as below:
But still getting same error.
 
public class KBController {

 public list<Knowledge__kav> KBArticles {get;set;}
 
 public KBController (ApexPages.StandardController StdCtrller){
 
 this.KBArticles = new list<Knowledge__kav>();
 
 KBArticles = [Select articleNumber, Title from Knowledge__kav where ArticleNumber='000001000'];  
 
 }
 }
ArchieTechie6ArchieTechie6
I tried the same. Here's a screenshot
 User-added image
devedeve
Hi Archana, Have you checked that there are records in Knowledge__kav object? Thanks
ArchieTechie6ArchieTechie6
Thankyou for responding for each query of mine Jyoti!

There are some records in the object. I have created at elast 8 KB Articles in this object. 
I am missing something and not really sure what.

In my URL, while browsing the page - Do I need to pass any ID?  For example, below URL

https://myorgURL/apex/MyKBPage?articleNumber=000001000

and my query will be: 

articleId = ApexPages.currentPage().getParameters().get('articleNumber');
KBArticles = [Select articleNumber, Title from Knowledge__kav where ArticleNumber = articleId ];  

I tried this as well. Getting the same error. 
devedeve
Yes you have to pass articleNumber in url
This was selected as the best answer
ArchieTechie6ArchieTechie6
Thank You so much Jyoti. 

Thats the mistake I did. I was passing wrong Id and sometimes, I was checking without passing any ID. 
As I said,  I am really new and you helped me a lot! Thanks again!