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 Apex test class for the below code please help me guys.

Apex Class:
______________

public class AskEDD_ArticleAction {
    @InvocableMethod
    public static List<FlowOutputs> updateArticleTypeForDataCategories(List<FlowInputs> articleType) 
    {
        List<Knowledge__kav> lstArticleType = [SELECT Id, Title, AskEDD_Category__c FROM Knowledge__kav WHERE Id =:articleType[0].recordId AND (PublishStatus = 'draft' OR Approval_Status__c = 'Pending Approval')];

        List<Knowledge__DataCategorySelection> lstDC = [SELECT ParentId, DataCategoryName   FROM Knowledge__DataCategorySelection
                                                            WHERE ParentId =:articleType[0].recordId];
List<FlowOutputs> outputlist = new List<FlowOutputs>();
        
        FlowOutputs output = new FlowOutputs();
        
        Map<Id, String> datacategoryNameMap = new Map<Id, String>();

        for(Knowledge__DataCategorySelection dcObj:lstDC)
        {   
            if(datacategoryNameMap.containsKey(dcObj.ParentId))
            {
                String str =  datacategoryNameMap.get(dcObj.ParentId);
                datacategoryNameMap.put(dcObj.ParentId, str + ',' + dcObj.DataCategoryName);
            }
            else
            {
                datacategoryNameMap.put(dcObj.ParentId, dcObj.DataCategoryName);
            }
        }

        for(Knowledge__kav artObj:lstArticleType)
        {
            if(datacategoryNameMap.containsKey(artObj.Id))
            {
                artObj.AskEDD_Category__c = datacategoryNameMap.get(artObj.Id);
                
                if (String.isEmpty(artObj.AskEDD_Category__c)){
                     output.noCategory = true;
                    outputlist.add(output);
                    return outputlist;
                } 
                else {
                     output.noCategory = false;
                    outputlist.add(output);
                }
         //     output.categoryString  = artObj.AskEDD_Category__c;
            }
        }

     //   update lstArticleType;
        return outputlist;
       }  
    public class FlowInputs {
        @InvocableVariable
        public string recordId;
    }
    public class FlowOutputs {
        @InvocableVariable
        public boolean noCategory;
    }
}
Best Answer chosen by SIVA KUMAR 507
Suraj Tripathi 47Suraj Tripathi 47
Hi,

You can try this test class:-
@istest
public class AskEDD_ArticleActionTest {
    @istest
    public static void test(){
        Knowledge__kav k= new Knowledge__kav();
        k.IsVisibleInCsp=true;
        k.UrlName='UTF-8';
        k.Title='title';
        k.IsVisibleInPkb=true;
        k.AskEDD_Category__c='Payroll_Tax';
        k.Approval_Status__c='Pending Approval';
        insert k;
        
        Knowledge__DataCategorySelection kn= new Knowledge__DataCategorySelection();
        kn.ParentId=k.id;
        kn.DataCategoryGroupName='AskEDD';
        kn.DataCategoryName='Payroll_Tax';
        insert kn;
        
        Knowledge__DataCategorySelection kn1= new Knowledge__DataCategorySelection();
        kn1.ParentId=k.id;
        kn1.DataCategoryGroupName='AskEDD';
        kn1.DataCategoryName='Paid_Family_Leave';
        insert kn1;
        
        test.startTest();
        list<AskEDD_ArticleAction.FlowInputs> fList= new list<AskEDD_ArticleAction.FlowInputs>();
        AskEDD_ArticleAction.FlowInputs fl= new AskEDD_ArticleAction.FlowInputs();
        fl.recordId=k.id;
        fList.add(fl);
        
        AskEDD_ArticleAction.updateArticleTypeForDataCategories(fList);
        test.stopTest();
    }
}

 

All Answers

nilesh walkenilesh walke
hi SIVA please mention your question so we can answer it you just privided the logic please let us known what you want to build in the logic
SIVA KUMAR 507SIVA KUMAR 507
Hi Nilesh,
I Need test class for that class please help me
nilesh walkenilesh walke
yes SIVA  we will help you but first what you are doing first understand it then aske peoples  atlist put your question in it you are just saying you need test class but why what you are making think like that and if i help  also you should understand that code so please provide ecplanation it what you want to do (WE can make test class in many topics ) you first clerify it what you want then only we can shjeect you the code possibility
 
Suraj Tripathi 47Suraj Tripathi 47
Hi SIVA,
You can take reference from this below code.
@istest
public class AskEDD_ArticleActionTest {
    @istest
    public static void test(){
        Knowledge__kav k= new Knowledge__kav();
        k.IsVisibleInCsp=true;
        k.UrlName='UTF-8';
        k.Title='title';
        k.IsVisibleInPkb=true;
        k.AskEDD_Category__c='Geography';
        k.Approval_Status__c='Pending Approval';
        insert k;
        
        Knowledge__DataCategorySelection kn= new Knowledge__DataCategorySelection();
        kn.ParentId=k.id;
        kn.DataCategoryGroupName='KnowledgeArticleVersion';
        kn.DataCategoryName='Geography';
        insert kn;
        
        Knowledge__DataCategorySelection kn1= new Knowledge__DataCategorySelection();
        kn1.ParentId=k.id;
        kn1.DataCategoryGroupName='KnowledgeArticleVersion';
        kn1.DataCategoryName='student';
        insert kn1;
        
        test.startTest();
        list<AskEDD_ArticleAction.FlowInputs> fList= new list<AskEDD_ArticleAction.FlowInputs>();
        AskEDD_ArticleAction.FlowInputs fl= new AskEDD_ArticleAction.FlowInputs();
        fl.recordId=k.id;
        fList.add(fl);
        
        AskEDD_ArticleAction.updateArticleTypeForDataCategories(fList);
        test.stopTest();
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.
SIVA KUMAR 507SIVA KUMAR 507
Hi Suraj, Thanks for Reply and when i tried to run the test class it is showing 0% code coverage suraj. can you once recheck and help me please.
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Siva,

When i run the test class in my org I covered 87%

User-added image

Please check your test class and tell me whai is the error comes in your code.

Thanks
SIVA KUMAR 507SIVA KUMAR 507
Hi Suraj,
i am getting below errors can you please solve it.
.User-added image
Suraj Tripathi 47Suraj Tripathi 47
Hi siva,

you are getting this error because you have not created the Data Category Groups in your org. 
You can follow these steps:-
--> Click on Gear Icon and Select Service Setup.
--> Enter Data Category Setup in the Quick Find box and select Data Category Setup.
--> In Category groups click new and fill the group name as KnowledgeArticleVersion and save it.
User-added image
--> Activate Category group.
--> In Categories in KnowledgeArticleVersion section.
User-added image
--> Click the action tab and then click Add child category and fill the name as student then click on Add and then again fill name as Geography and click on save button.

Please mark it as the best answer if it helps.
Thanks.
SIVA KUMAR 507SIVA KUMAR 507
Hi suraj, in my org already there is one data category.

User-added image
Suraj Tripathi 47Suraj Tripathi 47
Hi SIVA,
Check this test class:-
@istest
public class AskEDD_ArticleActionTest {
    @istest
    public static void test(){
        Knowledge__kav k= new Knowledge__kav();
        k.IsVisibleInCsp=true;
        k.UrlName='UTF-8';
        k.Title='title';
        k.IsVisibleInPkb=true;
        k.AskEDD_Category__c='Workforce Services';
        k.Approval_Status__c='Pending Approval';
        insert k;
        
        Knowledge__DataCategorySelection kn= new Knowledge__DataCategorySelection();
        kn.ParentId=k.id;
        kn.DataCategoryGroupName='AskEDD';
        kn.DataCategoryName='Workforce Services';
        insert kn;
        
        Knowledge__DataCategorySelection kn1= new Knowledge__DataCategorySelection();
        kn1.ParentId=k.id;
        kn1.DataCategoryGroupName='AskEDD';
        kn1.DataCategoryName='Labor Market Information';
        insert kn1;
        
        test.startTest();
        list<AskEDD_ArticleAction.FlowInputs> fList= new list<AskEDD_ArticleAction.FlowInputs>();
        AskEDD_ArticleAction.FlowInputs fl= new AskEDD_ArticleAction.FlowInputs();
        fl.recordId=k.id;
        fList.add(fl);
        
        AskEDD_ArticleAction.updateArticleTypeForDataCategories(fList);
        test.stopTest();
    }
}
  • In case you find any other issue please mention. 
  • If you find your Solution then mark this as the best answer. 

 
SIVA KUMAR 507SIVA KUMAR 507
HI Suraj, just now i run the test class and again its showing same error.
User-added image
Suraj Tripathi 47Suraj Tripathi 47
Hi,
You can try this test class:-
@istest
public class AskEDD_ArticleActionTest {
    @istest
    public static void test(){
        Knowledge__kav k= new Knowledge__kav();
        k.IsVisibleInCsp=true;
        k.UrlName='UTF-8';
        k.Title='title';
        k.IsVisibleInPkb=true;
        k.AskEDD_Category__c='Payroll Tax';
        k.Approval_Status__c='Pending Approval';
        insert k;
        
        Knowledge__DataCategorySelection kn= new Knowledge__DataCategorySelection();
        kn.ParentId=k.id;
        kn.DataCategoryGroupName='AskEDD';
        kn.DataCategoryName='Payroll Tax';
        insert kn;
        
        Knowledge__DataCategorySelection kn1= new Knowledge__DataCategorySelection();
        kn1.ParentId=k.id;
        kn1.DataCategoryGroupName='AskEDD';
        kn1.DataCategoryName='Paid Family Leave';
        insert kn1;
        
        test.startTest();
        list<AskEDD_ArticleAction.FlowInputs> fList= new list<AskEDD_ArticleAction.FlowInputs>();
        AskEDD_ArticleAction.FlowInputs fl= new AskEDD_ArticleAction.FlowInputs();
        fl.recordId=k.id;
        fList.add(fl);
        
        AskEDD_ArticleAction.updateArticleTypeForDataCategories(fList);
        test.stopTest();
    }
}
SIVA KUMAR 507SIVA KUMAR 507
Hi Suraj, just now tried the latest test class also again i am getting same error please suraj help me with these
SIVA KUMAR 507SIVA KUMAR 507
Hi Suraj is these is the Datacategories 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,

You can try this test class:-
@istest
public class AskEDD_ArticleActionTest {
    @istest
    public static void test(){
        Knowledge__kav k= new Knowledge__kav();
        k.IsVisibleInCsp=true;
        k.UrlName='UTF-8';
        k.Title='title';
        k.IsVisibleInPkb=true;
        k.AskEDD_Category__c='Payroll_Tax';
        k.Approval_Status__c='Pending Approval';
        insert k;
        
        Knowledge__DataCategorySelection kn= new Knowledge__DataCategorySelection();
        kn.ParentId=k.id;
        kn.DataCategoryGroupName='AskEDD';
        kn.DataCategoryName='Payroll_Tax';
        insert kn;
        
        Knowledge__DataCategorySelection kn1= new Knowledge__DataCategorySelection();
        kn1.ParentId=k.id;
        kn1.DataCategoryGroupName='AskEDD';
        kn1.DataCategoryName='Paid_Family_Leave';
        insert kn1;
        
        test.startTest();
        list<AskEDD_ArticleAction.FlowInputs> fList= new list<AskEDD_ArticleAction.FlowInputs>();
        AskEDD_ArticleAction.FlowInputs fl= new AskEDD_ArticleAction.FlowInputs();
        fl.recordId=k.id;
        fList.add(fl);
        
        AskEDD_ArticleAction.updateArticleTypeForDataCategories(fList);
        test.stopTest();
    }
}

 
This was selected as the best answer
SIVA KUMAR 507SIVA KUMAR 507
Hi Suraj Thanks for your reply. i covered 87% code. thanks for your help.
SIVA KUMAR 507SIVA KUMAR 507
Hi suraj, I Marked  best Answer 
Suraj Tripathi 47Suraj Tripathi 47
Thank you Siva