如何使用 JavaScript 和 an:m 关系链接两条记录

发布于 2024-12-03 17:22:57 字数 330 浏览 2 评论 0 原文

我在两个实体之间存在多对多关系。我知道 Dynamics CRM 在数据库中为此创建了一个相交表。我知道如何使用 fetch 命令从自动创建的实体中检索记录。

但现在我想使用 JavaScript 动态地将新记录添加到该表中。这可能吗?

我尝试为此类型创建新记录,但出现以下错误。

create 方法不支持 ["relationship_entity_name"] 类型的实体。

I have a many-to-many relationship between two entities. I know Dynamics CRM creates an intersect table for that in the database. And I know how I can retrieve records with a fetch command from that auto created entity.

But now I want to dynamically add new records to that table using JavaScript. Is this possible?

I've tried to create a new record for this type, but then I got the following error.

The create method does not support entities of type ["relationship_entity_name"].

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌面超妹 2024-12-10 17:22:57

JavaScript SDK 在这一点上并不是最清楚的,但基本原理是 SDK 不允许直接插入相交表。它只允许调用 Associate 方法。下面是两个 TechNet 链接,它们可能会引导您走向正确的方向。

此外,埃维诺在制作CRM JavaScript 库(已更新以参考 archive.org) 公开可用,其中包括 Associate 辅助方法。

最后,一些示例代码:

function AssociateEntities(moniker1, moniker2, relationshipName) {
    var xml;
    var resultXml;

    xml = "<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>";
    xml += "<Request xsi:type='AssociateEntitiesRequest'>";
    xml += "<Moniker1><Id xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker1[0].id + "</Id>";
    xml += "<Name xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker1[0].entityType + "</Name></Moniker1>";
    xml += "<Moniker2><Id xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker2[0].id + "</Id>";
    xml += "<Name xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker2[0].entityType + "</Name></Moniker2>";
    xml += "<RelationshipName>" + relationshipName + "</RelationshipName>";
    xml += "</Request></Execute>";

    resultXml = CallCrmService(xml, "Execute");

    if (resultXml) {
        return "success";
    }
    else {
        return null;
    }
}

The JavaScript SDK is not the clearest on this point, but the basics of it is that the SDK does not allow direct insertion into intersect tables. It only allows calls to the Associate method. Below are two TechNet links which may lead you in the right direction.

Additionally, Avanade has done a wonderful job in making a CRM JavaScript library (updated to reference archive.org) publicly available which includes an Associate helper method.

Finally, some sample code:

function AssociateEntities(moniker1, moniker2, relationshipName) {
    var xml;
    var resultXml;

    xml = "<Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>";
    xml += "<Request xsi:type='AssociateEntitiesRequest'>";
    xml += "<Moniker1><Id xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker1[0].id + "</Id>";
    xml += "<Name xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker1[0].entityType + "</Name></Moniker1>";
    xml += "<Moniker2><Id xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker2[0].id + "</Id>";
    xml += "<Name xmlns='http://schemas.microsoft.com/crm/2006/CoreTypes'>" + moniker2[0].entityType + "</Name></Moniker2>";
    xml += "<RelationshipName>" + relationshipName + "</RelationshipName>";
    xml += "</Request></Execute>";

    resultXml = CallCrmService(xml, "Execute");

    if (resultXml) {
        return "success";
    }
    else {
        return null;
    }
}
王权女流氓 2024-12-10 17:22:57

我最近使用 Xrm.WebApi 做到了这一点。您不能将 createRecord 用于多对多链接实体,这是如何做到的。

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)
}

//driversToAssign is the return array from a standard Dynamics Lookup that contains the id for a driver.
var simpleDriverArray = driversToAssign.map(driver=>{return {entityType: "new_driver", id:driver.id}})
var res = await createAssociation("new_induction_new_driver", "new_induction", inductionId, simpleDriverArray)
console.log(res) //This is a 204 - no content response

I recently did this using Xrm.WebApi. You can't use createRecord for the many-to-many linking entity, this is how to do it.

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)
}

//driversToAssign is the return array from a standard Dynamics Lookup that contains the id for a driver.
var simpleDriverArray = driversToAssign.map(driver=>{return {entityType: "new_driver", id:driver.id}})
var res = await createAssociation("new_induction_new_driver", "new_induction", inductionId, simpleDriverArray)
console.log(res) //This is a 204 - no content response
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文