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
Kevin LanguedocKevin Languedoc 

Missing required custom field in test class

I am trying to run a test but I keep getting the same error that the Address field is empty or missing. When I add a !Test,isRunning I get the following error msg. But the actual class works beautifully in dev. I am trying to get my test coverage up and apply the proper unit test. The actual class is below.

This is the test class

Error message:

 You cannot call addFields when the data is being passed into the controller by the caller.

OR:

13:33:29:311 FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc): []

when I add the following code
 
if(!Test.isRunningTest()){
             sc.addFields(new List<String>{'CaseAssignedOwner__c'});
        }

@isTest
public class testCaseForm1747SendMail {
    static testMethod void testSend(){
       
        Case testCase = new Case();
        testCase.Subject = 'unittest';
        testCase.Description = 'unit test description';
        testCase.CaseAssignedOwner__c = 'klanguedoc@jdi.jubl.com';
        testCase.Case_Category__c = 'Product Enquiry';
        insert testCase;
            
        ApexPages.StandardController sc = new ApexPages.StandardController(testCase);
        // test constructor;
        // testCaseForm1747SendMail

        
       
    
        CaseForm1747SendMail Send1747 = new CaseForm1747SendMail(sc);
        sc.addFields(new List<String>{'CaseAssignedOwner__c'});
       
        send1747.send();
        
        
    }
}

Actual class : works fine
 
public class CaseForm1747SendMail {
    private final Case c;
    public CaseForm1747SendMail(ApexPages.StandardController stdCrlr){
   		if (!Test.isRunningTest()) { 
            stdCrlr.addFields(new List<String>{'CaseAssignedOwner__c'});

        }           
        this.c = (Case)stdCrlr.getRecord();
       
        
    }
    public Case getCase(){
        return c;
    }
   
     public PageReference send() {
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 

        // Reference the attachment page and pass in the case ID
        PageReference pdf =  Page.SendQA1747asPDF;
        pdf.getParameters().put('id',(String)c.id); 
        pdf.setRedirect(true);

        // Take the PDF content
        //Blob b = pdf.getContent();
         Blob b = Test.isRunningTest() ? Blob.valueOf('UNIT.TEST') : pdf.getContent();

        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(c.CaseNumber + '.pdf');
        efa.setBody(b);
        String[] toAddresses = new List<String>();
        Case_Owner__c[] assignedOwner = new List<Case_Owner__c>();

		System.debug(c.CaseAssignedOwner__c);
       assignedOwner =  [select  MemberEmail__c from Case_Owner__c where Queues__c = : c.CaseAssignedOwner__c ];
         for(Case_Owner__c em : assignedOwner){
            toAddresses.add(String.valueOf(em.MemberEmail__c));
         }
       System.debug(toAddresses);
       
        email.setSubject( c.Subject );
             
        email.setToAddresses( toAddresses );
        email.setPlainTextBody( c.Description );

        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

        // Sends the email
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   
        
        PageReference casePage = new ApexPages.StandardController(c).view();
        casePage.setRedirect(true);
        
        List<Task> tasks = new List<Task>();
        tasks.add(new Task(
    ActivityDate = Date.today(),
    Subject='Envoyé formulaire 1747 pour le Case ' + c.CaseNumber,
    WhatId = c.Id,
    OwnerId = UserInfo.getUserId(),
    Status='Complété - Completed'));

insert tasks;
        return null;
    }
    
}

 
Best Answer chosen by Kevin Languedoc
Pankaj_GanwaniPankaj_Ganwani
Hi,

When you validate any change set to Production then all the existing test classes run in the Production. There would be an existing class named as MassReassignOpportunitiesController that contains testReassign method. At line no. 216 there would be system.assert statement, please remove that statement and redeploy MassReassignOpportunitiesController with rest of the artifacts.

I hope it solves your issue.

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

Please insert the records for Case_Owner__c object as well in test class since you are framing toAddresses list in controller by iterating on Case_Owner__c object records. Make sure you populate the value for MemberEmail__c field.
Kevin LanguedocKevin Languedoc
How do you do that?
Pankaj_GanwaniPankaj_Ganwani
Hi,

Can you please let me know if you are asking me how to insert the Case_Owner__c records in test class? If so, then place the following code in test class after line no 10:

Case_Owner__c  obj = new Case_Owner__c();
obj.MemberEmail__c = 'test@gmail.com';
obj.Queues__c = 'klanguedoc@jdi.jubl.com';
insert obj;
Kevin LanguedocKevin Languedoc
As suggested i have added the code example to my test class. When I ran the test I got a success message in the Sandbox. However when I deploy to Production, I am still getting this error

Inbound deployment from Sandbox
System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc): [] 
Stack Trace: Class.CaseForm1747SendMail.send: line 51, column 1 Class.testCaseForm1747SendMail.testSend: line 29, column 1
Line 51 from the caseForm1747SendMail is:
 
// Sends the email
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
and line 29 from testCaseForm1747SendMail is
 
send1747.send();
I really appreciate your help. Your advice so far has been impeccable. I really hope you can help me resolve the deployment issue


 
Kevin LanguedocKevin Languedoc
User-added image

Here is the actual output from deployment from Sandbox to Production
Kevin LanguedocKevin Languedoc
Actually, I am now getting the error again in Sandbox test
 
FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc): []

 
Kevin LanguedocKevin Languedoc
I have added the following code to the actual class and re-ran the test class and received a success message
 
if(Test.isRunningTest()){
             toAddresses.add('klanguedoc@jdi.jubl.com');
  }
I will deploy to production and see if this resolves at least one of the errors
 
Kevin LanguedocKevin Languedoc
Now I am still getting this error message in deployment
 
System.AssertException: Assertion Failed 
Stack Trace: Class.MassReassignOpportunitiesController.testReassign: line 216, column 1
Isn't this a program from SFDC? It is not part of my deployment so why am I getting this error msg
 
Kevin LanguedocKevin Languedoc
User-added image
Pankaj_GanwaniPankaj_Ganwani
Hi,

When you validate any change set to Production then all the existing test classes run in the Production. There would be an existing class named as MassReassignOpportunitiesController that contains testReassign method. At line no. 216 there would be system.assert statement, please remove that statement and redeploy MassReassignOpportunitiesController with rest of the artifacts.

I hope it solves your issue.
This was selected as the best answer
Kevin LanguedocKevin Languedoc
Actually that didn't work but I also commented out the next line as well and then the test succeeded. I wa able to move into production. thanks a lot for al your help
 
// System.assert(controller.optyList.size()>0);
       
      //  controller.optyList[0].selected = true;