function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sanjay WadgeSanjay Wadge 

Need a queyr to get list of Knowledge Article Numbers associated to a case


I am trying to write a apex code to get the list of Knowledge Articles associated to a cases. I am particularly interested in ArticleNumber from Knowedge Articles. We have CaseArticle object (middle object in M2M relationship) which stores both, CaseId and KnowledgeArticleId. Using KnowledgeArticleId, I want to get Aritcle Numbers stored in KnowledgeArticle. Struggle I am having is wrining joined query. I can query one by one object starting with Case Id, then getting KnowledgeArticleId and then getting ArticleNumber. But this is not efficient way of doing as I need to loop through multiple cases (say for cases created in last one month). Also, not sure what is relationship names as API doc does not give the details as well as these objects are not visible in salesforce setup (except case object). Please advise.
Himanshu ParasharHimanshu Parashar
Hi Sanjay,

There is one object call KnowledgeArticleVersion which contains one field called sourceid(case or reply). I think you can query that object as shown below to get your data.
set<id> caseids= new set<id>();
for(Case objcase : [select id from case limit 10])

List<KnowledgeArticleVersion> lstArticlesVersions = [select id,ArticleNumber,sourceid from KnowledgeArticleVersion where Language='en_US' and PublishStatus='online' and sourceid in : 

Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Is it possible to get a list of cases associated to all the knowledge article ids from your script you posted? and then display them on the case view.Objective is to display all similar cases that has same knowledge article associated to the case.  This will help number of cases that are being generated for a specific issue and how is it getting reslved using a specific KB article