Hello,
you can use the below code in your plugin - and register the plugin on both SetState and SetStatedynamicEntity as two different steps.
string openOpportunity_count = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='opportunity'>
<attribute name='opportunityid' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
<condition attribute='parentaccountid' operator='eq' value='[account_GUID]' />
</filter>
</entity>
</fetch>";
EntityCollection openOpportunity_count_result = _service.RetrieveMultiple(new FetchExpression(openOpportunity_count));
if (openOpportunity_count_result.Entities.Count > 0)
{
throw new InvalidPluginExecutionException("The account cannot be deactivated because there are open Opportunities against it.");
}
Thanks and Regards.