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() {
-
- var opportunityid = Xrm.Page.data.entity.getId().substring(1, 37);
-
-
- var ownerid = Xrm.Page.getAttribute("ownerid").getValue()[0].id;
- ownerid = ownerid.substring(1, 37);
-
-
- var serverURL = Xrm.Page.context.getClientUrl();
-
-
- var connection = {};
-
- connection["[email protected]"] = "/opportunities(" + opportunityid + ")";
-
-
- connection["[email protected]"] = "/systemusers(" + ownerid + ")";
-
- connection["[email protected]"] = "/connectionroles(EA5B38CE-A5EE-4CA0-A339-3A51B7DA87FE)";
-
- 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 !!