I have a plugin that is executed when a new instance from the sales order detail entity is created. 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]: The product and the unit cannot be updated while the entity is locked. (Fault Detail is equal to Exception details:
ErrorCode: 0x80040265
Message: The product and the unit cannot be updated while the entity is locked.
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:
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");
}
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"]);
entity["de_salesprdrate"] = productEntity["de_prdrate"];
tracingService.Trace("de_salesprdrate " + entity["de_salesprdrate"]);
service.Update(entity);
tracingService.Trace("Update salesprdrate " + entity["de_salesprdrate"]);
}
catch (FaultException<OrganizationServiceFault> ex)
{
tracingService.Trace("exception:", ex.ToString());
tracingService.Trace(ex.StackTrace);
if (ex.InnerException != null)
{
tracingService.Trace("inner exception:", ex.InnerException.ToString());
tracingService.Trace(ex.InnerException.StackTrace);
}
throw new InvalidPluginExecutionException(ex.Message);
}
catch (Exception ex)
{
tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
tracingService.Trace(ex.StackTrace);
throw;
}
}
}
}
In the post operation stage of the plugin pipeline for message "Update" the system blocks modifications to the entity
object that is subject of the plugin pipeline. (This is the entity that was retrieved from tne context.InputParameters
collection.)
Instead the plugin should be registered on the pre validation or pre operation stage. In these plugin pipeline stages attributes can be modified on entity found in context.InputParameters["Target"]
.
The entity object must not be updated explicitly, so remove this line:
service.Update(entity);