Create a record using JavaScript
The below snippet code can be used to create a record by java Script. we nedd to add Json2 and jquery1.4.1.min files to webresource which we can get from SDK.
function RecordCreate() {
if(Xrm.Page.getAttribute('new_name') != null) //checking whether the attribute is null or not
{
var name = Xrm.Page.getAttribute('new_name').getValue();
}
if(Xrm.Page.getAttribute('new_mainphone') != null)
{
var mainphone = Xrm.Page.getAttribute('new_mainphone').getValue();
}
var context = Xrm.Page.context;
var serverUrl = context.getServerUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
var CRMObject = new Object();
/////////////////////////////////////////////////////////////
// Specify the ODATA entity collection
var ODATA_EntityCollection = "/AccountSet"; //Entity_NameSet--- type of record to be created
/////////////////////////////////////////////////////////////
// Define attribute values for the CRM object you want created
CRMObject.Name = name; //"Name" is Schema Name of a field in my account entity
CRMObject.Telephone1 = mainphone; //"Telephone1" is Schema Name of a field in my account entity
CRMObject.Address1_Name=name;
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(CRMObject);
//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 + ODATA_EntityCollection,
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) {
alert("success");
var NewCRMRecordCreated = data["d"];
alert("CRM GUID created: " + NewCRMRecordCreated.AccountId); //AccountId is a Primary field in Account Entity.
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("failure");
}
});
}
if(Xrm.Page.getAttribute('new_name') != null) //checking whether the attribute is null or not
{
var name = Xrm.Page.getAttribute('new_name').getValue();
}
if(Xrm.Page.getAttribute('new_mainphone') != null)
{
var mainphone = Xrm.Page.getAttribute('new_mainphone').getValue();
}
var context = Xrm.Page.context;
var serverUrl = context.getServerUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
var CRMObject = new Object();
/////////////////////////////////////////////////////////////
// Specify the ODATA entity collection
var ODATA_EntityCollection = "/AccountSet"; //Entity_NameSet--- type of record to be created
/////////////////////////////////////////////////////////////
// Define attribute values for the CRM object you want created
CRMObject.Name = name; //"Name" is Schema Name of a field in my account entity
CRMObject.Telephone1 = mainphone; //"Telephone1" is Schema Name of a field in my account entity
CRMObject.Address1_Name=name;
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(CRMObject);
//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 + ODATA_EntityCollection,
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) {
alert("success");
var NewCRMRecordCreated = data["d"];
alert("CRM GUID created: " + NewCRMRecordCreated.AccountId); //AccountId is a Primary field in Account Entity.
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("failure");
}
});
}
No comments:
Post a Comment