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
SIVA KUMAR 507SIVA KUMAR 507 

Hey All I need Test Class for the below code please help me guys.

Apex Class:
______________
global class AskEDD_ArticleDataCategories implements Schedulable {
    global void execute(SchedulableContext ctx) {
        List<Knowledge__DataCategorySelection> dataCategories = [SELECT Id, DataCategoryGroupName, DataCategoryName, ParentId 
                                                                 FROM     Knowledge__DataCategorySelection 
                                                                 WHERE     SystemModstamp >= LAST_N_DAYS:1];
        if(dataCategories.Size() > 0) {
            List<String> parentIds = new List<String>();
            for(Knowledge__DataCategorySelection dataCategory : dataCategories) {
                parentIds.add(dataCategory.ParentId);
            }
            dataCategories = [SELECT     Id, DataCategoryGroupName, DataCategoryName, ParentId 
                              FROM         Knowledge__DataCategorySelection 
                              WHERE     ParentId IN: parentIds];            
            
            Map<String, String> parentIdMap = new Map<String, String>();
            for(Knowledge__DataCategorySelection dataCategory : dataCategories) {
                if(parentIdMap.get(dataCategory.ParentId) == null) {
                    parentIdMap.put(dataCategory.ParentId, dataCategory.DataCategoryName);
                }
                else {
                    String temp = parentIdMap.get(dataCategory.ParentId) + ';' + dataCategory.DataCategoryName;
                    parentIdMap.put(dataCategory.ParentId, temp);
                }
            }
            
            List<Knowledge__kav> articles = [SELECT Id, AskEDD_Category__c FROM Knowledge__kav WHERE Id IN: parentIdMap.keySet() AND PublishStatus = 'Draft'];
            for(Knowledge__kav article : articles) {
                article.AskEDD_Category__c = parentIdMap.get(article.Id);
            }
            
            update articles;
        }
    }
}
Suraj Tripathi 47Suraj Tripathi 47

Hi Siva,

I have to continue your test class for this I need to know who is the parent of this "Knowledge__DataCategorySelection"

Name the Object which is the parent of "Knowledge__DataCategorySelection"?

@isTest
Public Class AskEDD_ArticleDataCategoriesTest{

public static testMethod void testschedule() {
 
Knowledge__DataCategorySelection  obj=new Knowledge__DataCategorySelection();
obj.SystemModstamp=Datetime.now();
obj.DataCategoryGroupName='Test';
obj.DataCategoryName='Data';
obj.ParentId=
insert obj;
}

}
SIVA KUMAR 507SIVA KUMAR 507
Hi Suraj,
"Knowledge__DataCategorySelection" with this name in my org there is no object suraj.
How Can I check That? Can You Please tell me?
Suraj Tripathi 47Suraj Tripathi 47

Actually, this is not a standard object so I also don't know about it

let me research on it.

I will get back to you

SIVA KUMAR 507SIVA KUMAR 507
Ok Suraj,
 
Suraj Tripathi 47Suraj Tripathi 47

Try this below test class and please share a Screenshot of the Test Coverage

@isTest
Public Class AskEDD_ArticleDataCategoriesTest{


public static testMethod void testschedule() {

FAQ__kav newFAQ = new FAQ__kav();
	newFAQ.Title = 'test1';
	newFAQ.UrlName = 'test1';
	insert newFAQ;
	
	Knowledge__DataCategorySelection  obj=new Knowledge__DataCategorySelection();
	obj.SystemModstamp=Datetime.now();
obj.DataCategoryGroupName='Test';
obj.DataCategoryName='Data';
obj.ParentId=newFAQ.id;
insert obj;
	
	
	Knowledge__DataCategorySelection  obj2=new Knowledge__DataCategorySelection();
obj2.SystemModstamp=Datetime.now();
obj2.DataCategoryGroupName='Test2';
obj2.DataCategoryName='Data2';
obj2.ParentId=obj.ParentId;
insert obj2;
 
 Test.StartTest();
AskEDD_ArticleDataCategories  sh1 = new AskEDD_ArticleDataCategories ();

String sch = '0 0 23 * * ?'; system.schedule('Test Data', sch, sh1); 
Test.stopTest(); }
}

}
 

 

SIVA KUMAR 507SIVA KUMAR 507
Hi Suraj,
when i save this code i am getting below errors.
User-added image
Suraj Tripathi 47Suraj Tripathi 47

for FlowDefination I thought it was object in your org and if all the field is not writable then how ould i write test class

No idea about it.

Suraj Tripathi 47Suraj Tripathi 47

 Please replace the below code and share screenshot

@isTest
Public Class AskEDD_ArticleDataCategoriesTest{


public static testMethod void testschedule() {
 
Knowledge__DataCategorySelection  obj=new Knowledge__DataCategorySelection();
obj.DataCategoryGroupName='Test';
obj.DataCategoryName='Data';
insert obj;

 Test.StartTest();
AskEDD_ArticleDataCategories  sh1 = new AskEDD_ArticleDataCategories ();

String sch = '0 0 23 * * ?'; system.schedule('Test Territory Check', sch, sh1); 
Test.stopTest(); }

}

}
SIVA KUMAR 507SIVA KUMAR 507
when i save it.below errors are populated.
User-added image
Suraj Tripathi 47Suraj Tripathi 47

ok good

this eroor is showing from another class 

go to flowcontroller_test class and comment all the code

Suraj Tripathi 47Suraj Tripathi 47
now run test in this class  "AskEDD_ArticleDataCategoriesTest"and show the coverage of "AskEDD_ArticleDataCategories"
SIVA KUMAR 507SIVA KUMAR 507
Hi, it is showing required filed is missing.
User-added image
Suraj Tripathi 47Suraj Tripathi 47

Insert failed because of Parent Id

Now I need to know the Parent of this object "Knowledge__DataCategorySelection"

 

SIVA KUMAR 507SIVA KUMAR 507
Hi Suraj, Finally I Found the parent Object, now What should i do?
SIVA KUMAR 507SIVA KUMAR 507
this one suraj
User-added image
SIVA KUMAR 507SIVA KUMAR 507
Hi suraj when i run the below code i am facing different error.
test class:
________
@isTest
Public Class AskEDD_ArticleDataCategoriesTest{


    public static testMethod void testschedule() {
   Knowledge__kav newFAQ = new Knowledge__kav();
    newFAQ.Title = 'test1';
    newFAQ.UrlName = 'test1';
    insert newFAQ;
    
 
Knowledge__DataCategorySelection  obj=new Knowledge__DataCategorySelection();
obj.DataCategoryGroupName='Test';
obj.DataCategoryName='Data';
obj.ParentId=newFaQ.id;
insert obj;

 Test.StartTest();
AskEDD_ArticleDataCategories  sh1 = new AskEDD_ArticleDataCategories ();

String sch = '0 0 23 * * ?'; system.schedule('Test Territory Check', sch, sh1); 
Test.stopTest(); }

}
User-added image