In this article , I am going to explain how to create , retrieve , update , delete entity record in CRM 2011
First need to add these three javascript files as webresource in CRM
jquery1.4.1.min.js
json2.js
SDK.JQuery.js
these files are in SDK under Script folder (sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts)
var primaryContact = null;
Code to create Account Entity record
function createAccount() {
getFirstContactToBePrimaryContact();
var account = {};
account.Name = "Test Account Name";
account.Description = "This account was created by the JQueryRESTDataOperations sample.";
if (primaryContact != null) {
//Set a lookup value
account.PrimaryContactId = { Id: primaryContact.ContactId, LogicalName: "contact", Name: primaryContact.FullName };
}
//Set a picklist value
account.PreferredContactMethodCode = { Value: 2 };
//Set a money value
account.Revenue = { Value: "2000000.00" };
//Set a Boolean value
account.DoNotPhone = true;
//Create the Account
SDK.JQuery.createRecord(
account,
"Account",
function (account) {
alert("The account named \"" + account.Name + "\" was created with the AccountId : \"" + account.AccountId + "\".");
retrieveAccount(account.AccountId)
},
errorHandler
);
}
Code to retrieve Account Entity record
function retrieveAccount(AccountId) {
SDK.JQuery.retrieveRecord(
AccountId,
"Account",
null, null,
function (account) {
alert("Retrieved the account named \"" + account.Name + "\". This account was created on : \"" + account.CreatedOn + "\".");
updateAccount(AccountId);
},
errorHandler
);
}
Code to update Account Entity record
function updateAccount(AccountId) {
var account = {};
account.Name = "Updated Account Name";
account.Address1_AddressTypeCode = { Value: 3 };
account.Address1_City = "Sammamish";
account.Address1_Line1 = "123 Maple St.";
account.Address1_PostalCode = "98074";
account.Address1_StateOrProvince = "WA";
account.EMailAddress1 = "someone@microsoft.com";
SDK.JQuery.updateRecord(
AccountId,
account,
"Account",
function () {
alert("The account record changes were saved");
deleteAccount(AccountId);
},
errorHandler
);
}
Code to delete Account Entity record
function deleteAccount(AccountId) {
if (confirm("Do you want to delete this account record?")) {
SDK.JQuery.deleteRecord(
AccountId,
"Account",
function () {
alert("The account was deleted.");
enableResetButton();
},
errorHandler
);
}
}
Code to retrieve muliple contact Entity records
function getFirstContactToBePrimaryContact() {
SDK.JQuery.retrieveMultipleRecords(
"Contact",
"$select=ContactId,FullName&$top=1",
function (results) {
var firstResult = results[0];
if (firstResult != null) {
primaryContact = results[0];
}
},
errorHandler,
function () {
//OnComplete handler
}
);
}
function errorHandler(error) {
alert(error.message);
}
First need to add these three javascript files as webresource in CRM
jquery1.4.1.min.js
json2.js
SDK.JQuery.js
these files are in SDK under Script folder (sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts)
var primaryContact = null;
Code to create Account Entity record
function createAccount() {
getFirstContactToBePrimaryContact();
var account = {};
account.Name = "Test Account Name";
account.Description = "This account was created by the JQueryRESTDataOperations sample.";
if (primaryContact != null) {
//Set a lookup value
account.PrimaryContactId = { Id: primaryContact.ContactId, LogicalName: "contact", Name: primaryContact.FullName };
}
//Set a picklist value
account.PreferredContactMethodCode = { Value: 2 };
//Set a money value
account.Revenue = { Value: "2000000.00" };
//Set a Boolean value
account.DoNotPhone = true;
//Create the Account
SDK.JQuery.createRecord(
account,
"Account",
function (account) {
alert("The account named \"" + account.Name + "\" was created with the AccountId : \"" + account.AccountId + "\".");
retrieveAccount(account.AccountId)
},
errorHandler
);
}
Code to retrieve Account Entity record
function retrieveAccount(AccountId) {
SDK.JQuery.retrieveRecord(
AccountId,
"Account",
null, null,
function (account) {
alert("Retrieved the account named \"" + account.Name + "\". This account was created on : \"" + account.CreatedOn + "\".");
updateAccount(AccountId);
},
errorHandler
);
}
Code to update Account Entity record
function updateAccount(AccountId) {
var account = {};
account.Name = "Updated Account Name";
account.Address1_AddressTypeCode = { Value: 3 };
account.Address1_City = "Sammamish";
account.Address1_Line1 = "123 Maple St.";
account.Address1_PostalCode = "98074";
account.Address1_StateOrProvince = "WA";
account.EMailAddress1 = "someone@microsoft.com";
SDK.JQuery.updateRecord(
AccountId,
account,
"Account",
function () {
alert("The account record changes were saved");
deleteAccount(AccountId);
},
errorHandler
);
}
Code to delete Account Entity record
function deleteAccount(AccountId) {
if (confirm("Do you want to delete this account record?")) {
SDK.JQuery.deleteRecord(
AccountId,
"Account",
function () {
alert("The account was deleted.");
enableResetButton();
},
errorHandler
);
}
}
Code to retrieve muliple contact Entity records
function getFirstContactToBePrimaryContact() {
SDK.JQuery.retrieveMultipleRecords(
"Contact",
"$select=ContactId,FullName&$top=1",
function (results) {
var firstResult = results[0];
if (firstResult != null) {
primaryContact = results[0];
}
},
errorHandler,
function () {
//OnComplete handler
}
);
}
function errorHandler(error) {
alert(error.message);
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.