2016-02-26 46 views
2

我在使自定义工作流程活动工作时遇到问题。我已经剥夺它回到最简单的代码:CRM 2015自定义工作流程活动,出现错误:实体是只读的,“Id”属性无法修改

// TODO: Implement your custom Workflow business logic. 

// Get the context 
var xrmContext = new OrganizationServiceContext(service); 

// Get an object that we can update 
var client = (from a in xrmContext.CreateQuery("account") 
    where a.GetAttributeValue<Guid>("accountid") == new Guid("bfa273d1-d34c-e511-80cc-00155d021c08") 
    select a).Single(); 

// Make a change 
client.Attributes["name"] = "Gray Test A - CHANGED"; 

// Write it out 
xrmContext.UpdateObject(client); 
xrmContext.SaveChanges(); 

如果您有创建工作流程,你会看到,它并没有比这更简单:

  • 获取上下文
  • 获取帐户记录
  • 做出最简单的更改
  • 更新并保存。

我得到跟踪日志中出现以下错误:

[2016-02-26 15:15:09.328] Process:CrmAsyncService |Organization:00000000-0000-0000-0000-000000000000 |Thread: 36 |Category: Platform |User: 00000000-0000-0000-0000-000000000000 |Level: Error |ReqId: 00000000-0000-0000-0000-000000000000 | ExceptionConverter.ConvertMessageAndErrorCode ilOffset = 0x23B 
> System.InvalidOperationException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #5CC91FB6: System.InvalidOperationException: The entity is read-only and the 'Id' property cannot be modified. Use the context to update the entity instead. 
> at Microsoft.Xrm.Sdk.Entity.set_Id(Guid value) 
> at Microsoft.Crm.Extensibility.ProxyPluginExecutionContext.CopyModifiedEntityAttributes(PipelineConversionContext context, Entity originalEntity, Entity newEntity) 
> at Microsoft.Crm.Extensibility.ProxyPluginExecutionContext.ConvertPropertyBagToParameterCollection(PipelineExecutionContext context, PropertyBag propertyBag, ParameterCollection originalParameterCollection) 
> at Microsoft.Crm.Extensibility.ProxyPluginExecutionContext.Dispose(Boolean disposing) 
> at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.GetImages(PipelineExecutionContext context, Object pluginDefinition) 
> at Microsoft.Crm.Extensibility.ImageRetrievalStep.MergeEntityRequests(PipelineExecutionContext context, Dictionary`2 entityRequests) 
> at Microsoft.Crm.Extensibility.ImageRetrievalStep.Execute(PipelineExecutionContext context) 
> at Microsoft.Crm.Extensibility.PipelineInstrumentationHelper.Execute(Boolean instrumentationEnabled, String stopwatchName, ExecuteWithInstrumentation action) 
> at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context) 
> at Microsoft.Crm.Extensibility.MessageProcessor.<>c__DisplayClass1.<RunStage>b__0() 
> at Microsoft.Crm.Extensibility.PipelineInstrumentationHelper.Execute(Boolean instrumentationEnabled, String stopwatchName, ExecuteWithInstrumentation action) 
> at Microsoft.Crm.Extensibility.MessageProcessor.RunStage(PipelineExecutionContext context, Int32 pipelineStage) 
> at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context) 
> at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context) 
> at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.ExecuteInternal(IInProcessOrganizationServiceFactory serviceFactory, IPlatformMessageDispatcherFactory dispatcherFactory, String messageName, String requestName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, ParameterCollection fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId, Guid transactionContextId, Int32 invocationSource, Nullable`1 requestId, Version endpointVersion) 
> at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequestRequestWithInstrumentation(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, UserAuth userAuth, Guid targetUserId, OrganizationContext context, Boolean returnResponse, Boolean checkAdminMode, Object operation) 
> at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequest(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, UserAuth userAuth, Guid targetUserId, OrganizationContext context, Boolean returnResponse, Boolean checkAdminMode) 
> at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequest(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode) 
> at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode) 

我不明白为什么我不能做到这一点最简单的操作。

任何人都可以摆脱任何光线? 谢谢, 灰色

+0

我觉得你有一个像client.Id = someguid一条线;或客户端[“primarykey”] = someguid,这可能会导致该错误 –

+0

Guido:我发布的代码是整个代码(除了样板工作流活动代码)。我从字面上得到一个实体,改变一个值,并试图挽回它。我没有在任何地方设置accountid属性值的.Id属性。 – Gray

+2

尽管它没有回答为什么这个代码有问题,但我发现它设置一个新的实体对象,从查询的返回结果中复制id,在新的实体对象中设置属性,并调用service.update在那个对象上。通过这种方式,审计历史不会被更新操作之前和之后具有相同值的一堆“更新字段”堵塞。它还利用CRM API更新数据的方法 –

回答

0

尝试将记录附加到保存之前的背景下,这样的:

xrmContext.ClearChanges(); 
if (!xrmContext.IsAttached(client)) xrmContext.Attach(client); 
xrmContext.UpdateObject(client); 
xrmContext.SaveChanges(); 
+0

谢谢,但已经尝试过了。我害怕没有快乐。 – Gray

相关问题