locked
Is it valid to Catch Exceptions in Plugins and attempt to retry? RRS feed

  • Question

  • Here is my setup:

    CRM Online 2015.1

    Custom Entities: Foo, FooNumberSettings

    I have plugin that fires on the create of a "Foo", Pre-Operation.  Within the plugin, there is a Try Catch statement that queries the FooNumberSettings, then increments an integer in FooNumberSettings, and performs an Update to the FooNumberSettings. If an exception occurs and there is an error that contains the message "My Custom Duplicate Message", the Try Catch is retried:

    while(true) {
    	try {
    		var settings = GetSettings(service);
    		settings.new_foo = settings.new_foo + 1;
    		service.Update(settings);
    		break;
    	}
    	catch(Exception ex){
    		if(ex.Message.Contains("My Custom Duplicate Message"){
    			continue;
    		}
    		throw;
    	}
    }

    I have plugin that fires on the update of the "FooSettings", Pre-Operation.  It compares target's new_foo value to a pre-image's new_foo value. If the new_foo's value from the target is equal to, or less than the pre-image's, the an exception is thrown with the message "My Custom Duplicate Message".

    Every now and then, due to multiple plugins attempting to update FooSettings simultaneously, I'll throw my "My Custom Duplicate Message" Exception.  In my logging, I see it being caught, then an attempt is retried.  When this attempt is retried, an exception is thrown with this message:

     - There is no active transaction. This error is usually caused by custom plug-ins that ignore errors from service calls and continue processing.

    If I change the On Create plugin for Foo to be Pre-Validation, rather than Pre-Operation, everything works flawlessly.

    So my question is, is this a bug, or can you not catch exceptions within a Transaction and retry?

    Wednesday, April 27, 2016 12:35 AM