• Sanjay Wadge
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies
Hi,

I have written following my first Apex class. Need help in writing a test class to meet the code coverage.

public without sharing class UpdateKBArticleNoOnCase {
    
    public class UpdateKBArticleException extends Exception {}
    
    public static void handleArticleNoUpdate () {

   DateTime rightNow =  DateTime.now();
      DateTime d24hAgo = rightNow.addHours(-24);
      
      // Get unique cases for which articles were added....
      
      Set<Id> setid = new Set<Id>();
    
      // for (CaseArticle CasesWithArticle : [Select CaseId from CaseArticle where CreatedDate = YESTERDAY])
      for (CaseArticle CasesWithArticle : [Select CaseId from CaseArticle where CreatedDate > :d24hAgo])
      { setid.add(CasesWithArticle.CaseId); }

      // for (Case cases : [Select Id, KB_Article__c from Case IN CasesWithArticle])
      for (Case cases : [Select Id, KB_Article__c from Case WHERE Id IN :setid])
      {
        
        String AllArticleNos;
        Integer i=0;
        
        List<String> ArticleNumbers = new List<String>();
        for (CaseArticle Articles : [Select KnowledgeArticle.ArticleNumber FROM CaseArticle 
             where CaseId = :cases.Id])
        { ArticleNumbers.add(Articles.KnowledgeArticle.ArticleNumber); 
          if (i == 0)
             AllArticleNos = Articles.KnowledgeArticle.ArticleNumber;
          else
             AllArticleNos += ', '+Articles.KnowledgeArticle.ArticleNumber;
          i++;
        }
        
        System.debug(cases.id + ' ' + AllArticleNos);
                 
        // Update Case
        
        cases.KB_Article__c = AllArticleNos;
        update cases;
    
       }
    }
}

I have seen insert test examples but did not find any update one. Please help in writing test class for the above mention code as well as steps to test the aboe code.
Thanks .
Hi,

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.
 
Hi,

I am trying to wirte a query in apex class to get the list of cases created between last 24 hours.
I tried following and nothing works. Every query gives some or the other syntax errors.

SELECT ID from Cases WHERE CreateDate = LAST 1 DAYS

SELECT ID from Cases WHERE CreateDate > NOW()-1 and CreateDate < NOW()

SELECT ID from Cases WHERE CreateDate > TODAY()-1 and CreateDate < TODAY()

I also tried - Cases c = new Cases [CreateDate = TODAY()];

Also I want to store ID, either in string array or list. (Sorry I am new to Apex).
Please suggest what I am doing wrong and possible solution.

-Sanjay