2017-06-07 7 views
0

CRMでコントラクトルックアップフィールドをフィルタリングするためにfetchxmlを作成しました。私は契約の検索フィールドにいくつのレコードがあるのか​​を知りたい。レコードが1つしかない場合は、その参照フィールドに自動的に値を設定できます。Dynamics CRMのルックアップフィールドの長さを

fetchxmlレコードをループすることができますが、カウントを返すことに幸運を祈ることができないようにAPI関数を使って試行しました。必要な結果を得る別の方法がありますか?

function buildCustomView() { 
 
     // Some GUID but only needs to be unique among the other available views for the lookup 
 
     var viewId = "{00000000-0000-0000-0000-000000000001}"; 
 
     var viewDisplayName = "Contracts for This Account"; 
 

 
     //Only need Contracts for the selected Worksite and buisness unit if filled in 
 
     var worksite = getFieldValue("hc_worksite"); 
 

 
     if (!worksite) { return; } 
 
     if (!worksite[0]) { return; } 
 
     if (!worksite[0].id) { return; } 
 
     //check if buisness unit is availble and if it is can filter lookup further 
 
     var buisnessUnit = getFieldValue("hc_businessunit"); 
 
     var condition; 
 
     if (buisnessUnit != null) { 
 
      condition = "<filter type='and'>" + 
 
            "<condition attribute='hc_businessunit' operator='eq' value='" + buisnessUnit[0].id + "' />" + 
 
           "</filter>"; 
 
      ///if take out the account name, contract lookup will still be filteredl 
 
     } else { 
 
      condition = ""; 
 
     } 
 

 
     //Get all Contracts that pertain to the selected worksite 
 
     var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' >" + 
 
         "<entity name='contract'>" + 
 
          "<attribute name='title' />" + 
 
          "<attribute name='customerid' />" + 
 
          "<attribute name='contractid' />" + 
 
          "<attribute name='hc_contracttype' />" + 
 
          "<attribute name='hc_contractsubtype' />" + 
 
          "<attribute name='activeon' />" + 
 
          "<attribute name='hc_contractlevel' />" + 
 
          "<attribute name='expireson' />" + 
 
          "<attribute name='hc_contractaccount' />" + 
 
          "<attribute name='hc_contract' />" + 
 
          "<order attribute='title' descending='false' />" + condition + 
 
          "<link-entity name='hc_account_contract' from='contractid' to='contractid' visible='false' intersect='true'>" + 
 
           "<link-entity name='account' from='accountid' to='accountid' alias='ab'>" + 
 
           "<filter type='and'>" + 
 
            "<condition attribute='accountid' operator='eq' value='" + worksite[0].id + "' />" + 
 
           "</filter>" + 
 
           "</link-entity>" + 
 
          "</link-entity>" + 
 
          "</entity>" + 
 
         "</fetch>"; 
 
     //Set up the whole view's UI 
 
     var layoutXml = "<grid name='resultset' object='1' jump='hc_contract' select='1' icon='1' preview='2'>" + 
 
         "<row name='result' id='contractid'>" + 
 
          "<cell name='title' width='300' />" + 
 
          "<cell name='hc_contractaccount' width='200' />" + 
 
          "<cell name='hc_contracttype' width='100' />" + 
 
          "<cell name='hc_contractsubtype' width='100' />" + 
 
          "<cell name='hc_contractlevel' width='100' />" + 
 
          "<cell name='activeon' width='100' />" + 
 
          "<cell name='expireson' width='100' />" + 
 
          "<cell name='customerid' width='150' />" + 
 
          "<cell name='hc_contract' width='100' />" + 
 
         "</row>" + 
 
         "</grid>"; 
 

 
     try { 
 
      //Set the view 
 
      Xrm.Page.getControl("hc_contract").addCustomView(viewId, "contract", viewDisplayName, fetchXml, layoutXml, true); 
 
     } catch (e) { 
 
      Xrm.Utility.alertDialog("Error: " + e.message); 
 
     } 
 
     var count = 0; 
 
     //count results of fetch xml 
 
     SDK.WEBAPI.executeFetchSync("contract", fetchXml, 
 
      function (results) { 
 
       
 
       for (i=0;i<results.length; i++){ 
 
        count++; 
 
       } 
 
       Xrm.Utility.alertDialog(count); 
 
      }, 
 
      function (error) { 
 
       //Show error 
 
       alertDialog("Error: " + error.message); 
 
      }); 
 
     
 
    }

+0

fetchxmlでサポートされている集計メソッドを試すことができます - https:// msdn Microsoft.com/ja-jp/library/gg309565.aspx –

答えて

1

あなたはカウント集計属性を使用してFetchXMLクエリによって返されたレコードをカウントすることができます。 documentation hereを参照してください。

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' aggregate='true'> 

をそして、カウント集計属性を追加します:

<attribute name='contractid' alias='contract_count' aggregate='count' /> 

は、その後、あなたのexecuteFetchSync成功コールバックで値を取得する:

results.entities[0].contract_count; 

フェッチあなたの最初の行にaggregate='true'を入れて

関連する問題