Hi Everyone,
I Have Stuck in a Query. My requirement is simply that email address should be unique for a contact record. One record has a unique email address and if user creates another record with earlier existing email address, then form should not save and shows a alert message.
I can do this thing by plugin. But I want to do by JS. I have searched a lot on google and find odata JS Query, Xrm toolkit.
But unfortunately, My requirement is not fulfill. I have used below Odataquery code and read about a retrievemultiplerecords function in XrmServicetoolkit.
function Account_onchange() {
debugger;
alert("one");
//var accountId = Xrm.Page.getAttribute('parentcustomerid').getValue();
var accountId = Xrm.Page.getAttribute('emailaddress1');
alert("two");
if (accountId != null) {
var contacts = GetContactsByAccount(accountId);
if (contacts != null && contacts[0].results.length > 0) {
for (var count = 0; count < contacts[0].results.length; count++) {
alert("Rest");
alert(contacts[0].results[count].ContactId);
alert(contacts[0].results[count].LastName);
}
}
alert("There is no data");
}
}
function GetContactsByAccount(accountId) {
debugger;
var serverUrl = "http://" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
var oDataUri = oDataPath + "/ContactSet?$select=ContactId,LastName&$filter=EMailAddress1 eq '" +
accountId +"'";
var jSonArray = new Array();
jQuery.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: oDataUri,
async: false,
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) {
if (data && data.d != null) {
jSonArray.push(data.d);
}
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert("Error : has occured during retrieval of the contacts");
}
});
return jSonArray;
alert(jSonArray);
}
Please help on this.
I will appreciate your view.
Thanks in Advance
Vivek Gupta