• Sushma Kamisetty
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi,

Bear with me, I'm new to this and I expect it's a simple problem, but it has stumped me.

I'm trying to write a unit test for a trigger that fires after inserting a record in a custom object, and updates some data from the custom object into the standard Contact object.  The trigger works fine when I test it in the sandbox, and updates everything as I want, but I can't manage to write the unit test so I can deploy it to production!

In order to write the test I assume I need to create a test contact record for the trigger to update, and have written code to do this.  However, when I run the test, it fails at the point of inserting the test contact record with the folowing errors:

Class.SBCopyResToContactTest.TestContUpdate: line 13, column 1
and
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Failed to create Account for Contact null TestName. Record Type ID: this ID value isn't valid for the user: 012b0000000DiUUAA0: []

The code in question is:

12: Contact testCon = new Contact(LastName = 'TestName');
13: insert testCon;

The infuriating thing is that if I run those two lines of code in the 'execute anonymous' window they work fine, and create the contact record no problem, in the sandbox and in the production org.  But in the apex unit test they fail.

We are a non-profit organization, and are using the Non Profit Success Pack with Household account types.  From what I can make out from that error message it looks as if it may be trying to create the account with the wrong record type (that I'd is certainly not the correct one for the account records.  But as I said, if I exceute the code outside the unit test it runs, and the account is created with the correct record type.  The other, worse case, scenario is that it's a custom code or validation (we've had some outside development done).

Any suggestions would be much appreciated, even a hit as to why it works 'anonymously' and not in the test would help!