Hi Indrasena,
When you create N:N entity, CRM internaly creates an intersect entity (which you can see in the relationship). You can query this entity just like any other entity.
Below is the code to retrieve all the accounts for a specific lead (using the OOB accounleads N:N relationship)
============
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accountleadscollection?$select=accountid&$filter=leadid eq C3CF3B9D-233E-E811-814A-C4346BDC1F11", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var accountid = results.value[i]["accountid"];
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
================
You can use CRM Rest Builder to build query for your custom entities.
github.com/.../CRMRESTBuilder
Hope this helps.