繁体   English   中英

自定义代码活动无法在Dynamics CRM中为订单创建关注

[英]Custom Code activity is not working to create a follow for an Order in dynamics crm

我是“自定义代码”活动的开始者,当尝试更新订单表单上的子网格时,我尝试为特定订单创建自动跟踪。 但是,我在自定义代码活动中面临以下异常,以在动态crm 2016中创建订单的跟踪。您对此有何想法,我该如何解决?

源代码

[RequiredArgument]
        [Input("InputEntity")]
        [ReferenceTarget("salesorder")]
        public InArgument<EntityReference> inputEntity { get; set; }

        [Output("createFollowPost")]
        [ReferenceTarget("postfollow")]
        public OutArgument<EntityReference> outputEntity { get; set; }

private void createFollow(IOrganizationService service, Guid ownerId, Guid orderId,CodeActivityContext caContext) {
            Entity postfollow = new Entity("postfollow");
            postfollow["ownerid"] = new EntityReference("systemuser",ownerId);
            postfollow["regardingobjectid"] = new EntityReference("salesorder",orderId);

            Guid followId = service.Create(postfollow);
            this.outputEntity.Set(caContext, inputEntity.Get(caContext));
            //this.outputEntity.Set(caContext, new EntityReference("postfollow", followId));
        }

例外

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Entity Reference cannot have Id and Key Attributes empty.Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220989</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>Entity Reference cannot have Id and Key Attributes empty.</Message>
  <Timestamp>2017-03-15T13:17:20.826911Z</Timestamp>
  <InnerFault>
    <ErrorCode>-2147220970</ErrorCode>
    <ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <Message>System.ArgumentException: Entity Reference cannot have Id and Key Attributes empty.</Message>
    <Timestamp>2017-03-15T13:17:20.826911Z</Timestamp>
    <InnerFault i:nil="true" />
    <TraceText i:nil="true" />
  </InnerFault>
  <TraceText>[Microsoft.Xrm.Sdk.Workflow: Microsoft.Xrm.Sdk.Workflow.Activities.RetrieveEntity]
[RetrieveEntity]
</TraceText>
</OrganizationServiceFault>
   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Retrieve(EntityReference entityReference, ColumnSet columnSet, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode)
   at Microsoft.Crm.Extensibility.InprocessServiceProxy.RetrieveCore(String entityName, Guid id, ColumnSet columnSet)
   at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Retrieve(String entityName, Guid id, ColumnSet columnSet)
   at Microsoft.Crm.Workflow.Services.RetrieveActivityService.<>c__DisplayClass1.<RetrieveInternal>b__0(IOrganizationService sdkService)
   at Microsoft.Crm.Workflow.Services.ActivityServiceBase.ExecuteInTransactedContext(ActivityDelegate activityDelegate)
   at Microsoft.Crm.Workflow.Services.RetrieveActivityService.ExecuteInternal(ActivityContext executionContext, RetrieveEntity retrieveEntity)
   at Microsoft.Crm.Workflow.Services.RetrieveActivityService.Execute(ActivityContext executionContext, RetrieveEntity retrieveEntity)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

您可能无法像在此处那样在创建时将所有者分配为属性:

postfollow["ownerid"] = new EntityReference("systemuser",ownerId);

相反,尝试在Create()之后执行AssignRequest:

AssignRequest assign = new AssignRequest 
{
    Assignee = new EntityReference("systemuser", ownerId),
    Target = new EntityReference("postfollow", followId)
};

service.Execute(assign);

另外,请尝试检查ownerId和orderId是否有效:

if (orderId != null && orderId != Guid.Empty) { }

我认为您应该更改此行:

this.outputEntity.Set(caContext, inputEntity.Get(caContext));

您的输入实体是salesOrder,并且您正在将该实体分配给postFollow的outputEntity。 您注释掉的行看起来更好:

this.outputEntity.Set(caContext, new EntityReference("postfollow", followId));

但是看起来更好的代码是:

postFollow.Id = service.Create(postfollow);

this.outputEntity.Set(caContext, postFollow.ToEntityReference());

还要确保传递给您的函数的ownerId和orderId都具有系统中现有记录的有效ID和LogicalNames。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM