+ Start a Discussion

Unit Test for Trigger on Opportunities: Fails due to required field closedate

New to this and confused.  Have a trigger for after insert/update on opportunities but cannot get the unit testing passed due to closedate being a required field.  no matter what format i use, the same error always is returned - namely: REQUIRED_FIELD_MISSING [CloseDate]: Close Date.
Please could someone assist - what should i put closedate = to?
Box @ HomeBox @ Home


My test classes usually use something like:

sOpportunity.closedate = System.today() + 7;

It really depends on what functionality your testing in your APEX code and what your business processes are.

Hello pru1,

If you are not using the close date within your code then you can also place in a base date of something like 2010-01-01.
I usually use CloseDate=Date.newInstance(2010,02,02).  This way you can set it to whatever you want.


Thanks for the suggestions however I still get the same error.

The test code I'm using is as follows:

public class Tests {

static testMethod void testUpdatePrincipleLivesonWonStageTrigger() {

Opportunity o = new Opportunity(name='Test 1', stagename='Activated (Won)', employer__c = 'a0020000002AG4XAAW', Employer_Broker_Combination_Key__c = 'EBCK', CloseDate=Date.newInstance(2010,02,02));

insert o;

o.Name = 'Test 1 updated';

o.Stagename = 'Lost';

update o;



I've tried all three suggestions for closedate = (mentioned above) but it doesnt seem to be recgonizing the closedate field.

There are no errors when I save, however when I run the test the following error is returned under Failure and Warnings:

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [CloseDate]: [CloseDate]

Would appreciate any further suggestions