Newer Version Available

This content describes an older version of this product. View Latest

Salesforce Knowledge with SOQL and SOSL

Use the Salesforce Object Query Language (SOQL) to search your organization’s Knowledge data for specific information. SOQL is similar to the SELECT statement in the widely used Structured Query Language (SQL) but is designed specifically for Salesforce. Use the Salesforce Object Search Language (SOSL) to construct text-based search queries against the search index.
Knowledge Object Limits
KnowledgeArticleVersion
  • Always filter on a single value of PublishStatus unless the query filters on one or more primary key IDs. To support security, only users with the “Manage Articles” permission see articles whose PublishStatus value is Draft.
  • Archived article versions are stored in the articletype_kav object. To query archived article versions, specify the article Id and set IsLatestVersion='0'.
  • Always filter on a single value of Language. However, in SOQL, you can filter on more than one Language if there is a filter on Id or KnowledgeArticleId.
  • You cannot use binding variables in Apex SOQL statements with KnowledgeArticleVersion objects. For example, the following SOQL statement causes a compilation error.
    1final String PUBLISH_STATUS_ONLINE = 'Online';
    2List<Knowledge__kav> articles = [ 
    3SELECT Id FROM Knowledge__kav 
    4WHERE PublishStatus = :PUBLISH_STATUS_ONLINE 
    5];

    Instead, use dynamic SOQL as follows. See Dynamic SOQL in Apex Developer Guide.

    1final String PUBLISH_STATUS_ONLINE = 'Online'; 
    2final String q = 'SELECT Id, PublishStatus FROM Knowledge__kav 
    3WHERE PublishStatus = :PUBLISH_STATUS_ONLINE';
    4List<Knowledge__kav> articles = Database.query(q);

For generic SOQL and SOSL information see the SOQL and SOSL Reference.