You need to sign in to do that
Don't have an account?
Gwirizanani Sinoia
try catch block code coverage
Help
Trying to get code coverage for the code below. I am currently getting 69% on this triggger. I can not seem to cover the try cacth block. Please help
Thanks
trigger ContactTrigger on Contact (before update, after update) {
Contacthandler = ContactHandler.getHandler();
try {
if(Trigger.isBefore) {
if (Trigger.isUpdate) {
handler.checkmytest(Trigger.new);//check if dates have changed
handler.checkpeople(Trigger.new);//sort dates according to source and priority
}
if(Trigger.isAfter) {
if (Trigger.isUpdate) {
handler.individual(Trigger.new, Trigger.oldMap);
}
}
}
catch(DMLException e) {
for (Integer i=0 ; i<e.getNumDml() ; i++) {
Contact o = Trigger.newMap.get(e.getDmlId(i));
o.addError(e.getDmlMessage(i));
}
}
}
Trying to get code coverage for the code below. I am currently getting 69% on this triggger. I can not seem to cover the try cacth block. Please help
Thanks
trigger ContactTrigger on Contact (before update, after update) {
Contacthandler = ContactHandler.getHandler();
try {
if(Trigger.isBefore) {
if (Trigger.isUpdate) {
handler.checkmytest(Trigger.new);//check if dates have changed
handler.checkpeople(Trigger.new);//sort dates according to source and priority
}
if(Trigger.isAfter) {
if (Trigger.isUpdate) {
handler.individual(Trigger.new, Trigger.oldMap);
}
}
}
catch(DMLException e) {
for (Integer i=0 ; i<e.getNumDml() ; i++) {
Contact o = Trigger.newMap.get(e.getDmlId(i));
o.addError(e.getDmlMessage(i));
}
}
}
Hey there!
I would argue that there is no reason for this to be wrapped in a Try/catch block.
1) Wrapping your whole trigger in a "Try" is not best practice. Errors are ok, especially in a trigger when you need immediate feedback from the database that something failed. We don't want things to fail invisibly, so if a trigger fails, let it fail. The try/catch block in apex is more useful in a custom interface when you need to manage the user feedback yourself in your own UI (via a lightning compontent or visualforce for example)
2) This catch block is basically doing the exact same thing that the trigger would do if it failed anyway. All you are doing is adding the DML error to the contact, which is exactly what the DML failure does in a trigger.
So, take out the try/catch from your trigger and everything should work exactly the same way. As far as text coverage, you would have to cause something to fail the "Try" in order to test the "Catch". Take out the try catch and bingo, you're covered!
All Answers
Hey there!
I would argue that there is no reason for this to be wrapped in a Try/catch block.
1) Wrapping your whole trigger in a "Try" is not best practice. Errors are ok, especially in a trigger when you need immediate feedback from the database that something failed. We don't want things to fail invisibly, so if a trigger fails, let it fail. The try/catch block in apex is more useful in a custom interface when you need to manage the user feedback yourself in your own UI (via a lightning compontent or visualforce for example)
2) This catch block is basically doing the exact same thing that the trigger would do if it failed anyway. All you are doing is adding the DML error to the contact, which is exactly what the DML failure does in a trigger.
So, take out the try/catch from your trigger and everything should work exactly the same way. As far as text coverage, you would have to cause something to fail the "Try" in order to test the "Catch". Take out the try catch and bingo, you're covered!
@Jake Hebert 22 I will try your approach and see.
Thanks