【问题标题】:Fetch XML Query Issue获取 XML 查询问题
【发布时间】:2014-08-12 09:49:33
【问题描述】:

在我工作的公司中,我在 Microsoft Dynamics CRM 2013 中创建了一个名为 Issue 的新实体,在实体 Issue 中有一个子网格,用户可以在其中添加实体 Cost To Business 的记录。

目的是用户在需要时添加记录。我现在使用带有 C# 插件的 FetchXML 查询,以便我可以检索与子网格中的每条记录相关的成本属性,以便我可以填充总成本字段。

到目前为止,我有以下查询

Guid orderID = (Guid)((Entity)context.InputParameters["Target"]).Id;

string fetchxml =
    @"<fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'>
          <entity name='new_issue'>
              <attribute name='new_issueid'/>
              <attribute name='new_name'/>
              <attribute name='createdon'/>
              <order descending='false' attribute='new_issueid'/>
              <filter type='and'> 
                  <condition attribute='new_issueid' operator='eq' value='" + orderID + @"'/> 
              </filter> 
              <link-entity name = 'new_costtype' alias='ac' to='new_issueid' from='new_costtypeissueid'>
                  <link-entity name='new_costtobusiness' alias='ad' to='new_costtobusinesscosttypeid' from='new_costtobusinessid'>
                       <attribute name='new_coststring'/>                                             
                  </link-entity>
              </link-entity>
          </entity>
      </fetch>"; 

但是,查询不能返回任何内容,因为以下代码没有被执行。

EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml));

if (result != null && result.Entities.Count > 0)
{
    List<int> _product = new List<int>();

    foreach (Entity _entity in result.Entities)//loop through every record
    {
        costToBusiness = ((AliasedValue)_entity.Attributes["ad.new_coststring"]).Value.ToString();
        total = (total) + Convert.ToInt32(costToBusiness);           
    }

    if(entity.Attributes.Contains("new_businessstring"))
    {
        entity.Attributes["new_businessstring"] = currencyName +  total.ToString();
        costToBusiness = "";
        total = 0;
        currencyName = "";
    }
    else
    {
        entity.Attributes.Add("new_businessstring", currencyName +  total.ToString());
        costToBusiness = "";
        total = 0;
        currencyName = "";            
    }
}

是否有人对为什么会发生这种情况有原因/建议? 提前致谢

更新

来自 throw new InvalidPluginExecutionExeption 的下载日志的详细信息

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: <fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'>
                    <entity name='new_issue'>
                        <attribute name='new_issueid'/>
                        <attribute name='new_name'/>
                        <attribute name='createdon'/>
                            <order descending='false' attribute='new_issueid'/>
                            <filter type='and'> 
                                <condition attribute='new_issueid' operator='eq' value='ddd33910-7c12-e411-a5f0-00155d550f0a'/> 
                            </filter> 
                            <link-entity name = 'new_costtype' alias='ac' to='new_issueid' from='new_costtypeissueid'>
                            <link-entity name='new_costtobusiness' alias='ad' to='new_costtobusinesscosttypeid' from='new_costtobusinessid'>
                                <attribute name='new_coststring'/>                                             
                            </link-entity>
                            </link-entity>
                    </entity>
              </fetch>Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220891</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<KeyValuePairOfstringanyType>
  <d2p1:key>OperationStatus</d2p1:key>
  <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
</KeyValuePairOfstringanyType>
<KeyValuePairOfstringanyType>
  <d2p1:key>SubErrorCode</d2p1:key>
  <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">
2146233088</d2p1:value>
</KeyValuePairOfstringanyType>

`fetch distinct='true' mapping='logical' output-format='xml-platform'version='1.0'&gt;
                    &lt;entity name='new_issue'&gt;
                        &lt;attribute name='new_issueid'/&gt;
                        &lt;attribute name='new_name'/&gt;
                        &lt;attribute name='createdon'/&gt;
                            &lt;order descending='false' attribute='new_issueid'/&gt;
                            &lt;filter type='and'&gt; 
                                &lt;condition attribute='new_issueid' operator='eq' value='ddd33910-7c12-e411-a5f0-00155d550f0a'/&gt; 
                            &lt;/filter&gt; 
                            &lt;link-entity name = 'new_costtype' alias='ac' to='new_issueid' from='new_costtypeissueid'&gt;
                            &lt;link-entity name='new_costtobusiness' alias='ad' to='new_costtobusinesscosttypeid' from='new_costtobusinessid'&gt;
                                &lt;attribute name='new_coststring'/&gt;                                             
                            &lt;/link-entity&gt;
                            &lt;/link-entity&gt;
                    &lt;/entity&gt;
              &lt;/fetch&gt;</Message>
<Timestamp>2014-08-13T13:10:53.9593098Z</Timestamp>

[CostToBusiness: CostToBusiness.CostBusiness]
[2db9e3ba-3616-e411-a5f0-00155d550f0a: CostToBusiness.CostBusiness: Update of new_issue]


</TraceText>
</OrganizationServiceFault>

`

对消息的最后一部分感到抱歉,由于某种原因,我终生无法将其全部转换为代码格式

【问题讨论】:

  • 需要额外的上下文。代码应该如何执行?它在插件中吗?插件是如何注册的?
  • 嗨@Daryl,这是一个插件,它在更新、预操作和同步时注册(预操作,因为插件正在更新字段)谢谢
  • 您是否希望插件在用户向业务添加新成本时触发?
  • 保存/更新记录时不会触发插件
  • 问题与查询有关,因为我在未调用的 foreach 循环中放入了 throw new InvalidPluginExecutionExeception,因此查询必须不返回任何内容

标签: c# plugins dynamics-crm-2013 fetchxml


【解决方案1】:

如果我理解正确,您将 new_issue 作为某个其他实体的子实体,该实体具有存储来自 new_issue 的汇总成本的字段。 在这种情况下,您应该在 new_issue 上安装 PostOperation 插件(创建/更新/StateChange),并且您的 fetch xml 应该是聚合 fetch xml,它将搜索查找字段 id 等于父实体 ID 的所有 new_issue,然后汇总成本。在删除的情况下,您必须以稍微不同的方式处理它。 高温

【讨论】:

  • 不能将其作为 PostOperation,因为我正在使用插件更新字段。所以必须是PreValidation/PreOperation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多