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
Muthouazhagi DhanapalMuthouazhagi Dhanapal 

Code coverage problem - Please help !

Hi , 

I am trying to deploy my code to production. 

I just have one Apex trigger that I want to deploy, and I have written a test class to test the Apex trigger. 

I was able to achieve 100% test coverage for the Apex trigger.

But production has several other Apex classes , I am getting only 62% test coverage in Production. 

Is this because I also need to write test classes to test the other apex classes? The overall code coverage is affecting?

I am very confused. Someone, please help me with this.

Thanking you in advance.

AbhishekAbhishek (Salesforce Developers) 
The first thing to check is your test cases. Here are the things I check when receiving this problem:

Each class is set to SeeAllData=false This is a big one, if your class is set to SeeAllData=true and you're relying on data from the org during the testing phase instead of building your own, it'll fail due to differences in the org. The best practice here is to build all of your test data individually in your test cases.

Make sure no try/catch blocks are allowing code to slip through. This can change the course of the test case if you're running try/catch in the test case but ignoring the catch section. Allowing the test to bypass like that will reduce the amount of code covered as well. Test classes are the one place to really not use try/catch religiously.

Make sure your test cases are using System.runAs and setting up a user in the proper profile. Certain sections of your code might be skipped if the running user doesn't have access to specific fields, objects, etc. It is best to always configure a user in the test class with the proper profile to ensure this issue doesn't happen. This can also happen with feature licenses like Knowledge where certain objects aren't available unless that feature license is enabled at the user level. Usually, that'll throw a specific error to that, but you could run into code that tries/catches around it, and causes lowering of the test coverage levels.

The biggest thing is to remember that you're moving your data to a different org with a different user, different permissions, etc. Hopefully, your testing and prod Org's are nearly in sync, but that isn't always the case. Comb the code and make sure nothing in there is relying on that other org.


For further reference, you can check this too,

https://salesforce.stackexchange.com/questions/41755/is-the-75-code-coverage-requirement-to-deploy-to-production-per-class-or-overal


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.