+ Start a Discussion
sumit dsumit d 

Test class Error:- List has no rows for assignment to SObject

Hi All,
I have created a test class but its giving me error:- System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.CommEventsController.getZoneId: line 21, column 1 Class.CommEventsControllerTest.getZoneIdTest: line 10, column 1

Can someone help me with this ?

My class and test class is given below:- 
public without sharing class CommEventsController {
    @AuraEnabled(cacheable=true)
    public static String getZoneId(Id recordId) {
        
            String sobjectName;
            String zoneId;
            sobjectName = recordId.getSobjectType().getDescribe().getName();
            if(sobjectName == 'Forum__c'){
                List<Forum__c> listOfForum = New List<Forum__c>();
                listOfForum = [Select Id,Name from Forum__c where Id =: recordId];
                String forumName = listOfForum[0].Name;
                String query = 'SELECT Id, bre__Zone_Id__c FROM bre__BR_Events_Zone__c WHERE Name = :forumName';
                SObject objectRecod = Database.query(query);
                zoneId = (String)objectRecod.get('bre__Zone_Id__c');
                System.debug('zoneId'+zoneId);
            }else{
                List<CollaborationGroup> listOfChatterGroup = New List<CollaborationGroup>();
                listOfChatterGroup = [Select Id,Name from CollaborationGroup where Id =: recordId];
                String groupName = listOfChatterGroup[0].Name;
                String query = 'SELECT Id, bre__Zone_Id__c FROM bre__BR_Events_Zone__c WHERE Name =:groupName';
                SObject objectRecod = Database.query(query);

                zoneId = (String)objectRecod.get('bre__Zone_Id__c');
                System.debug('zoneId'+zoneId);
            }
            return zoneId;
            }
     }
Test class:- 
@istest
public class CommEventsControllerTest {
    public static testmethod void getZoneIdTest(){
        Forum__c f = new Forum__c();
        insert f;
        
        CollaborationGroup groupe = new CollaborationGroup(Name = 'Test 1', CollaborationType = 'Unlisted');
        insert groupe;
        Test.startTest();
        CommEventsController.getZoneId(groupe.id);
        CommEventsController.getZoneId(f.id);
        Test.stopTest();
    }
}
Prateek Prasoon 25Prateek Prasoon 25
Hey Sumit

Please use LIMIT 1 clause.