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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Associate a N to N relationship through the web API during Creation in Javascript

(0) ShareShare
ReportReport
Posted on by

Hi,

So I am trying to use the webapi to clone a current record, but I am getting stuck wjen it comes to copying over the contents of the subgrid to the cloned record.  Everyone works until I get to the N to N association where it tells me an array should be there rather then a string, but I have no idea what that should look like. Any help would be appreciated

var serverURL = parent.Xrm.Page.context.getClientUrl(); 
var commitment = {};
commitment["new_name"] = parent.Xrm.Page.getAttribute("new_name").getValue() + " Clone";

//Recipients var allRows = parent.Xrm.Page.getControl("Recipients").getGrid().getRows();
commitment["new_new_commitment_contact@odata.bind"] = "/contacts("+allRows.get(0).getData().getEntity().getEntityReference().id.replace('{',"").replace('}',"")+")";


var req = new XMLHttpRequest(); req.open("POST", serverURL + "/api/data/v8.0/new_commitments", true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("MSCRMCallerID", (parent.Xrm.Page.getAttribute("ownerid").getValue()[0].id.replace('{',"").replace('}',"")));
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 /* complete */ )
{
req.onreadystatechange = null; if (this.status == 204)
{
}
else { var error = JSON.parse(this.response).error; alert(error.message); } } };
req.send(JSON.stringify(commitment));


 


*This post is locked for comments

I have the same question (0)
  • Verified answer
    Andreas Cieslik Profile Picture
    9,267 on at
    RE: Associate a N to N relationship through the web API during Creation in Javascript

    As you can read here:

    msdn.microsoft.com/.../gg328090.aspx

    odata.bind is only valid for single-valued navigation properties (a lookup field)

    and not for collection-valued navigation properties (n:n)

    You would first need to create the commitment record then you could actually associate an existing contact like this:

    msdn.microsoft.com/.../mt607875.aspx

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans