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.
  1. function CreateConnection() {
  2. //get opportunity id
  3. var opportunityid = Xrm.Page.data.entity.getId().substring(1, 37); //remove {}
  4. //get owner of opportunity
  5. var ownerid = Xrm.Page.getAttribute("ownerid").getValue()[0].id;
  6. ownerid = ownerid.substring(1, 37); //remove {}
  7. //get server url
  8. var serverURL = Xrm.Page.context.getClientUrl();
  9. //prepare entity object
  10. var connection = {};
  11. //set record1id lookup with opportunity
  12. connection["[email protected]"] = "/opportunities(" + opportunityid + ")";
  13. //set record2id lookup with opportunity owner
  14. connection["[email protected]"] = "/systemusers(" + ownerid + ")";
  15. //setup connection role
  16. connection["[email protected]"] = "/connectionroles(EA5B38CE-A5EE-4CA0-A339-3A51B7DA87FE)"; //connection role
  17. connection.effectiveend = new Date();
  18. var req = new XMLHttpRequest();
  19. req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/connections", true);
  20. req.setRequestHeader("OData-MaxVersion", "4.0");
  21. req.setRequestHeader("OData-Version", "4.0");
  22. req.setRequestHeader("Accept", "application/json");
  23. req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  24. req.onreadystatechange = function () {
  25. if (this.readyState === 4) {
  26. req.onreadystatechange = null;
  27. if (this.status === 204) {
  28. Xrm.Utility.alertDialog('Connection Created...');
  29. } else {
  30. Xrm.Utility.alertDialog("Error while creating Connection"+this.statusText);
  31. }
  32. }
  33. };
  34. req.send(JSON.stringify(connection));
  35. }

I hope it will help someone !!