You need to sign in to do that
Don't have an account?
Kevin 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
Actual class : works fine
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; } }
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
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.
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;
Inbound deployment from Sandbox Line 51 from the caseForm1747SendMail is:
and line 29 from testCaseForm1747SendMail is
I really appreciate your help. Your advice so far has been impeccable. I really hope you can help me resolve the deployment issue
Here is the actual output from deployment from Sandbox to Production
I will deploy to production and see if this resolves at least one of the errors
Isn't this a program from SFDC? It is not part of my deployment so why am I getting this error msg
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.