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
Kasia Wojewodzka 7Kasia Wojewodzka 7 

Knowledge Article Test Class. System.QueryException: List has no rows for assignment to SObject

Dear Community 

Hope you can assit me with the error message I am getting when running test on the line 54?  

Knowledge__kav testArticle = [SELECT ID, Region__c, Line_of_Business__c FROM Knowledge__kav WHERE Title = 'TestIHDNA'];

Would you advise how to proceed?  
I am testing  Knowledge Article field updates here
1. If the knowloedge creator has certain  profile, I want to check if Knowdlege Article 2 custom fields were updated. 

2. Then if the knowledge article creator has a one of the roles listed, the fields on the Article are being updated per requirments. 


Best
Kasia 
 
@isTest
public class Test_PB_KnowledgeLOBnadRegionUpdates {


    //Tests the node "testVSSNA"
    @isTest
    static void testVSSNA(){
        Test.startTest();
        Profile p = [select id from Profile where profile.name='Customer and Technical Support - CAG NA'];
        User userVSSNA = [SELECT Id from User where ProfileId = :p.id AND IsActive = TRUE limit 1];
        System.runAs(userVSSNA){
            //Create a General Article
            Knowledge__kav testKnowledgeArticle = new Knowledge__kav(
                     RecordTypeId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get('General').getRecordTypeId(),
                     Title = 'TestVSSNA',
                     UrlName = 'salesforce-crm',
                     Summary = 'Salesforce Cloud CRM',
                     Language = 'en_US'

              );
              insert testKnowledgeArticle;

                // Test if Region is North America and Line of Business is VSS
                Knowledge__kav testArticle =  [SELECT ID, Region__c, Line_of_Business__c FROM Knowledge__kav WHERE Title = 'TestVSSNA'];

                System.assertEquals(testArticle.Region__c, 'North America');
                System.assertEquals(testArticle.Line_of_Business__c, 'VSS');

            Test.stopTest();
        }
    }

    @isTest
    static void testIHDNA(){
        Test.startTest();
        Profile p = [select id from Profile where profile.name='Diagnostic Technical Support'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name = 'CAG IHD CS Manager' or Name = 'CAG IHD CS Supervisor' or Name = 'CAG IHD CS Agent' LIMIT 1]; 
        // Create test user with specific role
        User userIHDNA = new User(UserRoleId = r.Id, ProfileId = p.Id, UserPermissionsKnowledgeUser = true, UserName='xxx@testorg.com', Alias = 'standt',Email='xxx@testorg.com',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', TimeZoneSidKey='America/Los_Angeles');
        insert userIHDNA ;
        System.runAs(userIHDNA){
                //Create a General Article
                Knowledge__kav testKnowledgeArticle = new Knowledge__kav(
                RecordTypeId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get('General').getRecordTypeId(),
                Title = 'TestIHDNA',
                UrlName = 'salesforce-crm',
                Summary = 'Salesforce Cloud CRM',
                Language = 'en_US'
                );
                insert testKnowledgeArticle;

                 // Test if Region is North America and Line of Business is VSS
                Knowledge__kav testArticle = [SELECT ID, Region__c, Line_of_Business__c FROM Knowledge__kav WHERE Title = 'TestIHDNA'];

                System.assertEquals(testArticle.Region__c, 'North America');
                System.assertEquals(testArticle.Line_of_Business__c, 'IHD');
                Test.stopTest();
         }
    }
}


 
AnudeepAnudeep (Salesforce Developers) 
The error "List has no rows for assignment to SObject" occurs when the query doesn't return any rows. 
 
Knowledge__kav testArticle = [SELECT ID, Region__c, Line_of_Business__c FROM Knowledge__kav WHERE Title = 'TestIHDNA'];

The above query is not returning any rows. You may want to try changing the value in the where clause so that it returns the results. Also, You can do a size check to avoid this exception

See Apex error 'List has no rows for assignment to SObject' to learn more