Archive
JScript to Create record and its related record using Web API – CRM 2016
Below is the script snippet to create ‘Account’ record using Web API.
function createAccount() {
var clientUrl = Xrm.Page.context.getClientUrl();var req = new XMLHttpRequest()
req.open(“POST”, encodeURI(clientUrl + “/api/data/v8.0/accounts”), 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.setRequestHeader(“Prefer”, “odata.include-annotations=*”);// Set Account Object
var objAccount = {};
objAccount.name = “Rajeev Associates”;
objAccount.creditonhold = false;
objAccount.accountcategorycode = 1;
objAccount.revenue = 123456;// Create new Contact and Set as ‘Primary Contact’
objAccount.primarycontactid = {};
objAccount.primarycontactid.firstname = “Hello”;
objAccount.primarycontactid.lastname = “Lobo”;// Set existing Contact as ‘Primary Contact’
//objAccount[‘primarycontactid@odata.bind’] = “/contacts(” + { contact GUID } + “)”;//convert JSON object to string
var body = JSON.stringify(objAccount);
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 204) {
var accountUri = this.getResponseHeader(“OData-EntityId”);// Get Account GUID
var accountID = accountUri.split(/[()]/);
accountID = accountID[1];Xrm.Utility.alertDialog(“Created Account ID : ” + accountID);
}
}
};req.send(body);
}
- To set the lookup you need to fetch the Contact GUID first and set to ‘primarycontactid@odata.bind’. ‘@odata.bind’ is suffix and ‘primarycontactid‘ is the schema name of lookup.
- Below is sample script to retrieve Contact GUID by Fullname
function getPrimaryContact() {
var clientUrl = Xrm.Page.context.getClientUrl();
var req = new XMLHttpRequest();
req.open(“GET”, encodeURI(clientUrl + “/api/data/v8.0/contacts?$select=fullname&$filter=fullname eq ‘John Miller'”), 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 = JSON.parse(this.response);
var dat = data.value;// Loop through ‘Contact’ result set
for (var i = 0; i < dat.length; i++) {// Get ‘Contact ID’ from Contact record.
var contactId = dat[i].contactid;
if (contactId) {Xrm.Utility.alertDialog(“Contact ID : ” + contactId);
}
}
}
else {
var error = JSON.parse(this.response).error;
alert(“Error retrieving contact – ” + error.message);
}
}
};req.send();
}
🙂
Create record synchronously using OData and JScript in CRM 2011
Hi,
Below is the Jscript function to create record synchronously using OData and Jscript
function createRecordSync(entityObject, odataSetName) {
var jsonEntity = window.JSON.stringify(entityObject);
// Get Server URL
var serverUrl = Xrm.Page.context.getServerUrl();
//The OData end-point
var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;
var createRecordReq = new XMLHttpRequest();
var ODataPath = serverUrl + ODATA_ENDPOINT;
createRecordReq.open(‘POST’, ODataPath + “/” + odataSetName, false);
createRecordReq.setRequestHeader(“Accept”, “application/json”);
createRecordReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
createRecordReq.send(jsonEntity);
var newRecord = JSON.parse(createRecordReq.responseText).d;
return newRecord;
}
How do I call this method :-
- Create a new .jscript file (i.e., “account.js”)
- Copy & Paste above code
- Add “account.js” as a webresource in CRM
- Add “Json2.js” & “jquery1.4.1.min.js” helper script files as webresources
- Get the helper files from CRM 2011 SDk under path “\sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts”
- To create an “Account”
- Pass “Account” object, OData Set name (i.e.,AccountSet)
var account = new Object();
account.Name = “Rajeev Pentyala”;
var createdAccount = createRecordSync(account, “AccountSet”);
if (createdAccount) {
alert(“New Account Created !!!; Id – “+createdAccount.AccountId);
}
🙂