You need to sign in to do that
Don't have an account?
Kathleen Palmer
Apex trigger 0% code coverage will not deploy in Production.
Hi! I finally got this simple Apex Trigger working in Sandbox:
Then I went to deploy in production and I received this error "Code Coverage Failure The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.restrictFileDeletion.
I have read all related threads and Trailhead, and I'm still stuck on how to write a test class to get this to deploy in production. I would be so appreciate of any help to get this working. I was so excited to get this working in Sandbox, now I've wasted hours, and it's still not working in production. Thanks in advance!!
trigger restrictFileDeletion on ContentDocument (before delete) { String profileName=[SELECT id,Name FROM Profile WHERE Id =:UserInfo.getProfileId()].Name; for (ContentDocument cd : Trigger.old){ If(profileName=='ALS Community v6'){ cd.addError('You cannot delete this record!'); } } }
Then I went to deploy in production and I received this error "Code Coverage Failure The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.restrictFileDeletion.
I have read all related threads and Trailhead, and I'm still stuck on how to write a test class to get this to deploy in production. I would be so appreciate of any help to get this working. I was so excited to get this working in Sandbox, now I've wasted hours, and it's still not working in production. Thanks in advance!!
Adding to the above answers, a test class make sure your code is working properly as you wanted.
An ideal test class has 3 parts:
- A data preparation where you create your test data which will only beused in the context for testing and not saved to the DB
- Calling the function you want to be tested (For apex classes) or the DML operation which invokes the apextrigger
- Comparison of the expected and the original results
Salesforce requires a test class with a well coverage before deploying to production to make sure your code doesnt break at any point in the production.For your trigger you can refer the following test class:
Best Regards
Eldon
All Answers
I have created trigger like this one: And I have created unit test for this trigger like this: You just need to create fake record then start test (Test.startTest()), try to update it (update testAccount), and verify the result (System.assert) and stop the test (Test.stopTest()).
Test data is never saved into the system and you cannot access existing objects in the org unless you use @isTest(SeeAllData=true) which generally you want to avoid if possible (which is not always possible). You see the exmaple that Yuri Aldanov made for you, they created a new account outside of the test, then started the test, made the change to that account which then makes the trigger fire and does what it was built to do. Then they use System.assert to verify that the trigger made the required changes. If not then the test fails. When something in the system changes that makes this trigger no longer work or someone tries to deploy something that conflicts with the trigger making it fail, the System.assert will return as a failure so you know something is wrong and prevent the deployment.
I hope this all helps clear things up a little. It was very confusing for me at first too. It gets even more complicated when you start making test classes for visualforce controllers and so on. GL!
Adding to the above answers, a test class make sure your code is working properly as you wanted.
An ideal test class has 3 parts:
- A data preparation where you create your test data which will only beused in the context for testing and not saved to the DB
- Calling the function you want to be tested (For apex classes) or the DML operation which invokes the apextrigger
- Comparison of the expected and the original results
Salesforce requires a test class with a well coverage before deploying to production to make sure your code doesnt break at any point in the production.For your trigger you can refer the following test class:
Best Regards
Eldon
I have gone through your issue and came up with a solution.
The thing is that you need to define a different profile name(which will be your profile name) when you are testing.
Please try the updated trigger I have created with the test class for it. Both mentioned below -
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com