Hello,
Thanks for visiting, some goodies for you.
Recently I have met CRM MVP Guido Preite came to know the beauty of webapi on JavaScirpt. Smart code and quick development by XRM.WebApi wrapper for server side calls on clientside. Amazing part is, response is quite fast.
function CreateRecord() {
var data =
{
“name”: “Sample Account”,
“primarycontactid”:
{
“firstname”: “John”,
“lastname”: “Smith”
},
“opportunity_customer_accounts”:
[
{
“name”: “Opportunity associated to Sample Account”,
“Opportunity_Tasks”:
[
{ “subject”: “Task associated to opportunity” }
]
}
]
}
// create account record
Xrm.WebApi.createRecord(“account”, data).then(
function success(result) {
alert(“Account created with ID: ” + result.id);
// perform operations on record creation
},
function (error) {
alert(error.message);
// handle error conditions
}
);
}
function deleteRecord() {
var id = “94ab9061-781d-e911-a981-00224800c940”;
Xrm.WebApi.deleteRecord(“account”, id).then(
function success(result) {
alert(“Account deleted”);
// perform operations on record deletion
},
function (error) {
alert(error.message);
// handle error conditions
}
);
}
function updateRecord() {
var id = “c8714c94-8a1d-e911-a983-00224800c5df”;
// define the data to update a record
var data =
{
“name”: “Updated Sample Account “,
“creditonhold”: true,
“address1_latitude”: 47.639583,
“description”: “This is the updated description of the sample account”,
“revenue”: 6000000,
“accountcategorycode”: 2
}
// update the record
Xrm.WebApi.updateRecord(“account”, id, data).then(
function success(result) {
alert(“Account updated”);
// perform operations on record update
},
function (error) {
alert(error.message);
// handle error conditions
});
}
function retrieveRecord() {
var id = “c8714c94-8a1d-e911-a983-00224800c5df”;
Xrm.WebApi.retrieveRecord(“account”, id, “?$select=name&$expand=primarycontactid($select=contactid,fullname)”).then(
function success(result) {
alert(“Retrieved values: Name: ” + result.name + “, Primary Contact ID: ” + result.primarycontactid.contactid + “, Primary Contact Name: ” + result.primarycontactid.fullname);
// perform operations on record retrieval
},
function (error) {
alert(error.message);
// handle error conditions
}
);
}
Further read // JavaScript source code works for v9.1 https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/xrm-webapi
ping me if you have any queries, hope this helped you.
Thank you!
Original Post https://mscrmsama.wordpress.com/2019/02/07/basics-crude-javascript-v9-x-webapi/