function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jon SkinnerJon Skinner 

Cannot create data in unit test

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!
Sushma KamisettySushma Kamisetty
Hi Jon,

As per the Exception, the Contact record should have a RecordType ID. 
Can you check if the Contact object has Recordtypes defined and if so, try the following code instead:
  1. ID rtid = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('/*recordtype name in contact object*/').getRecordTypeId();
  2. Contact testCon = new Contact(LastName = 'TestName',recordtypeid = rtid );
  3. insert testCon;
As per my knowledge the 'execute anonymous' window runs in User Mode where as the Test Class executes in System Mode and that might be the reason why it worked anonymously.

Hope this helps!
 
Leo10Leo10
Hi think you have to mention account id for contact
Contact testCon = new Contact(LastName = 'TestName',Accountid= testAccount.id);
 insert testCon;

Thank you
 
Jon SkinnerJon Skinner
Hi,

Thanks for your suggestions, but we don't have record types defined for contacts, and I've already tried creating an account for the test contact without any luck.
I think I've established that the problem is with the record type for the account the system tries to create.  The record type id the error references (012b0000000DiUUAA0) is for an innactive record type.  I just tried reactivating that account record type in the sandbox, and the test runs perfectly with 100% code coverage!  But that's just in the sandbox...
I may have to look into making the test run as my user.  The development work I mentioned has involved some rather arcane configurations involving user access and security.
But I will have to try and track down what's using that record inactive record type!