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.

Apex Class:
____________
public class EDDKMSCreateArticleFromTemplate {
  @AuraEnabled
    public static List<Knowledge_Article_Template__c> getKnowledgeArticleTemplates() {
        try {
          return [SELECT Id, Name FROM Knowledge_Article_Template__c];
        }
        catch(Exception ex) {
            throw new AuraHandledException(ex.getMessage());
        }
    }
    
    @AuraEnabled
    public static TemplateWrapper getKnowledgeArticleTemplateBody(String templateId, String templateName) {
        try {
            TemplateWrapper wrapper = new TemplateWrapper();
            if(Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get(templateName) != null) 
                wrapper.recordTypeId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get(templateName).getRecordTypeId(); 
            else
                wrapper.recordTypeId = '';
            
            wrapper.template = [SELECT   Id, Name, Template_Body__c, OT_Body__c, CF_Body__c, DET_Body__c,
                                    ADJ_Body__c, MGR_OT_EPR_Body__c, MGR_UISD_Body__c
                                FROM  Knowledge_Article_Template__c
                                WHERE  Id =: templateId];
            return wrapper;
        }
        catch(Exception ex) {
            throw new AuraHandledException(ex.getMessage());
        }        
    }
    
    public class TemplateWrapper {
        @AuraEnabled public String recordTypeId {get;set;}
        @AuraEnabled public Knowledge_Article_Template__c template {get;set;}
    }
}
Best Answer chosen by SIVA KUMAR 507
Suraj Tripathi 47Suraj Tripathi 47
Hi Siva,
You can take reference from this below code.
@isTest
public class EDDKMSCreateArticleFromTemplateTest {
    @istest
    public static void test(){
        Knowledge_Article_Template__c kn= new Knowledge_Article_Template__c();
        kn.Name='abc';
        kn.Template_Body__c='xyz';
        insert kn;
        
        Id recId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get('FAQ').getRecordTypeId();    
        
        Knowledge__kav k= new Knowledge__kav();
        k.IsVisibleInCsp=true;
        k.UrlName='UTF-8';
        k.Title='title';
        k.IsVisibleInPkb=true;
        k.RecordtypeId=recId;
        insert k;
        
        
        test.startTest();
        EDDKMSCreateArticleFromTemplate.getKnowledgeArticleTemplates();
        EDDKMSCreateArticleFromTemplate.getKnowledgeArticleTemplateBody(kn.id, 'FAQ');
        test.stopTest();
    }
     @istest
    public static void test1(){
        Knowledge_Article_Template__c kn= new Knowledge_Article_Template__c();
        kn.Name='abc';
        kn.Template_Body__c='xyz';
        insert kn;
                
        Knowledge__kav k= new Knowledge__kav();
        k.IsVisibleInCsp=true;
        k.UrlName='UTF-8';
        k.Title='title';
        k.IsVisibleInPkb=true;
        insert k;
      
        test.startTest();
        EDDKMSCreateArticleFromTemplate.getKnowledgeArticleTemplateBody(kn.id, '');
        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.

 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Siva,
You can take reference from this below code.
@isTest
public class EDDKMSCreateArticleFromTemplateTest {
    @istest
    public static void test(){
        Knowledge_Article_Template__c kn= new Knowledge_Article_Template__c();
        kn.Name='abc';
        kn.Template_Body__c='xyz';
        insert kn;
        
        Id recId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get('FAQ').getRecordTypeId();    
        
        Knowledge__kav k= new Knowledge__kav();
        k.IsVisibleInCsp=true;
        k.UrlName='UTF-8';
        k.Title='title';
        k.IsVisibleInPkb=true;
        k.RecordtypeId=recId;
        insert k;
        
        
        test.startTest();
        EDDKMSCreateArticleFromTemplate.getKnowledgeArticleTemplates();
        EDDKMSCreateArticleFromTemplate.getKnowledgeArticleTemplateBody(kn.id, 'FAQ');
        test.stopTest();
    }
     @istest
    public static void test1(){
        Knowledge_Article_Template__c kn= new Knowledge_Article_Template__c();
        kn.Name='abc';
        kn.Template_Body__c='xyz';
        insert kn;
                
        Knowledge__kav k= new Knowledge__kav();
        k.IsVisibleInCsp=true;
        k.UrlName='UTF-8';
        k.Title='title';
        k.IsVisibleInPkb=true;
        insert k;
      
        test.startTest();
        EDDKMSCreateArticleFromTemplate.getKnowledgeArticleTemplateBody(kn.id, '');
        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.

 
This was selected as the best answer
SIVA KUMAR 507SIVA KUMAR 507
Hi suraj,
thanks for your reply. when I run the test class i covered only 55%. and i am getting below User-added imageerrors
Suraj Tripathi 47Suraj Tripathi 47
Hi Shiva,

When I run the test class in my org I covered 77%
User-added image

Please check the error in your test class afterthat you tell me what is the error.

Thanks.
SIVA KUMAR 507SIVA KUMAR 507
Hi Suraj,
When i run the test class in my org I covered 55% and i am getting below errors.
User-added image
Suraj Tripathi 47Suraj Tripathi 47
Yes siva,
 
This error is getting in your test class because you have not created the recordtype inside the Knowledge__kav Object.
Follow these steps:-
--> Go to the Setup --> click the object manager --> Search Knowledge__kav object --> click the Knowledge__kav object.
--> In the left side You will be seeing the recordType option --> Click the recordtype 

If the recordType is already created in your org then copy the recordtype name and paste this name inside the test class 
Id recId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get('FAQ').getRecordTypeId()  you will paste the recordtype name in place of the 'FAQ' recordtype name.

And if the record type is not created in your org then you will create the recordtype and the name of the recordtype will paste instead of the 'FAQ' recordType name.

If you will doing these steps Maybe your class will run successfully.

Thanks Siva