CRM插件中不再存在的帐户

时间:2017-02-13 15:19:42

标签: dynamics-crm-online

我正在中间发生一些事情(比如<1%的时间),我不知道它是如何可能的。我在Qualify Lead活动的Post Operation上注册了一个插件,并覆盖了Qualify Button以执行Qualify Lead请求,将CreateAccountCreateContactCreateOpportunity值设置为假。然后该插件创建帐户,联系人和机会,以及其他4个潜在的自定义实体。

以下是相关代码:

protected override void ExecuteInternal(ExtendedPluginContext context)
{
    var createdEntities = context.GetOutputParameterValue<EntityReferenceCollection>("CreatedEntities");
    var initiatingUser = context.SystemOrganizationService.GetEntity<SystemUser>(context.InitiatingUserId, u => new { u.new_RelatedEmployee, u.FullName });
    var initiatingUserEntityRef = initiatingUser.ToEntityReference();
    var lead = RetrieveLead(context);
    CreateAccount(context, lead, initiatingUserEntityRef, createdEntities);
    CreateContact(context, lead, initiatingUserEntityRef, createdEntities);
    CreateLocation(context, lead, initiatingUser, createdEntities);
    CreateSystemPullJob(context, lead, initiatingUserEntityRef, createdEntities);
    CreateInstallJob(context, lead, initiatingUserEntityRef, createdEntities);
    CreateOpportunity(context, lead, initiatingUserEntityRef, createdEntities);
    CreateOpportunityProducts(context, lead, initiatingUserEntityRef, createdEntities);
    UpdateLead(context, lead, initiatingUser, createdEntities);
}

private static void CreateAccount(ExtendedPluginContext context, XrmLead lead, EntityReference initiatingUser, EntityReferenceCollection createdEntities)
{
    if (lead.CustomerId != null)
    {
        context.Trace("Account already exists for Lead.  Not creating Account.");
        return;
    }

    context.Trace("Creating Account");
    var account = context.SystemOrganizationService.InitializeFrom<Account>(lead.ToEntityReference(), TargetFieldType.ValidForCreate);
    account.OwnerId = initiatingUser;
    account.ModifiedOnBehalfBy = initiatingUser;
    account.CreatedOnBehalfBy = initiatingUser;

    account.Id = context.SystemOrganizationService.CreateWithSupressDuplicateDetection(account);

    lead.CustomerId = account.ToEntityReference();

    createdEntities.Add(account.ToEntityReference());
}

private static void CreateInstallJob(ExtendedPluginContext context,
                                     XrmLead lead,
                                     EntityReference initiatingUser,
                                     EntityReferenceCollection createdEntities)
{
    context.Trace("Creating Install Job");
    var job = context.SystemOrganizationService.InitializeFrom<new_job>(lead.ToEntityReference(), TargetFieldType.ValidForCreate);
    job.new_jobname = "New - Install";
    job.new_CustomerId = lead.CustomerId;
    job.new_LocationId = GetLocation(lead, createdEntities);
    job.new_JobTypeEnum = new_JobType.Installation;
    job.OwnerId = initiatingUser;
    job.ModifiedOnBehalfBy = initiatingUser;
    job.CreatedOnBehalfBy = initiatingUser;

    if (lead.new_PreviousLocationId != null)
    {
        // Set prerequiste job to system pull job
        job.new_PrerequisiteJobId = createdEntities.First(e => e.LogicalName == job.LogicalName);
    }

    job.Id = context.SystemOrganizationService.CreateWithSupressDuplicateDetection(job);

    createdEntities.Add(job.ToEntityReference());
}

创建帐户,添加到lead.CustomerId,然后用于在创建时将关系添加到该位置。

根据堆栈跟踪,在创建安装作业时发生错误(即使已经创建了联系人,并将其作为它的ParentCustomerId引用)

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Account With Id = d14241a1-eef1-e611-810e-e0071b6ac161 Does Not ExistDetail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
    <ActivityId>05e171b8-60dd-44d5-ba40-42edb79620d8</ActivityId>
    <ErrorCode>-2147220969</ErrorCode>
    <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
    <Message>Account With Id = d14241a1-eef1-e611-810e-e0071b6ac161 Does Not Exist</Message>
    <Timestamp>2017-02-13T13:16:34.5516854Z</Timestamp>
    <ExceptionSource>SdkClient</ExceptionSource>
    <InnerFault>
        <ActivityId>05e171b8-60dd-44d5-ba40-42edb79620d8</ActivityId>
        <ErrorCode>-2147220969</ErrorCode>
        <ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
        <Message>Account With Id = d14241a1-eef1-e611-810e-e0071b6ac161 Does Not Exist</Message>
        <Timestamp>2017-02-13T13:16:34.5516854Z</Timestamp>
        <ExceptionSource i:nil="true" />
        <InnerFault i:nil="true" />
        <OriginalException i:nil="true" />
        <TraceText i:nil="true" />
    </InnerFault>
    <OriginalException>System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Account With Id = d14241a1-eef1-e611-810e-e0071b6ac161 Does Not Exist (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).
        at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode, ExecutionContext executionContext)
        at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
        at Microsoft.Crm.Extensibility.InprocessServiceProxy.ExecuteCore(OrganizationRequest request)
        at Microsoft.Crm.Sandbox.SandboxSdkListener.ExecuteInternal(SandboxCallInfo callInfo, SandboxSdkContext requestContext, String operation, Byte[] serializedRequest, IExecutionContext context, String&amp; primaryEntityName)
        at Microsoft.Crm.Sandbox.SandboxSdkListener.Execute(SandboxCallInfo callInfo, SandboxSdkContext requestContext, String operation, Byte[] serializedRequest) 
        Original SdkErrors: 
    </OriginalException>
    <TraceText>Entered Contoso.Xrm.Lead.Plugins.QualifyLeadLogic.Execute()
        Contoso.Xrm.Lead.Plugins.QualifyLeadLogic.Execute is Executing for Entity: lead, Message: QualifyLead
        Creating Account
        Creating Contact
        Creating Location
        Creating Install Job
        Exception: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: Account With Id = d14241a1-eef1-e611-810e-e0071b6ac161 Does Not Exist (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).

        Server stack trace: 
        at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc&amp; rpc)
        at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
        at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
        at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

        Exception rethrown at [0]: 
        at Microsoft.Crm.Sandbox.SandboxOrganizationService.Execute(String operation, Byte[] serializedRequest, Object sandboxTraceSettingsObj)
        at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]&amp; outArgs)
        at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)

        Exception rethrown at [1]: 
        at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
        at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type)
        at Microsoft.Crm.Sandbox.ISandboxOrganizationService.Execute(String operation, Byte[] serializedRequest, Object traceSettings)
        at Microsoft.Crm.Sandbox.SandboxOrganizationServiceWrapper.ExecuteInternal(OrganizationRequest request)
        at DLaB.Xrm.Extensions.CreateWithSupressDuplicateDetection(IOrganizationService service, Entity entity)
        at Contoso.Xrm.Lead.Plugins.QualifyLeadLogic.CreateInstallJob(ExtendedPluginContext context, Lead lead, EntityReference initiatingUser, EntityReferenceCollection createdEntities)
        at Contoso.Xrm.Lead.Plugins.QualifyLeadLogic.ExecuteInternal(ExtendedPluginContext context)
        at DLaB.Xrm.Plugin.GenericPluginHandlerBase`1.ExecuteRegisteredEvent(T context)
        at DLaB.Xrm.Plugin.GenericPluginHandlerBase`1.Execute(IServiceProvider serviceProvider)
        **** Context Info ****
        Plugin: Contoso.Xrm.Lead.Plugins.QualifyLeadLogic
        * Registered Event *
        Stage: PostOperation
        Message: QualifyLead
        Message Name: QualifyLead
        Entity Logical Name: 
        Execute: Null
        BusinessUnitId: 425821ca-d73c-e411-936d-a45d36fd8134
        CorrelationId: 75366e73-bee1-4154-81a4-354a4c0ffe8f
        Depth: 1
        InitiatingUserId: 995e0b19-4592-e611-80f3-5065f38a4951
        IsInTransaction: True
        IsolationMode: 2
        MessageName: QualifyLead
        Mode: 0
        OperationCreatedOn: 2/13/2017 1:16:32 PM
        OperationId: 68ea91c1-3727-4cb4-a52d-3a632b4239e0
        Organization: org8ea62131(fc3abc92-879c-4a08-8715-4156ce535b92)
        OwningExtension: Contoso.Xrm.Lead.Plugins.QualifyLead: QualifyLead of lead (3d85ff0c-8705-e611-80ee-3863bb36bd38)
        PrimaryEntityId: 00000000-0000-0000-0000-000000000000
        PrimaryEntityName: lead
        SecondaryEntityName: none
        UserId: 995e0b19-4592-e611-80f3-5065f38a4951
        * Input Parameters *
            Param[CreateAccount]: False
            Param[CreateContact]: False
            Param[CreateOpportunity]: False
            Param[LeadId]: EntityReference { LogicalName: lead, Name: , Id: 122448c6-edf1-e611-810e-e0071b6ac161}
            Param[SourceCampaignId]: EntityReference { LogicalName: campaign, Name: , Id: 330a11ca-4687-e611-80f3-5065f38b21f2}
            Param[Status]: 3
            Param[OpportunityCurrencyId]: 
            Param[OpportunityCustomerId]: 
            Param[ProcessInstanceId]: 
        * Output Parameters *
            Param[CreatedEntities] Entity Reference Collection:
                EntityReference { LogicalName: account, Name: , Id: d14241a1-eef1-e611-810e-e0071b6ac161}
                EntityReference { LogicalName: contact, Name: , Id: dd4241a1-eef1-e611-810e-e0071b6ac161}
                EntityReference { LogicalName: new__location, Name: , Id: e64241a1-eef1-e611-810e-e0071b6ac161}
        PostEntityImages: Empty
        PreEntityImages: Empty
        * Shared Variables *
            Param[ChangedEntityTypes]: System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]:
                [lead, Update]
            Param[Contoso.Xrm.Lead.Plugins.QualifyLeadLogic|QualifyLead|PostOperation|00000000-0000-0000-0000-000000000000]: 1
        Has Parent Context: False
        Stage: 40
        Exiting Contoso.Xrm.Lead.Plugins.QualifyLeadLogic.Execute()
    </TraceText>
</OrganizationServiceFault>

Server stack trace: 
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at Microsoft.Crm.Sandbox.ISandboxHost.ExecuteAndReturnTraceInfo(SandboxCallInfo callInfo, SandboxPluginExecutionContext requestContext, Guid pluginAssemblyId, Int32 sourceHash, String assemblyName, Guid pluginTypeId, String pluginTypeName, String pluginConfiguration, String pluginSecureConfig, String assemblyContents, Boolean returnTraceInfo)
at Microsoft.Crm.Sandbox.SandboxPlugin.Execute(SandboxClient client, SandboxCallTracker callTracker, IExecutionContext requestContext, String assemblyContents, Boolean returnTraceInfo)
at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)

我只看到两种可能性,即创建帐户时没有创建(似乎不太可能,因为联系人创建成功)或者在创建联系人之后但在安装创建作业之前删除了帐户(但我&# 39;我不知道我们在哪里进行删除,而且我不认为你可以在交易之外删除交易中的实体......)

想法?

3 个答案:

答案 0 :(得分:1)

您是否可以验证“遗失的帐户ID”是否是应创建的帐户的ID? 如果在错误发生后查询此帐户的数据库,我猜它没有被命中? 你检查过“双击错误”吗?如果两次执行之间可能出现QualifyLead出血,那么同时发出两次SystemOrganizationService可能会产生有趣的影响。

答案 1 :(得分:0)

(试图将此作为评论,因为它在技术上不是答案,但它太长了。)

如果您注释掉安装作业的创建会怎样?其他一切都正常吗?帐户是否已创建?

要检查的另一件事是在每个create方法执行后查询帐户。当帐户消失时,可以帮助您缩小范围。

另外,关于您对交易外部删除的说明,我对交易如何运作的理解如下。如果删除来自创建自己的事务(比如说是异步插件),那么你的建议是真的。但是,如果删除来自创建联系人的某个同步插件,那么该插件也将在事务中运行。如果确实如此,则允许删除该帐户。

答案 2 :(得分:0)

不是答案,但评论时间太长

所以如果你说它a)间歇性地发生b)没有其他逻辑可以删除帐户,我认为唯一的结论是出于某些未知的时间原因,installjob的创建超前于该帐户可见。不知道为什么会发生这种情况但又一次,为了缩小范围你可以捕获异常并且不会使交易失败。这至少会告诉您该帐户是否确实已创建好。如果是这种情况并且无法找到计时问题的根本原因,那么只有在您确定可以检索帐户时才能尝试安装。所有解决方法都可以完成更多的变量消除。

另一个想法是它可能与安全有关吗?该帐户不存在消息可能真的意味着帐户不存在。如果插件上下文用户创建了它,但可能涉及其他安全因素,这似乎很奇怪?