web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Associate records (N:N relationship) via JavaScript

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Sachith,

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

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

    Hope this helps.

  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    11,401 on at

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


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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans