private class Test_ordinefermo {

    static void test_newAttachment(){
            Opportunity op= new Opportunity();
            op.Name= 'abc';
            op.Trattativa__c = 'Automotive';
            op.CloseDate = date.valueOf('2017-11-12');
            op.Tipo_di_Trattativa__c = 'Cross Selling';
            insert op;
            file_ordine_fermo__c fofc = new file_ordine_fermo__c();
            fofc.Name = 'fofcdemo';
            fofc.Trattativa_Correlata__c =op.Id;
            insert fofc;
            ApexPages.StandardController std = new ApexPages.StandardController(fofc);
            ordinefermo ord= new ordinefermo(std);

I have above test class testing a extention ordinefermo. when I try to test newAttachment() method is giving me

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

But I have already populated Trattativa_Correlata__c field in my test class. When I try to deleting the statement ord.newAttachment(); the test case is being successful but I am falling behind code coverage as I am not testing the method.

  • did you check all workflows, processes, triggers on Opportunity? Seems like some automation tool clear value of Trattativa_Correlata__c field. Can you highlight line, where this exception occurs ? Nov 28, 2017 at 12:42
  • what is the Data Type of "Trattativa_Correlata__c" field? Nov 28, 2017 at 12:44
  • It is a master look up to an opportunity object
    – prasad
    Nov 28, 2017 at 12:47
  • @AlexanderBerehovskiy ord.newAttachment(); is the line where Iam getting the error
    – prasad
    Nov 28, 2017 at 12:55
  • 1
    Please edit our post to add any clarifications (especially code). That's not what comments are for.
    – Adrian Larson
    Nov 28, 2017 at 14:57

1 Answer 1


Id for the opportunity is not passed to controller. Your method is getting id from apex page parameter. So you need to pass opportunity id to your page. Try this in your test method before calling newAttacment():

  • @prasad Did it work ?
    – A. Shah
    Dec 1, 2017 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.