Call associate/disassociate by JS using latest Web API
//--***********************************************************************************************//
//-- [[-- Start --]] Associate/DisAssociate records in N-to-N relationship by Using Web API
//--***********************************************************************************************//
function getEntityPluralName(entityName) {
var pluralName = '';
if (entityName.toUpperCase() == 'SYSTEMUSER') {
pluralName = 'systemusers';
}
else if (entityName.toUpperCase() == 'ROLE') {
pluralName = 'roles';
}
if (entityName.toUpperCase() == 'CONTACT') {
pluralName = 'contacts';
}
if (entityName.toUpperCase() == 'ACCOUNT') {
pluralName = 'accounts';
}
else if (entityName.toUpperCase() == 'SALESORDER') {
pluralName = 'salesorders';
}
return pluralName;
}
function associateManyToManyRecord_Async_ByWebAPI(primaryEntityName, primaryEntityId, relatedEntityName, relatedEntityId, n2nRelationName) {
var primaryEntityPluralName = getEntityPluralName(primaryEntityName);
var relatedEntityPluralName = getEntityPluralName(relatedEntityName);
primaryEntityId = primaryEntityId.replace(/\{|\}/gi, '');
relatedEntityId = relatedEntityId.replace(/\{|\}/gi, '');
var association = {};
association["@odata.id"] = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + relatedEntityPluralName + "(" + relatedEntityId + ")";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + primaryEntityPluralName + "(" + primaryEntityId + ")/" + n2nRelationName + "/$ref", true);
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) {
debugger;
req.onreadystatechange = null;
if (this.status === 204 || this.status === 1223) {
//Success - No Return Data - Do Something
}
else if (this.status === 412) {
if (this.responseText != null && this.responseText != undefined) {
var errorResponse = JSON.parse(this.responseText);
if (errorResponse['error'] != null && errorResponse['error'] != undefined) {
if (errorResponse['error']['message'] != null && errorResponse['error']['message'] != undefined) {
var errorMessage = errorResponse['error']['message'];
alert(errorMessage);//-- 'A record with matching key values already exists.'
//if (errorMessage == 'A record with matching key values already exists.') {
// disassociateManyToManyRecords_Sync_ByWebAPI(primaryEntityName, primaryEntityId, relatedEntityName, relatedEntityId, n2nRelationName);
//}
}
}
}
}
else {
//parent.Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(association));
}
function associateManyToManyRecord_Sync_ByWebAPI(primaryEntityName, primaryEntityId, relatedEntityName, relatedEntityId, n2nRelationName) {
var primaryEntityPluralName = getEntityPluralName(primaryEntityName);
var relatedEntityPluralName = getEntityPluralName(relatedEntityName);
primaryEntityId = primaryEntityId.replace(/\{|\}/gi, '');
relatedEntityId = relatedEntityId.replace(/\{|\}/gi, '');
var association = {};
association["@odata.id"] = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + relatedEntityPluralName + "(" + relatedEntityId + ")";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + primaryEntityPluralName + "(" + primaryEntityId + ")/" + n2nRelationName + "/$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.send(JSON.stringify(association));
debugger;
if (req.status === 204 || this.status === 1223) {
//Success - No Return Data - Do Something
}
else if (req.status === 412) {
if (req.responseText != null && this.req != undefined) {
var errorResponse = JSON.parse(req.responseText);
if (errorResponse['error'] != null && errorResponse['error'] != undefined) {
if (errorResponse['error']['message'] != null && errorResponse['error']['message'] != undefined) {
var errorMessage = errorResponse['error']['message'];
alert(errorMessage);//-- 'A record with matching key values already exists.'
//if (errorMessage == 'A record with matching key values already exists.') {
// disassociateManyToManyRecords_Sync_ByWebAPI(primaryEntityName, primaryEntityId, relatedEntityName, relatedEntityId, n2nRelationName);
//}
}
}
}
}
else {
//parent.Xrm.Utility.alertDialog(this.statusText);
}
}
function disassociateManyToManyRecords_Async_ByWebAPI(primaryEntityName, primaryEntityId, relatedEntityName, relatedEntityId, n2nRelationName) {
debugger;
var primaryEntityPluralName = getEntityPluralName(primaryEntityName);
var relatedEntityPluralName = getEntityPluralName(relatedEntityName);
primaryEntityId = primaryEntityId.replace(/\{|\}/gi, '');
relatedEntityId = relatedEntityId.replace(/\{|\}/gi, '');
var req = new XMLHttpRequest();
req.open("DELETE", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + primaryEntityPluralName + "(" + primaryEntityId + ")/" + n2nRelationName + "(" + relatedEntityId + ")/$ref", true);
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) {
debugger;
req.onreadystatechange = null;
debugger;
if (this.status === 204 || this.status === 1223) {
//Success - No Return Data - Do Something
//successUpdateCallback();
} else {
//Xrm.Utility.alertDialog(this.statusText);
var errorMsg = 'Some error occurred while disassociating records.';
}
}
};
req.send();
}
function disassociateManyToManyRecords_Sync_ByWebAPI(primaryEntityName, primaryEntityId, relatedEntityName, relatedEntityId, n2nRelationName) {
debugger;
var primaryEntityPluralName = getEntityPluralName(primaryEntityName);
var relatedEntityPluralName = getEntityPluralName(relatedEntityName);
primaryEntityId = primaryEntityId.replace(/\{|\}/gi, '');
relatedEntityId = relatedEntityId.replace(/\{|\}/gi, '');
var req = new XMLHttpRequest();
req.open("DELETE", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + primaryEntityPluralName + "(" + primaryEntityId + ")/" + n2nRelationName + "(" + relatedEntityId + ")/$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.send();
debugger;
if (req.status === 204 || req.status === 1223) {
//Success - No Return Data - Do Something
//successUpdateCallback();
} else {
//Xrm.Utility.alertDialog(this.statusText);
var errorMsg = 'Some error occurred while disassociating records.';
alert(errorMsg);
}
}
//--***********************************************************************************************//
//-- [[-- End --]] Associate/DisAssociate records in N-to-N relationship by Using Web API
//--***********************************************************************************************//
Testing associate/disassociate
function testAssociation() {
debugger;
////-- Set params for Web API Call
//var primaryEntityName = 'account';
//var primaryEntityId = '09DE1546-51E7-E311-9408-005056BD2B49';
//var relatedEntityName = 'contact';
//var relatedEntityId = '0AE8BB9B-97E9-E311-940D-005056BD2B49';
//var n2nRelationName = 'new_account_contact';
//-- Test Associate operation by Web API
//associateManyToManyRecord_Async_ByWebAPI(primaryEntityName, primaryEntityId, relatedEntityName, relatedEntityId, n2nRelationName);
//associateManyToManyRecord_Sync_ByWebAPI(primaryEntityName, primaryEntityId, relatedEntityName, relatedEntityId, n2nRelationName);
//-- Test Disassociate operation by Web API
//disassociateManyToManyRecords_Async_ByWebAPI(primaryEntityName, primaryEntityId, relatedEntityName, relatedEntityId, n2nRelationName);
//disassociateManyToManyRecords_Sync_ByWebAPI(primaryEntityName, primaryEntityId, relatedEntityName, relatedEntityId, n2nRelationName);
}

Like
Report
*This post is locked for comments