Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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

  • Verified answer
    Andreas Cieslik Profile Picture
    Andreas Cieslik 9,265 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

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