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
jbroquistjbroquist 

Odd Opportunity CloseDate Issue in unit tests

I'm working on a trigger for the Opportunity object that creates a few child "Commission" records when an opportunity is marked as  "Closed Won". The issue comes from my unit test that reopens and record that was marked as "Closed Won". 

 

Here is the error:

System.DmlException: Update failed. First exception on row 0 with id 006Q0000009MdFOIA0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [CloseDate]: [CloseDate]

The error occurs on the last update to the opportunity StageName that essentially reopens the deal.  I'm really banging my head against the wall on this one since it's pretty clear I'm setting the CloseDate when I insert the record.

 

//...insert other required records for test

 //create opportunity
Opportunity opportunity = new Opportunity();
opportunity.AccountId = account.Id;
opportunity.OwnerId = ramUser.Id;
opportunity.Name = 'Test Opportunity';
opportunity.StageName = 'First Appointment';
opportunity.RecordTypeId = [SELECT Id FROM RecordType WHERE SObjectType='Opportunity' AND Name='RAM Opportunity'].Id;
opportunity.TotalCash__c = 3000.00; //creates positive revenue so commissions can be calculated
opportunity.CloseDate = system.today();
insert opportunity;

//update opportunity to closed won
opportunity.StageName = 'Closed Won';
update opportunity;

Test.startTest();

//update opportunity again to 'In Processing' - reopen the deal
opportunity.StageName = 'In Processing';
update opportunity;

//...assertions

Test.stopTest();

 Any ideas as to why this may be happening?

imranrazaimranraza

Hi jbroquist,

//update opportunity to closed won
opportunity.StageName = 'Closed Won';
update opportunity;

Replace obove lines with

 

//update opportunity to closed won
opportunity.StageName = 'Closed Won';

opportunity.CloseDate = system.today();

update opportunity;

jbroquistjbroquist

Yeah I've already tried that... I get the same error.

Rajesh SriramuluRajesh Sriramulu

Hi

 

Try this,

 

opportunity.StageName = 'Closed Won';

opportunity.CloseDate = system.today()+1;

update opportunity;

 

 

Regards,

Rajesh.

jbroquistjbroquist

Same error Rajesh. This is really stumping me. I've never had this happen before...

Rajesh SriramuluRajesh Sriramulu

Hi

 

 

Sorry for delay can u post ur updated trigger and test class.

 

Regards,

Rajesh.