when i trying to insert opportunity in my testclass i am getting an error

   Test_opportunityTrigger.Test_opportunityTrigger: line 24, column 1
   22:28:17.312 (312087000)|FATAL_ERROR|System.DmlException: Insert failed. First      exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]:        [Name]

But i have name field in opportunity My code :

            Opportunity op = new Opportunity();
    op.RecordTypeId = rt.Id;
    //op.AccountId = ac.id;
    op.Name = 'OppName';
    op.City__c = 'OppCty';
    op.Postal_Code__c = '12345';
    op.State__c = 'opp_sta';    
    op.Copy_Account_BA_to_SA__c = true;
    op.Street__c = 'OppStr';
    op.CloseDate = date.today();
    op.StageName = 'No Decision';
    op.Email_Field__c = 'xyz@gmail.com';        
    insert op;

Insert op is at line 24. Do anybody have any idea.

  • Can you show the whole trigger, at least up to the relevant line? Feb 5 '13 at 11:57
  • BTW are you using the string literal 'OppName', or assigning that from something else (which may be null)? Feb 5 '13 at 11:58
  • One more thought check whether there is before insert trigger thats nullifying the value Feb 5 '13 at 12:16
  • MohithKumar the trigger is - after delete, after insert, after undelete, after update, before delete, before insert, before update
    – AnuRaj
    Feb 5 '13 at 12:22
  • My suggestion is to put a debug & check whether op is inserting or not
    – Eagerin Sf
    Feb 5 '13 at 12:44

Is it possible that your trigger, being a before insert, somehow nulls out the Name field before the insert happens?

If that is not happening, I would also check to see if you run any DML statements on other objects during your before insert trigger. This error may not be related to the opportunity but rather a separate object that is being created in your before insert trigger code that has a null Name field.

Also, as Peter pointed out below, this could also be a workflow rule. If for some reason you have a workflow rule that nulls out your Name field after it has been originally saved, your before/after triggers and validation will run again. This issue could pop up in this scenario as well.

If none of these are correct, update your question with your trigger code and let us take a look.

  • It could also be a workflow rule blanking it out. This makes it tricky because the first time through the trigger it is not null, then the workflow fires blanking it out, then the trigger executes again and it is blank. See triggers and order of execution. Feb 5 '13 at 13:18
  • 2
    The validation rules are run before the change is even saved to the server (note: not committed). This is where this type of error is caught. Notice Step 4 of your link. Feb 5 '13 at 13:22
  • 1
    I was looking at step 11: If the record was updated with workflow field updates, fires before and after triggers one more time (and only one more time), in addition to standard validations. So, before (3) and after (6) triggers run and the field is fine, then workflow rule fires (9) blanking it out, then before and after again and standard validations (11) run again and it is blank from the workflow rule firing. Feb 5 '13 at 13:28
  • Looks like you are right, my apologies. Feb 5 '13 at 13:30

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.