I am writing a test class. When i run my test class i am getting an error

System.DmlException: Insert failed. First exception on row 0; first error:   REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Class.TestCVFC_productToInstalledItems.TestCVFC_productToInstalledItems: line 20, column 1

but my code contains the name field

    Opportunity opp = new Opportunity();
    opp.Name = 'OppName';
    opp.Accountid = ac.id;        
    opp.Copy_Account_BA_to_SA__c = true;
    opp.Street__c = 'OppAddress';
    opp.StageName = 'Qualification';
    opp.CloseDate = date.today();
    insert opp;

insert opp is on the line no 20.

2 Answers 2


It seems likely that there is an insert trigger on Opportunity that is trying to insert another record.

Check for any triggers on Opportunities.

You can look in the debug log that gets created when the test is run to see what is occurring. With a sufficient logging level on the database you should see the record being inserted.

  • I've had this issue where I get the required field missing error on a specific field on a custom object. This field was deployed as a required field but was changes to not being required after a day. However, we're still getting this error once in a while on new records being inserted. I haven't been able to find out why this is happening. Please help if you have ideas as to what may be causing this. Thanks!
    – Bahman.A
    Sep 17, 2020 at 20:06

I agree with Daniel on checking for triggers doing some other sort of insert.

However, if you are positive that the error is with the Opportunity that you are inserting, I suggest taking a look at any workflow rules. You may have a workflow rule that is setting required fields to blank for some reason.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .