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
Olivia CannonOlivia Cannon 

Test class failing: List has no rows for assignment to SObject

Hi,

My test class is failing and I don't understand why - any help would be very gratefully received!

This is the error message:

Error Message: System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.TestUpdateTutor1onCampaign.TestUpdateTutor1onCampaign: line 37, column 1

This is line 37: cam = [SELECT Id FROM Campaign WHERE Name = 'Test Job'];

This is the test class as a whole:
@isTest
public class TestUpdateTutor1onCampaign{

    static testMethod void TestUpdateTutor1onCampaign() {
       // create an Account, a Tutor, a Job and a Job Member
  
       Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
       User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
       EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
       LocaleSidKey='en_US', ProfileId = p.Id, 
       TimeZoneSidKey='America/Los_Angeles', UserName='4323f1a8-2698-44c4-a234-dfcf80fa6a6c@testorg.com');
        
       test.StartTest();
       insert u;
       u = [SELECT Id FROM User WHERE Alias = 'standt' AND Email='standarduser@testorg.com'];
      
       Account acc = new Account(Name = 'Test Account');
       insert acc;

       acc = [SELECT Id FROM Account WHERE Name = 'Test Account'];
       
       //Get the Contact record types
       List<RecordType> listRecordTypes = [Select Name, Id From RecordType where sObjectType='Contact' and isActive=true];
       Map<String,String> mapRecordTypes = new Map<String,String>();
       for(RecordType rt: listRecordTypes) {
           mapRecordTypes.put(rt.Name,rt.Id);
       }      

       Contact c = new Contact(AccountID = acc.Id, FirstName = 'Test', LastName = 'Contact', Main_Profile__c = 'www.google.com',
                               RecordTypeId = mapRecordTypes.Get('Tutor') 
                               );
        insert c;
        c = [SELECT Id FROM Contact WHERE FirstName = 'Test'];
        
        Campaign cam = new Campaign(Name = 'Test Job', Tutor_1__c = null);
        insert cam;
        cam = [SELECT Id FROM Campaign WHERE Name = 'Test Job'];
            
        // insert a Campaign Member
        CampaignMember cm = new CampaignMember(CampaignId = cam.Id, ContactId = c.Id, Send_tutor_details_to_client__c = 'First');
        insert cm;
        cm = [SELECT Id FROM CampaignMember WHERE ContactId =: c.Id AND Send_tutor_details_to_client__c = 'First' ];
        
        system.assertEquals(cam.Tutor_1__c, cm.Tutor_details_for_email__c);
        
        List<Campaign> camtest = [SELECT Id FROM Campaign WHERE Id =: cam.Id ];
        
        system.assertEquals(cam.Tutor_1__c, cm.Tutor_details_for_email__c);

       test.StopTest();
    }

}

Thank you!

Best Answer chosen by Olivia Cannon
Anoop yadavAnoop yadav
Hi,

I think you do not have permissin to create Campaign.
In Current user, make "Marketing User" checkbox as checked.

For more information put a debug after line 36 in your code.
I think It is not inserting the campaign.

All Answers

Sonam_SFDCSonam_SFDC
SOQL typically returns a List, this error is seen when your query returns one or more than one record.

http://salesforce.stackexchange.com/questions/30578/list-has-no-rows-for-assignment-to-sobject
Anoop yadavAnoop yadav
Hi,

I think you do not have permissin to create Campaign.
In Current user, make "Marketing User" checkbox as checked.

For more information put a debug after line 36 in your code.
I think It is not inserting the campaign.

This was selected as the best answer
Olivia CannonOlivia Cannon
I can't believe I didn't think of the Marketing User - thank you!