October 31, 2019

"Custom plug-ins should not catch exceptions"... that's new.

The title refers to an error message I got today in one of my plugins, the complete text is the following:

ISV code reduced the open transaction count. Custom plug-ins should not catch exceptions from OrganizationService calls and continue processing.

I googled the message and I found the explanation inside the following page: Troubleshoot plug-ins.
The reason is simple: inside my code I wrapped an IOrganizationService.Update inside a try catch block:
try
{
    Entity updateRecord = new Entity(recordRef.LogicalName, recordRef.Id);
    updateRecord[fieldName] = fieldValue;
    service.Update(updateRecord);
}
catch (Exception ex) {
    // ...
}
Why I did that? In my case the specific update may or may not fail (due to the status of the entity and the field that I need to update) but I needed to make sure the plugin containing the above code should never fail.
I don't have a specific workaround to resolve this situation because it really depends on the logic you have inside the plugin (in my case one of the options is to create an additional async plugin just to perform this update so it will not affect the main plugin).
The page mentions that this error has been added recently, so if in your existing plugins you have an IOrganizationService call wrapped inside a try catch block, the plugin is probably failing right now.

UPDATE: looks like that wrapping an IOrganizationService call was never possible, but the platform before returned a non meaningful error message (like "no active transaction found"), at least now we are notified with the exact reason. Thanks Tanguy Touzard and Jonas Rapp for the correction!

1 comment:

  1. Hi. Thanks for sharing as it helped me work through this some time ago. This can be done on async plugins. See my write-up for more specifics and use case. Hopefully it will be helpful to others with the same requirement:

    http://helpfulbit.com/handling-exceptions-in-plugins/

    ReplyDelete