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
Jack Rose 1Jack Rose 1 

Unable to deploy without Apex Test

Hi, 

Really hoping someone is able to help me with an issue I'm having. I am a Salesforce Admin with no developer to hand. 
I am trying to do a change set to our Production Org, for a Apex Class. The purpose of the class is to insert the Data Categories into Custom Fields on the Knowledge Article. 
Unfortunately my change set failed as my test coverage was 0%. 

I have since discovered that I need a test class for the Apex Class but have no idea where to start. 
Here is the class I have, any help would be greatly appreciated. 
public class DataCategoryMgmtDL4B {
  @InvocableMethod
    public static void updateArticleTypeForDataCategories(List<Id> articleTypeIds)
    {
    
        List<string> DL4B_Products = new List<String>{'Property_Owner_Residential','Property_Owner_Commercial','Tradesman','Retail','Business_From_Home','Office_and_Surgery','Hotel','Pub_Restaurant','Tradesman_Professional','Property_Owner','Bed_Breakfast','Hair_Beauty','Office_Professional','Professional_Indemnity','Cyber','Van'};
        List<string> DL4B_Systems = new List<String>{'Acturis_ETrade','Evo','IS2000','PI_Legacy','SSP','UIS'};
        
        
        List<Knowledge__kav> lstArticleType = [SELECT Id, Title, DL4B_Data_Categories__c, DL4B_Systems_Categories__c FROM Knowledge__kav WHERE Id IN:articleTypeIds
                                                    AND PublishStatus = 'draft'];
        List<Knowledge__DataCategorySelection> lstDC = [SELECT ParentId, DataCategoryName FROM Knowledge__DataCategorySelection
                                                            WHERE ParentId IN:articleTypeIds];
        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))
            {   
                system.debug(datacategoryNameMap.get(artObj.Id));
                String allDataCategories = datacategoryNameMap.get(artObj.Id);
                List<String> parts = datacategoryNameMap.get(artObj.Id).split(';');
                artObj.DL4B_Data_Categories__c = '';
                artObj.DL4B_Systems_Categories__c = '';
                for(String unix: parts)
                {    
                    if(DL4B_Products.contains(unix)){
                        artObj.DL4B_Data_Categories__c += unix + ';';
                    }
                    if(DL4B_Systems.contains(unix)){
                        artObj.DL4B_Systems_Categories__c += unix + ';';
                    }
            }
        }
        update lstArticleType;
    }
}}

 
Raj VakatiRaj Vakati
Try this test class
 
@isTest 
private class NewsComponentClassTest{
  static testMethod void MakeArticle() {
    User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
    System.runAs (thisUser) 
    {
      Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
      User u = new User(Alias = 'standt', Email='test@asdasdsd.com', 
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = p.Id, 
        TimeZoneSidKey='America/Los_Angeles', UserName='sdss1sdasdasdasd23@test.com'');

      insert u;
	  
	    System.Test.startTest();

		// Test data and all required fields 
      Knowledge__kav newArticle = new Knowledge__kav
      (Title='test article', UrlName='testarticleurl', Language='en_US', DL4B_Data_Categories__c='Property_Owner_Commercial', DL4B_Systems_Categories__c ='IS2000'  
        );


       // Insert Article
      insert newArticle;

      // Publish Article
      String articleId = newArticle.Id;
      system.debug(articleId);
      KbManagement.PublishingService.publishArticle(articleId, true);

       

     

      
       

       System.Test.stopTest();

       }
	   }
	   
	   
	   
	   }