+ Start a Discussion
AngelikaAngelika 

How to Prevent Apex Code Breaking in Production

I am new to Apex Coding. Besides an unmanaged Clone List App and many Managed Apps , my Salesforce Org does not have any custom Apex Code in it.

 

I tried to Deploy an Apex Class to production and got the error on a test class, Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, </BR></BR></BR><font color="blue">Redirecting in just a second.</font></BR><SCRIPT LANGUAGE="JavaScript">window.location = "../apex/SFSSDupeCatcher__SFSSDupeCatcher?dId0=MDAxRjAwMDAwMG5TWHg4SUFH&scId=YT...

 

I know how to fix this error (change validation rules on my Dupe Catcher App Temporarily.

 

This "quick fix" works for deploying my code, but what about when my Apex Code stays in production.

 

If new validation rules, either from DupeCatcher or for Contacts, Accounts etc are put into place after my code in deployed in production , this will break my test data. What are the implications of test data/classes/methods breaking in production? I know I won't be able to deploy new code, but is there anything else that happens, particulary to the code that the test method/class/data was written for? Will test methods that are broken break the classes they cover?

 

If you were just begining Apex Development in your organization, what would you do differently? How can Apex Code break in production? How can I prevent this?

 

Thanks,

 

Angelika

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
kibitzerkibitzer

In most cases, new validation rules will only break the test code. That's because what they usually break is your creation of objects to use in testing - which is part of the test code, not the actual production classes.

 

However, if your production classes create objects, or even change field values that violate the new validation rules, your production code will break as well.

 

That's why unit tests are so important. They aren't used just for deployment - they are used for regression testing. From a developer perspective, when called in to look at a problem, the first thing to do is run all of the test classes on the organization to find out if someone has added a validation rule or workflow that broke the application code.

All Answers

kibitzerkibitzer

You can't prevent it. That's part of the challenge of Apex.

 

However, you can minimize the problem by using centralized object creation on all test classes. That way you only have to add the required field once. If you're creating a managed package for distribution, there are additional steps you can take to (in many cases) eliminate the need to disable validation rules on deployment.

 

I cover this subject at some length in my new book :-)

AngelikaAngelika

But...what are the implications of test data/classes/methods breaking in production? I know I won't be able to deploy new code, but is there anything else that happens, particulary to the code that the test method/class/data was written for? Will test methods that are broken break the classes they cover?

 

For instance, if my test class for a commission process breaks because of a new validation rule was put in production, does my commission class/process break as well?

 

Thanks!

kibitzerkibitzer

In most cases, new validation rules will only break the test code. That's because what they usually break is your creation of objects to use in testing - which is part of the test code, not the actual production classes.

 

However, if your production classes create objects, or even change field values that violate the new validation rules, your production code will break as well.

 

That's why unit tests are so important. They aren't used just for deployment - they are used for regression testing. From a developer perspective, when called in to look at a problem, the first thing to do is run all of the test classes on the organization to find out if someone has added a validation rule or workflow that broke the application code.

This was selected as the best answer
lakslaks

Hi,

 

I also came across the same problem about code breaking because of validation rules.

 

The solution for that would be to modify the corresponding Test class code and re-deploy it to Production right ?

 

But is there any way to ensure such things do not happen in Production. What is the recommended way to avoid such situations ?

Would it be to develop the validation rules also in Sandbox, Run tests and confirm that classes using it are also corrected and then deploy it all to Production ?

 

 

Regards,

Lakshmi.

kibitzerkibitzer

Absolutely you should always test validation rules and workflows in a sandbox before deploying to produciton.

 

The problem is that unlike Apex code, Salesforce does not require you to do this. But it's definitely a best practice.

 

Dan