SetParameter("fetchXml", FetchXml) 在crm 2016 online 中不支持

SetParameter("fetchXml", FetchXml) doesn't support in crm 2016 online

我有这个代码:

function FilterCasesSubgrid() {
    //var CasesSubgrid = Xrm.Page.getControl("contact").getGrid();
    var CasesSubgrid = window.parent.document.getElementById("contact");

    if(CasesSubgrid==null){ 
    setTimeout(function () { FilterCasesSubgrid(); }, 2000); //if the grid hasn’t loaded run this again when it has 
    return;
    }
    var fetchXml ="<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
    "<entity name='contact'>"+
    "<attribute name='fullname' />"+
    "<filter type='and'>"+
    "<condition attribute='fullname' operator='eq' value='s%' />"+
    "</filter>"+
    "</entity>"+
    "</fetch>";
    //Here i set the fetchxml directly to subgrid
    CasesSubgrid.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid 
    CasesSubgrid.control.Refresh(); //refresh the sub grid using the new fetch xml 
}

错误:

TypeError: Cannot read property 'SetParameter' of undefined at FilterCasesSubgrid

您需要等待元素 AND 控件 属性 (CasesSubgrid.control).

这个问题已经有人回答了

此代码不受支持,因此您不应期望它能正常工作。使用任何直接访问 DOM 的函数(即 window.parent.document.getElementById)或使用未在 MSDN SDK 中定义的函数是不受支持的,应该避免。

但是,鉴于您所做的一切似乎只是添加一个过滤器,通过设置现有的 FetchXML 查询,有支持的方法来执行此操作:

var myView = {
    entityType: 1039, // SavedQuery
    id:"{3A282DA1-5D90-E011-95AE-00155D9CFA02}", 
    name: "My Custom View"
}

//Set the view using ContactsIFollow
Xrm.Page.getControl("Contacts").getViewSelector().setCurrentView(myView);

解决方法如下:

  • 我们需要使用window.parent.document.getElementById
  • 等待控件在 DOM.
  • 中加载

所以代码看起来像这样:

function FilterCasesSubgrid() 
{
    //var CasesSubgrid = Xrm.Page.getControl("contact").getGrid();
    var CasesSubgrid =   window.parent.document.getElementById("contact");

    if(CasesSubgrid==null)
    { 
    setTimeout(function () { FilterCasesSubgrid(); }, 2000); //if the grid hasn’t loaded run this again when it has 
    return;
    }
    var fetchXml ="<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
    "<entity name='contact'>"+
    "<attribute name='fullname' />"+
    "<filter type='and'>"+
    "<condition attribute='fullname' operator='eq' value='s%' />"+
    "</filter>"+
    "</entity>"+
    "</fetch>";
    //Here i set the fetchxml directly to subgrid
  if(CasesSubgrid.control != null)
    {
    CasesSubgrid.control.SetParameter("fetchXml", fetchXml); //set the fetch xml to the sub grid 
    CasesSubgrid.control.Refresh(); //refresh the sub grid using the new fetch xml 
      }
  else
    {
       setTimeout(CasesSubgrid, 500);
      }
}