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
Michael MMichael M 

Error on test class: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, There exists no recipient to which you can send this List Email to.: []

Hello, I am getting this error when trying to run my test class: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, There exists no recipient to which you can send this List Email to.: []   Class.CampaignEmailHistoryTest.testNotesExtension: line 10, column 1


How can I fix this? 

Here is the test class:
@isTest
public class CampaignEmailHistoryTest {
    @isTest
    static void testNotesExtension(){

Campaign testCampaign= new Campaign(Name= 'Test');
insert testCampaign;
        
ListEmail le = new ListEmail(campaignId=testCampaign.id, subject= 'hey there',Status= 'Scheduled'); 
    insert le;
   
  ApexPages.StandardController con = new ApexPages.StandardController(testCampaign);
  CampaignEmailHistoryExtension  dce = new CampaignEmailHistoryExtension(con);
         
    }
    
}


And the real class:
public class CampaignEmailHistoryExtension {
public final Campaign thisCampaign;
 
    Public List<ListEmail> listEmails {get; set;}
 
    Public CampaignEmailHistoryExtension(ApexPages.StandardController stdController){
        this.thisCampaign = (Campaign)stdController.getRecord();

        try{
  
            Campaign camp=[Select Id, Name from Campaign where Id = :this.thisCampaign.id];
        
        listEmails = [SELECT ID, campaignId, CreatedDate, CreatedById, CreatedBy.FirstName, CreatedBy.LastName, subject, LastViewedDate
                      FROM ListEmail 
                      WHERE  campaignId IN (Select id from Campaign where Id = :this.thisCampaign.id)];
        }
        catch(exception e){
            Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, 'No old records'));
        }       
}
}
Best Answer chosen by Michael M
Michael MMichael M
It turns out I didn't need to insert a ListEmail to test it. This covers 77%:

@isTest
public class CampaignEmailHistoryTest {
    @isTest
    static void testNotesExtension(){

Campaign testCampaign= new Campaign(Name= 'Test');
insert testCampaign;
        
ListEmail le = new ListEmail(campaignId=testCampaign.id, subject= 'hey there',Status= 'Scheduled'); 
//    insert le;
   
  ApexPages.StandardController con = new ApexPages.StandardController(testCampaign);
  CampaignEmailHistoryExtension  dce = new CampaignEmailHistoryExtension(con);
         
    }
    
}

All Answers

AbhishekAbhishek (Salesforce Developers) 
Try the suggestions as mentioned in the below discussion,

https://salesforce.stackexchange.com/questions/273784/system-dmlexception-insert-failed-first-exception-on-row-0-first-error-canno

It might help you.
Michael MMichael M
I don't think that is the same issue.. 
Michael MMichael M
It turns out I didn't need to insert a ListEmail to test it. This covers 77%:

@isTest
public class CampaignEmailHistoryTest {
    @isTest
    static void testNotesExtension(){

Campaign testCampaign= new Campaign(Name= 'Test');
insert testCampaign;
        
ListEmail le = new ListEmail(campaignId=testCampaign.id, subject= 'hey there',Status= 'Scheduled'); 
//    insert le;
   
  ApexPages.StandardController con = new ApexPages.StandardController(testCampaign);
  CampaignEmailHistoryExtension  dce = new CampaignEmailHistoryExtension(con);
         
    }
    
}
This was selected as the best answer
Andrew GAndrew G
The issue will be the lack of recipients against the ListEmail.
reference (https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_listemail.htm):
Has a one-to-many relationship with ListEmailRecipientSource and ListEmailIndividualRecipient. 

So you would need to create one or the other in your test class.
Something like:
@IsTest
private class CampaignEmailHistoryTest {
    @IsTest
    static void testNotesExtension(){

        Campaign testCampaign= new Campaign(Name= 'Test');
        insert testCampaign;

        ListEmail le = new ListEmail(Name='testlist',  
                              FromAddress='orgwideaddress@yourdomain.com', 
                              CampaignId=testCampaign.Id, 
                              Subject= 'hey there',
                              Status= 'Scheduled');
        insert le;

        Lead lead = new Lead(LastName='Test',FirstName='T',Email='test@test.net',Company='none');
        insert lead;

        ListEmailIndividualRecipient recips = new ListEmailIndividualRecipient();
        recips.RecipientId = lead.Id;
        recips.ListEmailId = le.Id;
        insert recips;


        ApexPages.StandardController con = new ApexPages.StandardController(testCampaign);
        CampaignEmailHistoryExtension  dce = new CampaignEmailHistoryExtension(con);

    }

}

regards

Andrew
Michael MMichael M
Hi Andrew,
Thank you very much for your answer. I tried with your code, but am still getting the same error message..
Andrew GAndrew G
Thats interesting.  I can run that test method without error. Just bashed it through a dev org. Tweaked as below:

@IsTest
private class CampaignEmailHistoryTest {
    @IsTest
    static void testNotesExtension(){

        Campaign testCampaign= new Campaign(Name= 'Test');
        insert testCampaign;
        Lead lead = new Lead(LastName='Test',FirstName='T',Email='test@test.net',Company='none');
        insert lead;

        ListEmail le = new ListEmail(Name='testlist', FromAddress='xxx@xxx.net', CampaignId=testCampaign.Id, Subject= 'hey there',Status= 'Scheduled');
        insert le;

        ListEmailIndividualRecipient recips = new ListEmailIndividualRecipient();
        recips.RecipientId = lead.Id;
        recips.ListEmailId = le.Id;
        insert recips;

        List<Campaign> uCampaigns = [SELECT Id, Name FROM Campaign WHERE Id = :testCampaign.Id];
        System.debug('***Camp Name' +uCampaigns[0].Name);
        System.assertEquals(1,uCampaigns.size());

        List<Lead> uLeads = [SELECT Id, Name FROM Lead WHERE Id = :lead.Id];
        System.debug('***Lead Name' +uLeads[0].Name);
        System.assertEquals(1,uLeads.size());

        List<ListEmail> uLE = [SELECT Id, Name FROM ListEmail WHERE Id = :le.Id];
        System.debug('***Lead Name' +uLE[0].Name);
        System.assertEquals(1, uLE.size());

        List<ListEmailIndividualRecipient> uRecips = [SELECT Id, RecipientId, ListEmailId  FROM ListEmailIndividualRecipient WHERE Id = :recips.id];
        System.debug('***recip ID' +uRecips[0].RecipientId);
        System.debug('***List Id' +uRecips[0].ListEmailId);

        System.assertEquals(1,uRecips.size());



    }

}
perhaps the org type has an impact??


regards
Andrew
Michael MMichael M
Thank you so much. Now, I am getting a different error. It is saying: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_INPUT, invalid email address: FromAddress: [FromAddress]


on this line: 
ListEmail le = new ListEmail(Name='testlist', FromAddress='xxx@xxx.net', CampaignId=testCampaign.Id, Subject= 'hey there',Status= 'Scheduled');
Andrew GAndrew G
That address need to be a recognised address in your Org Wide Addresses list.

regards

Andrew
 
Michael MMichael M
That worked-- thank you!!!!