Skip to content

Commit 9f99213

Browse files
authored
Merge pull request #3681 from MicrosoftDocs/ankarm-webapionline-execute-associate
Client API Associate and Disassociate documentation
2 parents 63a543e + 07e1807 commit 9f99213

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

powerapps-docs/developer/model-driven-apps/clientapi/reference/Xrm-WebApi/online/execute.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,126 @@ Xrm.WebApi.online.execute(deleteRequest).then(
523523
);
524524
```
525525

526+
### Associate a record
527+
The following code sample demonstrates how to perform an Associate operation on collection-valued navigation properties (Many-To-One and Many-To-Many relationships). For single-valued navigation properties (One-To-Many relationships a.k.a Lookup fields), you can perform an Update operation as shown above or use [Xrm.WebApi.updateRecord](../updateRecord.md).
528+
529+
```JavaScript
530+
var Sdk = window.Sdk || {};
531+
532+
/*
533+
* Request to execute an Associate operation.
534+
*/
535+
Sdk.AssociateRequest = function(target, relatedEntities, relationship) {
536+
this.target = target;
537+
this.relatedEntities = relatedEntities;
538+
this.relationship = relationship;
539+
};
540+
541+
// NOTE: The getMetadata property should be attached to the function prototype instead of the
542+
// function object itself.
543+
Sdk.AssociateRequest.prototype.getMetadata = function() {
544+
return {
545+
boundParameter: null,
546+
parameterTypes: {},
547+
operationType: 2, // Associate and Disassociate fall under the CRUD umbrella
548+
operationName: "Associate"
549+
}
550+
};
551+
552+
// Construct the target EntityReference object
553+
var target = {
554+
entityType: "account",
555+
id: "0b4abc7d-7619-eb11-8dff-000d3ac5c7f9"
556+
};
557+
558+
// Construct the related EntityReferences that the Target will be associated with.
559+
var relatedEntities = [
560+
{
561+
entityType: "contact",
562+
id: "180a9aad-7619-eb11-8dff-000d3ac5c7f9"
563+
},
564+
{
565+
entityType: "contact",
566+
id: "753c58b4-7619-eb11-8dff-000d3ac5c7f9"
567+
}
568+
];
569+
570+
// The name of the existing relationship to associate on.
571+
var relationship = "new_account_contact";
572+
573+
var manyToManyAssociateRequest = new Sdk.AssociateRequest(target, relatedEntities, relationship)
574+
575+
Xrm.WebApi.online.execute(manyToManyAssociateRequest).then(
576+
function(result) {
577+
if (result.ok) {
578+
console.log("Status: %s %s", result.status, result.statusText);
579+
// perform other operations as required;
580+
}
581+
},
582+
function(error) {
583+
console.log(error.message);
584+
// handle error conditions
585+
}
586+
);
587+
```
588+
589+
### Disassociate a record
590+
The following code sample demonstrates how to perform a Disassociate operation on collection-valued navigation properties (Many-To-One and Many-To-Many relationships). For single-valued navigation properties (One-To-Many relationships a.k.a Lookup fields), you can perform an Update operation as shown above or use [Xrm.WebApi.updateRecord](../updateRecord.md).
591+
592+
> [!NOTE]
593+
> Unlike the Associate operation which allows associating the target entity record with multiple related entity records in a single operation, the Disassociate operation is limited to only disassociating one entity record from the target entity record per operation.
594+
595+
```JavaScript
596+
var Sdk = window.Sdk || {};
597+
598+
/*
599+
* Request to execute a Disassociate operation.
600+
*/
601+
Sdk.DisassociateRequest = function(target, relatedEntityId, relationship) {
602+
this.target = target;
603+
this.relatedEntityId = relatedEntityId;
604+
this.relationship = relationship;
605+
};
606+
607+
// NOTE: The getMetadata property should be attached to the function prototype instead of the
608+
// function object itself.
609+
Sdk.DisassociateRequest.prototype.getMetadata = function() {
610+
return {
611+
boundParameter: null,
612+
parameterTypes: {},
613+
operationType: 2, // Associate and Disassociate fall under the CRUD umbrella
614+
operationName: "Disassociate"
615+
}
616+
};
617+
618+
// Construct the target EntityReference object
619+
var target = {
620+
entityType: "account",
621+
id: "0b4abc7d-7619-eb11-8dff-000d3ac5c7f9"
622+
};
623+
624+
// The GUID of the related entity record to disassociate.
625+
var relatedEntityId = "180a9aad-7619-eb11-8dff-000d3ac5c7f9";
626+
627+
// The name of the existing relationship to disassociate from.
628+
var relationship = "new_account_contact";
629+
630+
var manyToManyDisassociateRequest = new Sdk.DisassociateRequest(target, relatedEntityId, relationship)
631+
632+
Xrm.WebApi.online.execute(manyToManyDisassociateRequest).then(
633+
function(result) {
634+
if (result.ok) {
635+
console.log("Status: %s %s", result.status, result.statusText);
636+
// perform other operations as required;
637+
}
638+
},
639+
function(error) {
640+
console.log(error.message);
641+
// handle error conditions
642+
}
643+
);
644+
```
645+
526646
### Related topics
527647

528648

powerapps-docs/developer/model-driven-apps/clientapi/reference/Xrm-WebApi/updateRecord.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ Xrm.WebApi.updateRecord("task", "5531d753-95af-e711-a94e-000d3a11e605", data).th
208208
);
209209
```
210210

211+
### Update associations for collection-valued navigation properties
212+
The [Xrm.WebApi.online.execute](online/execute.md) API can be used to associate and disassociate collection-valued navigation properties. This is **NOT** supported for mobile offline scenarios.
213+
211214
### Related topics
212215

213216
[Xrm.WebApi](../xrm-webapi.md)

0 commit comments

Comments
 (0)