Find your content:

Search form

You are here

Required field missing error in test case

 
Share

I'm writing a test case. When I save I get the following error message:

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

These are the two relevant lines of code:

Show__c s = new Show__c(Venue__c=a.id,Host__c=h.id,Active__c=true,Players_for_Max__c=100,Rate_Card__c=r.id,Min_Rate__c=110,Max_Rate__c=250);
insert s;

The error message corresponds to the insert line. Note that the fields that it claims are missing are parameters to the constructor. In fact, all of the fields in the constructor are required and none of the others are saying they are missing. There are no triggers that overwrite the "missing" fields either. The only thing I can think of is that the two fields are of type Currency and perhaps I'm not setting it correctly.


Attribution to: eyedar

Possible Suggestion/Solution #1

There's a lot that goes on with an insert. I suggest using the debug log to see what the values are along the way. For example you can see what the exact values are when a workflow is firing.

Another possibility is that if you are developing in an IDE, the code on the server that is actually getting executed does not match what you are viewing in your IDE. Double check that by viewing the actual class in Salesforce.


Attribution to: Peter Knolle

Possible Suggestion/Solution #2

The code you paste looks no problem to me. It can't be wrong with only the two lines. So the problem is caused by the other parts in your system.

I duplicate the comment by rao and Peter Knolie, the error can cause by the other classes that related to the controller and test class.

So, try to run the anonymous code in the Developer console, it would be the most quick way to identify the root-cause.


Attribution to: Cray Kao

Possible Suggestion/Solution #3

Each org will make different fields required for each standard and custom object. If one of those objects is referenced in the object you're inserting in your test class, when you create the referenced object, it must also satisfy the requirements set for the org when it comes to required fields. Otherwise, any validation rules that fire when your test class is run, will cause your test class to fail.

As others have pointed out, very often the line number given where a test class fails is not the line number that's causing the problem, it's simply where the problem caught up with you. My recommendation is that you query all the objects you're creating in your test class to get the required fields for each of them.

A good example might be with Opportunity. Opportunity requires an Account. So any fields required by your org for a new Account must be included when you create the Account(s) used in your test class. You'll also need Contacts for an Opportunity. So any required fields for Contacts will also be required when you create them. The same would apply to the User that's going to be the Owner of the Opportunity. If you don't have all the required fields for the related objects, in addition to the required fields set by your org for Opportunity, your test class for a trigger, class, controller, etc that fires on Opportunity is destined to fail.


Attribution to: crmprogdev
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32357

My Block Status

My Block Content