-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
3Questions
-
0Replies
Need to write a test class
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 .
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 .
-
- Sanjay Wadge
- March 15, 2015
- Like
- 0
- Continue reading or reply
Need a queyr to get list of Knowledge Article Numbers associated to a case
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.
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.
-
- Sanjay Wadge
- March 05, 2015
- Like
- 0
- Continue reading or reply
SOQL for cases created or modified between last 24 hours
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
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
-
- Sanjay Wadge
- March 04, 2015
- Like
- 0
- Continue reading or reply