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
GhanesanGhanesan 

System.QueryException: List has no rows for assignment to SObject - Adding Lead to Campaign

Getting this error on my test class: System.QueryException: List has no rows for assignment to SObject. line 18
Batch Apex:

Batch apex working fine, but in test class trying to add lead to campaign. 

global class EmailBounceBatchApex implements Database.Batchable<sobject>{
    global Database.QueryLocator start(Database.BatchableContext bc) {
        string query = 'SELECT Id,Name from Lead where EmailBouncedDate!=Null AND EmailBouncedReason!=Null';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc, List<Lead> Scope) {
        for(Lead l:Scope){
            
                l.Status = 'Disqualified';
                l.Reason_for__c = 'Inadequate Data';
                System.Debug(l.status);
           }
            update scope;
    
    }
    global void finish(Database.BatchableContext bc) {
                }
}

Test Class:
 @isTest(SeeAllData=true)
public class EmailBounceBatchApexTest {
    Public Static testmethod void BounceTest() {
        
       
        Lead testLead = new Lead();
        testLead.LastName = 'Test';
        testLead.Company ='ABC';
        testLead.Email='trd@gmail.com';
        testLead.Phone='1234567890';
        insert testLead;
        
       Campaign camp1 = new Campaign();
        camp1.Name = 'Test Campaign 1';
        camp1.IsActive = True;
        insert camp1;
        
        Campaign campID = [SELECT Id FROM Campaign WHERE Name = 'Test Campaign 1'
                           AND IsActive = True AND CreatedDate = :System.today() ];
      
      CampaignMember testMember = new CampaignMember(LeadId =testLead.Id, CampaignId ='campID', Status = 'Sent');
        insert testMember;
            
        }
}
Best Answer chosen by Ghanesan
AnkaiahAnkaiah (Salesforce Developers) 
Hi Ganesan,

try with below code getting 100% coverage.
@isTest
public class EmailBounceBatchApexTest {
    Public Static testmethod void BounceTest() {      
        Lead testLead = new Lead();
        testLead.LastName = 'Test';
        testLead.Company ='ABC';
        testLead.Email='trd@gmail.com';
        testLead.Phone='1234567890';
        insert testLead;
        // we can't able to perform insert operation on EmailBouncedDate & EmailBouncedReason fields
        // i.e. you need to keep test data on update 
        testLead.EmailBouncedDate = system.today();
        testLead.EmailBouncedReason ='email address was not valid';
        update testLead;
        
       Campaign camp1 = new Campaign();
        camp1.Name = 'Test Campaign 1';
        camp1.IsActive = True;
        insert camp1;
      
      CampaignMember testMember = new CampaignMember(LeadId =testLead.Id, CampaignId =camp1.id, Status = 'Sent');
        insert testMember;
        EmailBounceBatchApex bc = new EmailBounceBatchApex();
        database.executeBatch(bc);
            
        }
}

If this helps, Please mark it as best answer.

Thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Ganesan,

try with below code getting 100% coverage.
@isTest
public class EmailBounceBatchApexTest {
    Public Static testmethod void BounceTest() {      
        Lead testLead = new Lead();
        testLead.LastName = 'Test';
        testLead.Company ='ABC';
        testLead.Email='trd@gmail.com';
        testLead.Phone='1234567890';
        insert testLead;
        // we can't able to perform insert operation on EmailBouncedDate & EmailBouncedReason fields
        // i.e. you need to keep test data on update 
        testLead.EmailBouncedDate = system.today();
        testLead.EmailBouncedReason ='email address was not valid';
        update testLead;
        
       Campaign camp1 = new Campaign();
        camp1.Name = 'Test Campaign 1';
        camp1.IsActive = True;
        insert camp1;
      
      CampaignMember testMember = new CampaignMember(LeadId =testLead.Id, CampaignId =camp1.id, Status = 'Sent');
        insert testMember;
        EmailBounceBatchApex bc = new EmailBounceBatchApex();
        database.executeBatch(bc);
            
        }
}

If this helps, Please mark it as best answer.

Thanks!!
This was selected as the best answer
The TechieThe Techie
Hi,
https://help.salesforce.com/s/articleView?id=000328824&type=1
GhanesanGhanesan
Thanks @Anikaiah, Code covered 100%.