+ Start a Discussion
lovetolearnlovetolearn 

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error..."

I created an apex class in my Sandbox. I got a code coverage of 81%. I pushed the Controller into my production. However, when I go to my production inbound and try to deploy it I get this error message: "Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []", Failure Stack Trace: "Class.extFindJob.testPageMethods: line 36, column 6 External entry point"

 

I understand the error. I am using a developer mode Sandbox, so the records from my objects are not pulled. But, I cannot bypass setting a test value for my Account__c field its a mandatory field.

 

Please help me find a solution. Thank you for any help.

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

In your testmethod. In order to deploy to production, you need to create a test method, since you said you had 81% coverage, I assume that you have this.  prior to executing your controller code, execute the DML to create an account, it will only exist in the scope of the testmethod. 

Something like this:

 

Account testAccount = new Account(Name='test');
insert testAccount;

 You may need more fields defined depending on your required fields and validation rules.

Then, in your controller, instead of leaving the Account__c blank, set it equal to testAccount.id 

 

Post your testmethod code if you have more questions.

All Answers

JimRaeJimRae

You either need to do a SOQL query for an account that you could reference, or create an Account as part of your test method.  Creating the account is likely the best way to insure portability of your testmethod from sandbox to production.

lovetolearnlovetolearn

Create an account in my Sandbox?

JimRaeJimRae

In your testmethod. In order to deploy to production, you need to create a test method, since you said you had 81% coverage, I assume that you have this.  prior to executing your controller code, execute the DML to create an account, it will only exist in the scope of the testmethod. 

Something like this:

 

Account testAccount = new Account(Name='test');
insert testAccount;

 You may need more fields defined depending on your required fields and validation rules.

Then, in your controller, instead of leaving the Account__c blank, set it equal to testAccount.id 

 

Post your testmethod code if you have more questions.

This was selected as the best answer
lovetolearnlovetolearn

Thanks a lot.