+ Start a Discussion
Hermann OuréHermann Ouré 

Apex trigger test class for Knowledge__DataCategorySelection

Hello,
I am trying to write a test class for Knowledge__DataCategorySelection.
when running the test, the test method passed but the coverage of my trigger remains at 0%.
What am I doing wrong?
Thank you.
Hermann

Apex trigger
trigger KnownErrorCatTrigger on Knowledge__kav (after insert) {
    List<Knowledge__DataCategorySelection> dataCategories = new List<Knowledge__DataCategorySelection>();
    Id knownErrorRecordTypeId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get(label.Param_Article_KE).getRecordTypeId();
    String dataGroupName = label.Param_DataCatGroupName_HCC;
    String dataCatName = label.Param_DataCategoryName_KE;
    for(Knowledge__kav kw : trigger.new )
    {
        if(kw.RecordTypeId == knownErrorRecordTypeId) {
        //Knowledge__DataCategorySelection kDataCat = KnowledgeCategoryHelperKnownError.createDataCategory(kw.Id, 'Help_Center_Categories', 'Known_Error');
        Knowledge__DataCategorySelection dataCategory = new Knowledge__DataCategorySelection();
    	dataCategory.ParentId = kw.Id;
    	dataCategory.DataCategoryGroupName = dataGroupName;
   	    dataCategory.DataCategoryName = dataCatName;
        dataCategories.add(dataCategory);
       }
    }
    insert dataCategories;

}
Test Class
@isTest

public class KnownErrorCatTrigger_Test {
    
    public static testMethod void knowledge_DataCategorySelection () {
        
        Knowledge__DataCategorySelection dataCategory = new Knowledge__DataCategorySelection();
        dataCategory.ParentId = '02o5E000000Jl01QAC';
        dataCategory.DataCategoryGroupName = 'Help_Center_Categories';
        dataCategory.DataCategoryName = 'Known_Error';
    }
}


 
Best Answer chosen by Hermann Ouré
Agustin BAgustin B
Hi Hermann, you are right, now you should be calling the right trigger, which is the error you are getting?
Verify you dont need any other required field for creating the Knowledge__kav record, maybe thats why you cant insert it.
If not please tell me the line of the error so I can help better.

 

All Answers

Agustin BAgustin B
Hi, you need to insert your record, so in line 11 put: insert dataCategory;
Be careful with the insert dataCategories in your trigger, it may cause a loop.

If it helps please mark as correct, it may help others.
Hermann OuréHermann Ouré
Hello Agustin,

Thanks for your reply.
So I have added insert dataCategory; but now the test is failing.
@isTest

public class KnownErrorCatTrigger_Test {
    
    public static testMethod void knowledge_DataCategorySelection () {
        
        Knowledge__DataCategorySelection dataCategory = new Knowledge__DataCategorySelection();
        
        dataCategory.ParentId = '02o5E000000Jl01QAC';
        dataCategory.DataCategoryGroupName = 'Help_Center_Categories';
        dataCategory.DataCategoryName = 'Known_Error';
        
        insert dataCategory;
    }
}

 
Agustin BAgustin B

Hi Hermann, we are making progress, what is the error saying? 

Use a system.debug(knownErrorRecordTypeId ); in line 4 to see if that record type if filling properly.

If not show me the error and I can help

Please like the comment if we are making progress or mark as correct if you solved your issue.

Hermann OuréHermann Ouré
Agustin,
I think, I was going in the wrong directiong and your previous message made me understand that I need to create a test on Knowledge object to be able to call the trigger I believe, So I have altered to test class.
But something is still missing
@isTest

public class KnownErrorCatTrigger_Test {
    
    public static testMethod void knowledge_DataCategorySelection () {
        
        Id knownErrorRecordTypeId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get(label.Param_Article_KE).getRecordTypeId();

        Knowledge__kav article = new Knowledge__kav(RecordTypeId = knownErrorRecordTypeId);
        
        insert article;
   
    }
}

 
Agustin BAgustin B
Hi Hermann, you are right, now you should be calling the right trigger, which is the error you are getting?
Verify you dont need any other required field for creating the Knowledge__kav record, maybe thats why you cant insert it.
If not please tell me the line of the error so I can help better.

 
This was selected as the best answer
Hermann OuréHermann Ouré
Error is line 11
Insert failed
User-added image
Agustin BAgustin B
Hi Hermann, you have a validation rule or something like that, check some lines above you need to also fill the "How_To_Reproduce" field.
Check that formula and verify you fulfill everything to have a record inserted.

I ll wait for your response.
If it helps please like and if it solves your issue please close the question marking as correct.
Hermann OuréHermann Ouré

Hi Agustin,
Thank you so much for your help...

You put me on the right track to find the solution...
Like you said, I was missing the required field when running the test
Now it's fine

@isTest

public class KnownErrorCatTrigger_Test {
    
    
    
    public static testMethod void knowledge_DataCategorySelection () {
        
        Id knownErrorRecordTypeId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get(label.Param_Article_KE).getRecordTypeId();

        Knowledge__kav article = new Knowledge__kav(RecordTypeId = knownErrorRecordTypeId);
        	article.Title = 'Test';
        	article.UrlName = 'Test';
        	article.Summary = 'Test';
        	article.Affecting__c = 'Test';
        	article.How_to_Reproduce__c = 'Test';
        
        insert article;
   
    }
}