function RetrieveEntity2016(clientURL, entityId, entityType, query) { var req = new XMLHttpRequest(); req.open('GET', clientURL + "/api/data/v8.0/"; + entityType + "(" + entityId + ")" + query, 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 /* complete */) { req.onreadystatechange = null; if (this.status == 200) { var data = window.JSON.parse(this.response); console.log("Retrieved " + entityType + " with entity id "+ data.accountid); } else { var error = window.JSON.parse(this.response).error; console.log(error.message); } } }; req.send(); }
Tips and tricks for Dynamics CRM 4, 2011, 2013, 2015 / online from a Dynamics CRM Expert
Monday, March 7, 2016
CRM 2016 WEB API - Retrieve Specific Record
This is a helper function to retrieve and specific record in CRM 2016 using the WEB API
CRM 2016 WEB API - Create record with Javascript
This is a helper function to create records in CRM 2016 using the WEB API
function CreateRecord2016(entity, entityName){ try{ /* Example of entity : note, all fields in lowercase var CRMObject = new Object(); CRMObject.new_name = "test name"; CRMObject.new_customfieldname = "test"; */ var req = new XMLHttpRequest() req.open("POST",encodeURI(clientURL + "/api/data/v8.0/" + entityName + "s"), 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 /* complete */) { req.onreadystatechange = null; if (this.status == 204) { var createdRecordId = this.getResponseHeader("OData-EntityId"); result = createdRecordId; } else { var error = JSON.parse(this.response).error; //console.log(error.message); result = error; } } }; req.send(window.JSON.stringify(entity)); //Asynchronous AJAX function to Create a CRM record using OData return result; }catch(err){ showError(arguments.callee.toString().match(/function\s+([^\s\(]+)/)[1], err.message); } } function showError(functionName, errorMessage) { alert(functionName + ": " + errorMessage); }
Subscribe to:
Posts (Atom)