【问题标题】:Xrm.WebApi how to retrieve attributeXrm.WebApi 如何检索属性
【发布时间】:2023-03-10 15:42:01
【问题描述】:

我正在尝试检索实体组织中查找字段的 ID 和名称。

var fetchxml = `
                <fetch distinct='false' mapping="logical" output-format="xml-platform" version="1.0">
                <entity name="categorization">
                <attribute name="categorizationid"/>
                <attribute name="name"/>
                <attribute name="category1"/>
                <attribute name="category2"/>
                <attribute name="category3"/>
                <order descending="false" attribute="ava_category1"/>
                <filter type="and">
                <condition attribute="statecode" operator="eq" value="0" />
                </filter>
                </entity>
                </fetch>`;

fetchxml = "?fetchXml=" + encodeURIComponent(fetchxml);

Xrm.WebApi.retrieveMultipleRecords("categorization", fetchxml).then(
  function success(results) {
    var data = [];
    for (var r in results) {
      var category1text = results[r].attributes["category1"].name;
      var category1lookup = results[r]["category1"].id;

      if (results[r].attributes["category2"] != undefined) {
        category2text = results[r].attributes["category2"].name;
        var category2lookup = results[r].attributes["category2"].id;
      }

      var category3text = null;

      if (results[r].attributes["category3"] != undefined) {
        category3text = results[r].attributes["category3"].name;
        var category3lookup = results[r].attributes["category3"].id;
      }
    }
  }
);

为什么results[r].attributes["category1"].name 部分不运行?我错过了什么吗?

【问题讨论】:

    标签: javascript dynamics-crm


    【解决方案1】:

    应该是这样的:

    webApi.retrieveMultipleRecords("categorization", fetchxml)
        .then(result => {
            var data = [];
    
            result.entities.forEach(e => {
                var category1text = e.category1.name;
                ... etc.
    

    一些上下文:for in 循环遍历对象的属性。它返回字符串值。您可以使用for of 循环遍历数组。我个人更喜欢带有 lambda 表达式的 .forEach() 函数。

    W3Schools - JavaScript for/in Statement

    【讨论】:

      【解决方案2】:

      最好的方法是使用 odata 来构建您的请求

      ` var option = "?$select=categorizationid,name,_category1_value,_category2_value,_category3_value&$filter=statecode eq 0"; SDK.WEBAPI.retrieveMultipleRecords("categorization", option, function (results) {

                      for (var i = 0; i < results.value.length; i++) {
                          var Cat1LookupGuid = results.value[i]["_category1_value"];
                          var Cat1LookupName = results.value[i]["_category1_value@OData.Community.Display.V1.FormattedValue"];
                          
                           var Cat2LookupGuid = results.value[i]["_category2_value"];
                          var Cat2LookupName = results.value[i]["_category2_value@OData.Community.Display.V1.FormattedValue"];
                          
                          var Cat3LookupGuid = results.value[i]["_category3_value"];
                          var Cat3LookupName = results.value[i]["_category3_value@OData.Community.Display.V1.FormattedValue"];                  
      
      
                      }
                  
              }, function (error) {
                  console.error("your error" + error.message);
              }, function () { }
              );
              
              `
      

      这个工具可以帮助您创建您的 odata 请求 https://github.com/jlattimer/CRMRESTBuilder

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-07
        • 1970-01-01
        • 2011-08-03
        • 1970-01-01
        • 2018-08-26
        • 2021-10-05
        • 1970-01-01
        相关资源
        最近更新 更多