首页 > 解决方案 > crm中的子网格数据检索

问题描述

我有一个名为竞争对手的子网格动态 365 crm 我必须从该子网格获取数据并将其打印到警报中。我在网上搜索过,但是当我使用时从那里得到的每个解决方案都将错误作为不推荐使用的方法。如何使用 JavaScript 在动态 365 应用程序中获取子网格数据。任何人都可以为此提供示例代码。提前致谢

标签: javascriptdynamics-crmdynamics-crm-2013dynamics-crm-online

解决方案


下面是我在我的一个实例中尝试过的代码。考虑到您希望对 Opportunity 实体发出警报,并且还需要加载表单。

使用调试器,您将获得数据。

function onLoad(executionContext){
debugger;
   var formContext = executionContext.getFormContext();   
var formType= formContext.ui.getFormType();
if(formType && formType!==1 && formContext.data.entity.getId()){
    retreiveOppCompRecords(formContext.data.entity.getId().replace(/[{}]/g, "")).then(
            function success(result) {              
             if(result.entities.length > 0){
                 for(i=0;i<result.entities.length;i++){

                 }               
             }               

            },
            function (error) {
                var alertStrings = {
                    confirmButtonLabel: "OK",
                    text: 'Failed at Function: enableDisableExpiryDate with error: ' + error.message
                };
                var alertOptions = {
                    height: 250,
                    width: 450
                };
                Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
            }
        );
}
}


function retreiveOppCompRecords(recordId){
    var fetchxml = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">',
  '<entity name="opportunity" >',
    '<attribute name="name" />',
    '<filter type="and" >',
      '<condition attribute="opportunityid" operator="eq" value="' + recordId + '" />',
    '</filter>',
    '<link-entity name="opportunitycompetitors" from="opportunityid" to="opportunityid" alias="OppCompetitor" intersect="true" >',
      '<link-entity name="competitor" from="competitorid" to="competitorid" link-type="inner" alias="Competitor" >',
        '<attribute name="websiteurl" alias="CompetitorWebsite" />',
        '<attribute name="name" alias="CompetitoName" />',
      '</link-entity>',
    '</link-entity>',
  '</entity>',
'</fetch>'].join("");

    fetchxml = "?fetchXml=" + encodeURIComponent(fetchxml); 
    return Xrm.WebApi.retrieveMultipleRecords("opportunity", fetchxml);
}

推荐阅读