CRM Dynamics 365 - 多对多关系示例

问题描述 投票:0回答:2

我们正在使用 CRM 动态版本 v9.1 (/api/data/v9.1) 并尝试在实体之间创建多对多关系(例如实体“A”和实体“B”)。根据我们的 CRM API,这两个实体之间的映射由第三个实体维护,该实体基本上保留这两个实体的 ID。以下是我从 Microsoft 文档中找到的创建 POST 请求以创建多对多关系的参考(https://learn.microsoft.com/en-us/power-apps/developer/data-platform/webapi/使用 web-api 创建更新实体关系#创建多对多关系)。

但是,我无法成功创建多对多关系并在响应中收到以下错误 -

  • 回复 { “错误”: { “代码”:“0x80044363”, "message": "EntityRelationship 类型的架构名称不唯一。同名的架构名称已存在。" } }

  • 请求 {“@odata.type”:“#Microsoft.Dynamics.CRM.ManyToManyRelationshipMetadata”, "Entity1LogicalName": "A", “Entity2LogicalName”:“B”, "IntersectEntityName": "A_B", "Entity1IntersectAttribute": "50173618-08e3-df11-806c-005056907671", “Entity2IntersectAttribute”:“1eac5a1d-3adb-df11-806c-005056907671”,
    “IsCustomRelationship”:true, “IsValidForAdvancedFind”:true, "模式名称": "模式名称", “安全类型”:“无”, “IsManaged”:假, "RelationshipType": "多对多关系", "引入版本": "1.0", “元数据ID”:“1d73a50e-47ae-df11-b85b-005056907671”, “已更改”:空
    }

基本上,我想完全了解多对多关系在 CRM Dynamics 365 中的实际工作原理,如果有人能为我提供一个有效的示例,我将不胜感激。

dynamics-crm
2个回答
0
投票

当您具有 N:N 关系时,您应该使用关联/取消关联方法,而不是相交实体上的创建和删除方法。

例如:

var association = {
    "@odata.id": Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.1/sample_customtables(ddd99cd3-bec1-4643-af90-c4b21760ec0d)"
};

$.ajax({
    type: "POST",
    url: Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.1/sample_customtables(c6f35754-6fa8-4e8b-8b58-d4ca958001f4)/sample_sample_customtable_sample_Customtable1/$ref",
    async: true,
    headers: {
        "OData-MaxVersion": "4.0",
        "OData-Version": "4.0",
        "Content-Type": "application/json; charset=utf-8",
        "Accept": "application/json"
    },
    data: JSON.stringify(association),
    success: function (data, textStatus, xhr) {
        console.log("Success");
    },
    error: function (xhr, textStatus, errorThrown) {
        console.log(xhr);
    }
});

您可以使用我的工具 Dataverse REST Builder 根据您的表生成准确的请求(如果您需要原始 HTTP 请求,您可以将集合从 Dataverse REST Builder 导出到 Postman。


0
投票

正如Guido所说,你不应该使用创建/删除方法,事实上,如果你使用Xrm.WebApi,它会告诉你,你不能使用这些方法。作为参考,以下是我使用 Xrm.WebApi 的方法。

async function createAssociation(associationName, primaryEntityName, primaryId, relatedEntities) {
    var manyToManyAssociateRequest = {
        getMetadata: () => ({
            boundParameter: null,
            parameterTypes: {},
            operationType: 2,
            operationName: "Associate"
        }),
        relationship: associationName,
    target: {
        entityType: primaryEntityName,
        id: primaryId
    }}
    
    manyToManyAssociateRequest.relatedEntities = relatedEntities
        
    return await Xrm.WebApi.online.execute(manyToManyAssociateRequest)
}

//studentsToAssign is the return array from a standard Dynamics Lookup that contains the id for a student.
var simpleStudentArray = studentsToAssign.map(student=>{return {entityType: "new_student", id:student.id}})
var res = await createAssociation("new_class_new_student", "new_class", inductionId, simpleStudentArray)
console.log(res) //This is a 204 - no content response

这是它生成的 POST

https://mysite.dynamics.com/api/data/v9.0/new_classes(649c332d-ca46-ee11-be6f-00224894adf7)/new_class_new_student/$ref

"body": {"@odata.context":"https://mysite.dynamics.com/api/data/v9.0/$metadata#$ref",
    "@odata.id":"new_students(574af921-62f8-ec11-813d-005056893571)"}
© www.soinside.com 2019 - 2024. All rights reserved.