You need to sign in to do that
Don't have an account?
Ghanesan
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;
}
}
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;
}
}
try with below code getting 100% coverage.
If this helps, Please mark it as best answer.
Thanks!!
All Answers
try with below code getting 100% coverage.
If this helps, Please mark it as best answer.
Thanks!!
https://help.salesforce.com/s/articleView?id=000328824&type=1