【问题标题】:How to create Entity record with the predefined Id?如何使用预定义的 ID 创建实体记录?
【发布时间】:2019-06-10 10:48:50
【问题描述】:

是否可以使用预定义的 Id 重新创建实体记录? 这个问题的目的是弄清楚如何使用您提供的 id 创建实体记录。

例如,当我删除 ID 为“00-00-02”的客户记录时,它不再存在于 MS Dynamics CRM 中。所以现在我想用它的旧 id ('00-00-02') 使用 REST API 重新创建这个 Account 记录。 谁能建议我怎么做(或者不可能)?

【问题讨论】:

    标签: dynamics-crm dynamics-365 dynamics-crm-365 dynamics-crm-webapi


    【解决方案1】:

    D365 允许您设置实体的主键 Guid。通常,在将数据从一个 D365 组织迁移到另一个时,我们会推送 id。

    以下是通过 Web API 为新帐户设置 accountId 的示例。 (创建于Jason Lattimer's CRMRESTBuilder):

    var entity = {};
    entity.accountid = "008A5AD9-59B7-43BB-BA41-BA59CB5B4769";
    entity.name = "Acme Inc.";
    
    var req = new XMLHttpRequest();
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                var uri = this.getResponseHeader("OData-EntityId");
                var regExp = /\(([^)]+)\)/;
                var matches = regExp.exec(uri);
                var newEntityId = matches[1];
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(entity));
    

    【讨论】:

      【解决方案2】:

      这是相同代码的 OrganizationService 版本:

      Entity account = new Entity("account");
      account.Id = new Guid("008A5AD9-59B7-43BB-BA41-BA59CB5B4769");
      account["name"] = "Acme Inc.";
      service.Create(account);
      

      【讨论】:

      • 很好地了解了向 Guid 的转换。无论出于何种原因,Visual Studio 现在更喜欢将属性内联在构造函数中。
      猜你喜欢
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 2018-12-12
      • 1970-01-01
      相关资源
      最近更新 更多