If you are looking for sample code to create a connection record using WebAPI for Dynamics 365 CE, this post will help you. You can change the lookup entity based on your requirement.
- function CreateConnection() {
- //get opportunity id
- var opportunityid = Xrm.Page.data.entity.getId().substring(1, 37); //remove {}
- //get owner of opportunity
- var ownerid = Xrm.Page.getAttribute("ownerid").getValue()[0].id;
- ownerid = ownerid.substring(1, 37); //remove {}
- //get server url
- var serverURL = Xrm.Page.context.getClientUrl();
- //prepare entity object
- var connection = {};
- //set record1id lookup with opportunity
- connection["[email protected]"] = "/opportunities(" + opportunityid + ")";
- //set record2id lookup with opportunity owner
- connection["[email protected]"] = "/systemusers(" + ownerid + ")";
- //setup connection role
- connection["[email protected]"] = "/connectionroles(EA5B38CE-A5EE-4CA0-A339-3A51B7DA87FE)"; //connection role
- connection.effectiveend = new Date();
- var req = new XMLHttpRequest();
- req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/connections", 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.onreadystatechange = function () {
- if (this.readyState === 4) {
- req.onreadystatechange = null;
- if (this.status === 204) {
- Xrm.Utility.alertDialog('Connection Created...');
- } else {
- Xrm.Utility.alertDialog("Error while creating Connection"+this.statusText);
- }
- }
- };
- req.send(JSON.stringify(connection));
- }
I hope it will help someone !!

MohsinPosted Jan 3, 2021, 10:44 AM
In my case, I am creating connection between account and a custom entity. However, facing below error. There are no ConnectionRoleId and AssociatedObjectTypeCode pairs present. Entities being connected: (account,new_showevent); Entity Records being connected: (null,null); Record1ConnectionRoleName: null, Record2ConnectionRoleName: null. ConnectionRoleIds: xxxxxxxx-b49e-4e60-ac51-c54be22d9739, xxxxxxxx-b49e-4e60-ac51-c54be22d9739
Viknaraj ManogararajahPosted Jul 19, 2018, 6:42 AM
Nice Article...........
Necdet SaritasPosted Jun 28, 2018, 7:15 PM
What is the variable 'workorderid'?