I have a plugin that is executed when a new instance from the sales order detail entity is created, it updates a field in the form. when creating the instance an error appears in the plugin trace log:
get Ref
productIdRef
de_prdrate: 19
de_salesprdrate: 19
System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]:
An error occurred in FollowUpPlugin. (Fault Detail is equal to
Exception details: ErrorCode: 0x80040265 Message: An error occurred in
FollowUpPlugin.
OriginalException: PluginExecution ExceptionSource: PluginExecution
Note: there is also a System workflow running on the sales order detail entity on the create event. the plugin works fine on the quote details entity without any exceptions or errors.
I think the problem is that my plugin and the system workflow work on the same event. I did not find any solution to this problem. I appreciate any suggestions.
The code is below:
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
namespace Orders_Plugins
{
public class Update_Salesdetails_Field : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
if (entity.LogicalName != "salesorderdetail")
{
tracingService.Trace("Target entity is not Sales Order Detail! plug-in was not registered correctly! Exit PlugIn", serviceProvider);
throw new InvalidPluginExecutionException("The current entity is not the Sales Order Detail entity");
}
else
{
tracingService.Trace("get Ref", serviceProvider);
var productIdRef = entity.GetAttributeValue<EntityReference>("productid");
tracingService.Trace("productIdRef", serviceProvider);
var productEntity = service.Retrieve("product", productIdRef.Id, new ColumnSet("de_prdrate"));
tracingService.Trace("de_prdrate: " + productEntity["de_prdrate"].ToString(), serviceProvider);
entity["de_salesprdrate"] = productEntity["de_prdrate"];
tracingService.Trace("de_salesprdrate " + entity["de_salesprdrate"].ToString(),
service.Update(entity);
tracingService.Trace("Update salesprdrate " + entity["de_salesprdrate"].ToString(),
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
tracingService.Trace("exception:", ex.ToString());
}
catch (Exception ex)
{
tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}