【问题标题】:How to update or create N:N relationship using the Client API如何使用客户端 API 更新或创建 N:N 关系
【发布时间】:2019-12-03 11:38:57
【问题描述】:

我正在为模型驱动的应用程序编写一些客户端自定义,我需要更新 N:N 关系中的条目。我一直在关注文档here。我可以从关系表中读取,但尝试更新条目或创建新条目失败。

N:N 关系在自定义实体 new_lastask 和内置 systemuser 实体之间。为 N:N 关系生成的名称是 new_new_lastask_systemuser。我已设法创建和更新其他记录,并且我知道您需要使用具有确切大小写的Schema name,而不是该字段的name 以及更新查找字段时的@odata.bind 语法,但是我不知道该字段的名称应该是什么。

下面的示例代码尝试查找给定用户的 N:N 记录并将其切换给另一个用户,我已经给出了一个使用 updateRecord 方法的示例,但我也尝试过使用 createRecord 并且我得到了同样的错误。

// taskId is the Guid of the custom task entity, and userId is the guid of the user 

var query = "?$filter=new_lastaskid eq " + taskId;

Xrm.WebApi.retrieveMultipleRecords("new_new_lastask_systemuser", query).then(
    function success(result) {
        for (var i = 0; i < result.entities.length; i++) {
            if (result.entities[i].systemuserid===oldUserId) {
                // found the offering user, replace them
                var data = {
                    "systemuserid@odata.bind": "/systemusers" + newUserId
                }

                // try to just change the value of the user attached to the N:N record
                Xrm.WebApi.updateRecord(tableName, result.entities[i].new_new_lastask_systemuserid, data).then(
                    function success(result) {
                        console.log("Successfully updated");
                        // perform operations on record update
                    },
                    function (error) {
                        console.log(error.message);
                        // An error occurred while validating input parameters: Microsoft.OData.ODataException: 
                        // An undeclared property 'systemuserid' which only has property annotations in the payload...
                    }
                );

            }
        }                    
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

这是我在尝试使用name 而不是Schema Name 时尝试更新实体上的任何查找字段时遇到的相同错误,因此我认为这可能与字段,所以我尝试了很多变体(SystemUserId、User、SystemUser、systemuser、user、userid),但没有任何效果。

Schema name 应该属于我的 N:N 表上的查找字段,还是我要通过此 API 以错误的方式处理对这些的修改?

【问题讨论】:

    标签: dynamics-crm powerapps dynamics-crm-webapi


    【解决方案1】:

    我只是无法通过客户端 API 让它工作,所以我只是使用 fetch 调用 web api,遵循来自 8.2 api here 的文档:

    fetch('https://yoururl.crm.dynamics.com/api/data/v9.0/new_lastasks' + taskId + "/new_new_lastask_licensee/$ref", {
        method: 'post',
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "OData-MaxVersion": "4.0",
            "OData-Version": "4.0"
        },
        body: JSON.stringify(
            {"@odata.id": "https://yoururl.crm.dynamics.com/api/data/v9.0/systemusers" + currentUser }
        )
        }).then(function(response) { 
        console.log(response)
        })
    

    【讨论】:

      猜你喜欢
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多