简体   繁体   English

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

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

I am begginer to the Custom Code activity and I tried to create a an auto follow for a specific order when a subgrid on order form is updated. 我是“自定义代码”活动的开始者,当尝试更新订单表单上的子网格时,我尝试为特定订单创建自动跟踪。 But, I am facing following exception in custom code activity to create a follow for an order in dynamics crm 2016. Your thoughts on it, how can I resolve it? 但是,我在自定义代码活动中面临以下异常,以在动态crm 2016中创建订单的跟踪。您对此有何想法,我该如何解决?

Source code 源代码

[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));
        }

Exception 例外

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)

You may not be able to assign an owner as an attribute on create as you are doing here: 您可能无法像在此处那样在创建时将所有者分配为属性:

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

Instead, try to execute an AssignRequest after your Create() : 相反,尝试在Create()之后执行AssignRequest:

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

service.Execute(assign);

Also try checking that ownerId and orderId are valid: 另外,请尝试检查ownerId和orderId是否有效:

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

I think that you should change this line: 我认为您应该更改此行:

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

your input entity is salesOrder and you are assigning this entity to outputEntity which is postFollow. 您的输入实体是salesOrder,并且您正在将该实体分配给postFollow的outputEntity。 The line you commented out looks much better: 您注释掉的行看起来更好:

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

but the even better looking code would be: 但是看起来更好的代码是:

postFollow.Id = service.Create(postfollow);

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

Also make sure that both ownerId and orderId that are passed to your function have valid ids and LogicalNames for existing records in your system. 还要确保传递给您的函数的ownerId和orderId都具有系统中现有记录的有效ID和LogicalNames。

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

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