Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Associate records (N:N relationship) via JavaScript

Posted on by 1,004

My requirement is to create an association between 2 entity records where there is an N:N relationship between those 2 entities. I built the query with the REST builder. But when I execute it gives the error “Bad Request”. When I examined the response using F12 developer tools the details error is like this: 

“Invalid role specified for entity 'new_contract'in relationship 'new_cancellationreason_contract.Referencing”. 

My relationship name, entity names and GUIDs are double checked and correct. Any idea what this error is??

This is my query which was generated by the REST builder: 

var association = {

    "@odata.id": Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_cancellationreasons(6abb5393-b53e-e711-80d4-005056b44566)"

};

var req = new XMLHttpRequest();

req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_contracts(104A85B0-6DA9-E611-B581-005056B44BD9)/new_cancellationreason_contract/$ref", false);

req.setRequestHeader("Accept", "application/json");

req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

req.setRequestHeader("OData-MaxVersion", "4.0");

req.setRequestHeader("OData-Version", "4.0");

req.onreadystatechange = function() {

    if (this.readyState === 4) {

        req.onreadystatechange = null;

        if (this.status === 204 || this.status === 1223) {

            //Success - No Return Data - Do Something

        } else {

            Xrm.Utility.alertDialog(this.statusText);

        }

    }

};

req.send(JSON.stringify(association));


*This post is locked for comments

  • Suggested answer
    Gee Bee Profile Picture
    Gee Bee 11,397 on at
    RE: Associate records (N:N relationship) via JavaScript

    Hi

    If the relationship name is correct, try interchanging the entities.

    var association = {
    
        "@odata.id": Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_contracts(104A85B0-6DA9-E611-B581-005056B44BD9)"
    
    };
    
    var req = new XMLHttpRequest();
    
    req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_cancellationreasons(6abb5393-b53e-e711-80d4-005056b44566)/new_cancellationreason_contract/$ref", false);
    


  • Verified answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Associate records (N:N relationship) via JavaScript

    Hi Sachith,

    The issue you are facing is explained in the thread below.

    See: community.dynamics.com/.../216277

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans