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
MarkLevyMarkLevy 

Apex Test works in Sandbox but fails in deploy to Production??

This seems to be happening to me now where I have certain tests that work just fine in my Sandbox but fail assertions when deploying to Production thus failing the test. It seems to be happening all of a sudden on certain tests and I have no idea why.

All of my tests use @isTest(seeAllData=false) so there shouldn't be a problem between Sandbox VS. Production data.

This particular test that is failing is an update on a lead that should be updating properly, but the assertion shows that the update failed. I'm creating users in the test as well and using System.runAs so that shouldn't be different between Sandbox and Production either.

In my Production I have it set to run Apex tests one at a time, so that shouldn't be interfering with my tests either.

I really have no idea what's going on here.
Any assistance as to why a test using only test data would work in Sandbox but fail in Production would be helpful.

I could post my code but it's very very long, and it could be something else on the Production server that is causing the check if update worked assertion to fail as well so I can't very well post all the Apex code on my server. The most helpful thing for me would be strategy on what could be wrong and how to go about investigating this further. Is there some specific Lead Update Failed reason in a log somewhere I can look up or something that I can do shed more light on why this is happening?

Thanks!
Best Answer chosen by MarkLevy
MarkLevyMarkLevy
So I added some "debugging" to my code, since I am able to see the test failure message I can put the error directly there using the method in my code below:
 
errorThrown = false;
			//try updating B2B lead as the B2B User - should work
			String debugStr='';
			try {
				updB2BLead.LastName = 'b2blUpdated';
				update updB2BLead;
			} catch (DmlException e){
				for(Integer i=0;i<e.getNumDml();i++){
					debugStr += e.getDmlMessage(i)+' | ';
				}
				errorThrown = true;
			}
			finally
			{
				// Verification
				System.assert(errorThrown==false, 'DEBUG: '+debugStr);
			}

I go through the getDmlMessage and put it into the debugStr and then print the debugStr on the assert which is one of the few messages I can see when the deployment fails.

Doing this I discovered that it was failing because of a validation rule. This is very strange though because I have that same validation rule on my Sandbox. Anyway I think that now that I have clearer error messages I can figure this out. Thanks!

All Answers

Nayana KNayana K
One reason can be configuration is differing between sandbox and production.  May be you have an extra work flow / process / flows which causing extra effect 
Amit Chaudhary 8Amit Chaudhary 8
Can you please post what error you are getting ?
 
MarkLevyMarkLevy
It's not an error per-se. It's just that the lead can't be updated (update fails). Is there a way I can find out more details about a lead update failed error? A more detailed error message than just seeing that it didn't work?
MarkLevyMarkLevy
So I added some "debugging" to my code, since I am able to see the test failure message I can put the error directly there using the method in my code below:
 
errorThrown = false;
			//try updating B2B lead as the B2B User - should work
			String debugStr='';
			try {
				updB2BLead.LastName = 'b2blUpdated';
				update updB2BLead;
			} catch (DmlException e){
				for(Integer i=0;i<e.getNumDml();i++){
					debugStr += e.getDmlMessage(i)+' | ';
				}
				errorThrown = true;
			}
			finally
			{
				// Verification
				System.assert(errorThrown==false, 'DEBUG: '+debugStr);
			}

I go through the getDmlMessage and put it into the debugStr and then print the debugStr on the assert which is one of the few messages I can see when the deployment fails.

Doing this I discovered that it was failing because of a validation rule. This is very strange though because I have that same validation rule on my Sandbox. Anyway I think that now that I have clearer error messages I can figure this out. Thanks!
This was selected as the best answer