• Sanjay wadge 16
  • NEWBIE
  • 0 Points
  • Member since 2015

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

I have written following class which I want to -
1) run every 10 mintues
2) write a test method to get the 100% code coverage 
3) want to test this code before scheduling to make sure it works as expected.

Will appreciate your help as I am new to Apex.

global class UpdateKBArticleNoOnCase_sched implements Schedulable{

    global void execute(SchedulableContext SC) {
       
       //New Version 2.0
            
      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;
    
       }
       
    }
}
Hi,

I have written following class which I want to -
1) run every 10 mintues
2) write a test method to get the 100% code coverage 
3) want to test this code before scheduling to make sure it works as expected.

Will appreciate your help as I am new to Apex.

global class UpdateKBArticleNoOnCase_sched implements Schedulable{

    global void execute(SchedulableContext SC) {
       
       //New Version 2.0
            
      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;
    
       }
       
    }
}