This blog aims to provide some technical tips about Microsoft Dynamics CRM 4.0 to D365, SQL Server and .Net.
Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts
Thursday, July 11, 2013
Saturday, December 22, 2012
CRUD operations using Jquery in MS CRM 2011
function onLoad(context) {
var accountId = Xrm.Page.data.entity.getId();
//ReadMethod("MostRecent");
//deleteAccount("41FB760C-2C4B-E211-AB64-0800270618E2");
//WriteMethod("SAI KRISHNA YADAV");
//updateRecord(accountId, "AccountSet");
//ReadMethod("MostRecent");
//deleteAccount("41FB760C-2C4B-E211-AB64-0800270618E2");
//WriteMethod("SAI KRISHNA YADAV");
//updateRecord(accountId, "AccountSet");
}
function ReadMethod(queryName) {
try {
// debugger;
var serverUrl;
//serverUrl = Xrm.Page.context.getServerUrl() + oDataQuery;
var accountId = Xrm.Page.getAttribute("primarycontactid").getValue()[0].id;
$.ajax({
type: "GET",
url: "http://test/CRM2011VM/XRMServices/2011/organizationData.svc/ContactSet?$select=FullName&$filter=ContactId eq guid'" + accountId + "'",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (result) {
//debugger;
if (result != undefined && result.d != undefined && result.d.results != undefined && result.d.results != null) {
// for (var i = 0; i < result.d.results.length; i++) {
alert(result.d.results[0].FullName);
//WriteMethod(result.d.results[0].FullName);
//}
}
},
error: function (res) {
// debugger;
alert(res.responseText);
}
});
} catch (e) {
debugger;
alert(e.Description);
}
}
function WriteMethod() {
try {
var serverUrl;
var serverUrl = Xrm.Page.context.getServerUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc/AccountSet";
// Define attribute values for the CRM object you want created
var newAccount = new Object();
newAccount.Name = "Samplee";
newAccount.Telephone1 = "123";
newAccount.Fax = "456";
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(newAccount);
//Asynchronous AJAX function to Create a CRM record using OData
$.ajax({ type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: serverUrl + ODATA_ENDPOINT,
data: jsonEntity,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
var NewCRMRecordCreated = data["d"];
alert("CRM GUID created: " + NewCRMRecordCreated.AccountId);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("failure");
}
});
} catch (e) {
debugger;
alert(e.Description);
}
}
function deleteAccount(id) {
// Get Server URL
var serverUrl = Xrm.Page.context.getServerUrl();
//The OData end-point
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
//Asynchronous AJAX function to Delete a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: serverUrl + ODATA_ENDPOINT + "/" + "AccountSet" + "(guid'" + id + "')",
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
//Specify the HTTP method DELETE to perform a delete operation.
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
},
success: function (data, textStatus, XmlHttpRequest) {
alert("Record deleted successfully!!!!");
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert("Error while deletion – " + errorThrown);
}
});
}
function updateRecord(id, odataSetName) {
// Get Server URL
var serverUrl = Xrm.Page.context.getServerUrl();
var newAccount = new Object();
newAccount.Name = "Samplee";
newAccount.Telephone1 = "123";
newAccount.Fax = "456";
var jsonEntity = window.JSON.stringify(newAccount);
//The OData end-point
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
//Asynchronous AJAX function to Update a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
data: jsonEntity,
url: Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" + id + "')",
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
//Specify the HTTP method MERGE to update just the changes you are submitting.
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
},
success: function (data, textStatus, XmlHttpRequest) {
alert("Updated successfully");
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (XmlHttpRequest && XmlHttpRequest.responseText) {
alert("Error while updating " + odataSetName + " ; Error – " + XmlHttpRequest.responseText);
}
}
});
}
Add the onLoad function in Form onload/onChange of any field..
function ReadMethod(queryName) {
try {
// debugger;
var serverUrl;
//serverUrl = Xrm.Page.context.getServerUrl() + oDataQuery;
var accountId = Xrm.Page.getAttribute("primarycontactid").getValue()[0].id;
$.ajax({
type: "GET",
url: "http://test/CRM2011VM/XRMServices/2011/organizationData.svc/ContactSet?$select=FullName&$filter=ContactId eq guid'" + accountId + "'",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (result) {
//debugger;
if (result != undefined && result.d != undefined && result.d.results != undefined && result.d.results != null) {
// for (var i = 0; i < result.d.results.length; i++) {
alert(result.d.results[0].FullName);
//WriteMethod(result.d.results[0].FullName);
//}
}
},
error: function (res) {
// debugger;
alert(res.responseText);
}
});
} catch (e) {
debugger;
alert(e.Description);
}
}
function WriteMethod() {
try {
var serverUrl;
var serverUrl = Xrm.Page.context.getServerUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc/AccountSet";
// Define attribute values for the CRM object you want created
var newAccount = new Object();
newAccount.Name = "Samplee";
newAccount.Telephone1 = "123";
newAccount.Fax = "456";
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(newAccount);
//Asynchronous AJAX function to Create a CRM record using OData
$.ajax({ type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: serverUrl + ODATA_ENDPOINT,
data: jsonEntity,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
var NewCRMRecordCreated = data["d"];
alert("CRM GUID created: " + NewCRMRecordCreated.AccountId);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("failure");
}
});
} catch (e) {
debugger;
alert(e.Description);
}
}
function deleteAccount(id) {
// Get Server URL
var serverUrl = Xrm.Page.context.getServerUrl();
//The OData end-point
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
//Asynchronous AJAX function to Delete a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: serverUrl + ODATA_ENDPOINT + "/" + "AccountSet" + "(guid'" + id + "')",
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
//Specify the HTTP method DELETE to perform a delete operation.
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "DELETE");
},
success: function (data, textStatus, XmlHttpRequest) {
alert("Record deleted successfully!!!!");
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert("Error while deletion – " + errorThrown);
}
});
}
function updateRecord(id, odataSetName) {
// Get Server URL
var serverUrl = Xrm.Page.context.getServerUrl();
var newAccount = new Object();
newAccount.Name = "Samplee";
newAccount.Telephone1 = "123";
newAccount.Fax = "456";
var jsonEntity = window.JSON.stringify(newAccount);
//The OData end-point
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
//Asynchronous AJAX function to Update a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
data: jsonEntity,
url: Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" + id + "')",
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
//Specify the HTTP method MERGE to update just the changes you are submitting.
XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
},
success: function (data, textStatus, XmlHttpRequest) {
alert("Updated successfully");
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (XmlHttpRequest && XmlHttpRequest.responseText) {
alert("Error while updating " + odataSetName + " ; Error – " + XmlHttpRequest.responseText);
}
}
});
}
Add the onLoad function in Form onload/onChange of any field..
Subscribe to:
Posts (Atom)