MS CRM 2011 創建基於Fetch的報表 -- 進階版


 

原創地址:http://www.cnblogs.com/jfzhu/archive/2012/10/07/2713507.html

轉載請注明出處

 

(一) 在之前的文章中,我介紹了如何創建基於Fetch的報表,並使用數據預篩選功能。在這篇文章中,我們使用一個比較復雜的FetchXML來完成一個更有實際意義的報表。

 

在這個報表中,顯示相關客戶,客戶的訂單,以及訂單的產品信息。報表使用了三個inner join,account inner join order inner join orderdetail inner join product

 

(1) 利用CRM的高級查找創建FetchXML

image

下載Fetch XML:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true"> 
  <entity name="account"> 
    <attribute name="name" /> 
    <attribute name="primarycontactid" /> 
    <attribute name="telephone1" /> 
    <attribute name="accountid" /> 
    <order attribute="name" descending="false" /> 
    <filter type="and"> 
      <condition attribute="statuscode" operator="eq" value="1" /> 
    </filter> 
    <link-entity name="salesorder" from="customerid" to="accountid" alias="aa"> 
      <filter type="and"> 
        <condition attribute="statecode" operator="ne" value="2" /> 
      </filter> 
      <link-entity name="salesorderdetail" from="salesorderid" to="salesorderid" alias="ab"> 
        <link-entity name="product" from="productid" to="productid" alias="ac"></link-entity> 
      </link-entity> 
    </link-entity> 
  </entity> 
</fetch>

(2) 我們將它拷貝到報表中數據集的Query,並進行一些修改(添加我們需要的attribute)

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true"> 
  <entity name="account"> 
    <attribute name="name" /> 
    <attribute name="accountid" /> 
    <order attribute="name" descending="false" /> 
    <filter type="and"> 
      <condition attribute="statuscode" operator="eq" value="1" /> 
    </filter> 
    <link-entity name="salesorder" from="customerid" to="accountid" alias="aa"> 
      <attribute name="ordernumber" /> 
      <attribute name="name" /> 
      <attribute name="totalamount" /> 
      <filter type="and"> 
        <condition attribute="statecode" operator="ne" value="2" /> 
      </filter> 
      <link-entity name="salesorderdetail" from="salesorderid" to="salesorderid" alias="ab"> 
        <attribute name="quantity" /> 
        <attribute name="priceperunit" /> 
        <attribute name="baseamount" /> 
        <link-entity name="product" from="productid" to="productid" alias="ac"> 
          <attribute name="name" /> 
        </link-entity>        
      </link-entity> 
    </link-entity> 
  </entity> 
</fetch>

(3) 按照之前的文章介紹,使用數據預篩選功能。

添加一個CRM_FilteredAccount參數,使用以下的Query:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"><entity name="account"><all-attributes /></entity></fetch>

(4) 修改數據集Query的Fetch XML:

對這一行修改:  <entity name="account" enableprefiltering="1" prefilterparametername="CRM_FilteredAccount">

修改結果為:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true"> 
  <entity name="account" enableprefiltering="1" prefilterparametername="CRM_FilteredAccount"> 
    <attribute name="name" /> 
    <attribute name="accountid" /> 
    <order attribute="name" descending="false" /> 
    <filter type="and"> 
      <condition attribute="statuscode" operator="eq" value="1" /> 
    </filter> 
    <link-entity name="salesorder" from="customerid" to="accountid" alias="aa"> 
      <attribute name="ordernumber" /> 
      <attribute name="name" /> 
      <attribute name="totalamount" /> 
      <filter type="and"> 
        <condition attribute="statecode" operator="ne" value="2" /> 
      </filter> 
      <link-entity name="salesorderdetail" from="salesorderid" to="salesorderid" alias="ab"> 
        <attribute name="quantity" /> 
        <attribute name="priceperunit" /> 
        <attribute name="baseamount" /> 
        <link-entity name="product" from="productid" to="productid" alias="ac"> 
          <attribute name="name" /> 
        </link-entity>        
      </link-entity> 
    </link-entity> 
  </entity> 
</fetch>

(5) 在設計面板中添加Table:

image

(6) 到這一步報表就完成了。將報表上傳到CRM中看一下結果:

image

 

 

 

(二) 使用FetchXML實現inner join

在上面的實例中,已經看到如何使用 link-entity 來實現 inner join。link-entity 有一個屬性 -- link-type,它的默認值為inner,你可以不指定該屬性值,也可以指定其為inner。

<fetch distinct="false" no-lock="false" mapping="logical"> 
  <entity name="account" enableprefiltering="1" prefilterparametername="CRM_FilteredAccount"> 
    <attribute name="name" alias="name" /> 
    <attribute name="accountid" /> 
    <link-entity name="contact" to="accountid" from="parentcustomerid" alias="contact1"> 
      <attribute name="fullname" alias="contact1_fullname" /> 
      <attribute name="contactid" /> 
    </link-entity> 
  </entity> 
</fetch>
<fetch distinct="false" no-lock="false" mapping="logical"> 
  <entity name="account" enableprefiltering="1" prefilterparametername="CRM_FilteredAccount"> 
    <attribute name="name" alias="name" /> 
    <attribute name="accountid" /> 
    <link-entity name="contact" to="accountid" from="parentcustomerid" link-type="inner" alias="contact1"> 
      <attribute name="fullname" alias="contact1_fullname" /> 
      <attribute name="contactid" /> 
    </link-entity> 
  </entity> 
</fetch>


 

 

(三) 使用FetchXML實現outer join

實現outer join需要指link-entity 的屬性 link-type的值為outer

<fetch distinct="false" no-lock="false" mapping="logical"> 
  <entity name="account" enableprefiltering="1" prefilterparametername="CRM_FilteredAccount"> 
    <attribute name="name" alias="name" /> 
    <attribute name="accountid" /> 
    <link-entity name="contact" to="accountid" from="parentcustomerid" link-type="outer" alias="contact1"> 
      <attribute name="fullname" alias="contact1_fullname" /> 
      <attribute name="contactid" /> 
    </link-entity> 
  </entity> 
</fetch>

 

 

 

(四) 使用 Order + Count

你可以使用order對fetchxml返回的數據集進行排序,並且可以用count指定返回數據的個數。下面的代碼指定按照contact fullname 和 account name 進行排序,並返回最多1條數據。

<fetch distinct="false" no-lock="false" mapping="logical" count="1"> 
  <entity name="account" enableprefiltering="1" prefilterparametername="CRM_FilteredAccount"> 
    <attribute name="name" alias="name" /> 
    <attribute name="accountid" /> 
    <link-entity name="contact" to="accountid" from="parentcustomerid" link-type="inner" alias="contact1"> 
      <attribute name="fullname" alias="contact1_fullname" /> 
      <attribute name="contactid" /> 
      <order attribute="fullname"/> 
    </link-entity> 
    <order attribute="name"/> 
  </entity> 
</fetch>

 

 

 

(五) 使用 fetchXML來獲取 N:N 關系的數據

如果你使用的是Native N:N 關系,CRM 會自動為你的關系創建一個Entity。比如我做的一個項目中,有如下的關系, account 與 aw_aansluitadres 是 1 : N 的關系, aw_aansluitadres 與 aw_deelcontract 是 N : N 的關系, 系統便自動生成一個名為 aw_deelcontract_aansluitadres的entity。

 

如果你想獲得 account inner join aw_aansluitadres inner join aw_deelcontract, 你可以使用下面的fetchXML:

<fetch distinct="false" mapping="logical" > 
  <entity name="account" enableprefiltering="1" prefilterparametername="CRM_FilteredAccount"> 
    <attribute name="name" alias="name" /> 
    <link-entity name="aw_aansluitadres" from="aw_accountid" to="accountid" alias="aa" > 
      <link-entity name="aw_deelcontract_aansluitadres" from="aw_aansluitadresid" to="aw_aansluitadresid" visible="false" intersect="true"> 
        <link-entity name="aw_deelcontract" from="aw_deelcontractid" to="aw_deelcontractid" alias="ab"> 
          <attribute name="aw_einddatum" alias="ab_aw_einddatum" /> 
          <attribute name="aw_leverancierid" alias="ab_aw_leverancierid" /> 
          <attribute name="aw_jaartalid" alias="ab_aw_jaartalid" /> 
        </link-entity> 
      </link-entity> 
    </link-entity> 
  </entity> 
</fetch>

如果想得到 account outer join aw_aansluitadres outer join aw_deelcontract,我在上面已經解釋過了,需要為每個link-entity 的 link-type屬性指定值為outer

<fetch distinct="false" mapping="logical" count="1"> 
  <entity name="account" enableprefiltering="1" prefilterparametername="CRM_FilteredAccount"> 
    <attribute name="name" alias="name" /> 
    <link-entity name="aw_aansluitadres" from="aw_accountid" to="accountid" alias="aa" link-type="outer"> 
      <link-entity name="aw_deelcontract_aansluitadres" from="aw_aansluitadresid" to="aw_aansluitadresid" visible="false" link-type="outer"> 
        <link-entity name="aw_deelcontract" from="aw_deelcontractid" to="aw_deelcontractid" alias="ab" link-type="outer"> 
          <attribute name="aw_einddatum" alias="ab_aw_einddatum" /> 
          <attribute name="aw_leverancierid" alias="ab_aw_leverancierid" /> 
          <attribute name="aw_jaartalid" alias="ab_aw_jaartalid" /> 
        </link-entity> 
      </link-entity> 
    </link-entity> 
  </entity> 
</fetch>

 

 

 

總結: fetchXML可以實現inner join,也可以實現outer join,關鍵在於link-entity的link-type屬性,inner為inner join, outer 為 outer join,默認值為 inner。通過對數據使用order 排序,還可以通過設置fetch 的 count屬性來指定返回的數據數目。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM