
Hi Guys,
I have one entity and enter the data in that entity and am using javascript for compare records in same entity either this record is available in that entity or not.while i save the record i call javascript for compare record using mobile no and email id field if the record is equal to the previous record means they show some popup with ok either you want to continue the record or you going to prevent the record save..i write the code but it is not working,after click cancel the record is save but i want to don't save the record..what i want to make change in the code...
This is my code...
function preventAutoSave(econtext) {
var eventArgs = econtext.getEventArgs();
if (eventArgs.getSaveMode() == 70) {
eventArgs.preventDefault();
} else{
compareCredit();
}
}
function compareCredit(econtext)
{
var email = Xrm.Page.getAttribute("emailaddress").getValue();
var mobile = Xrm.Page.getAttribute("new_mobileno").getValue();
if(email != null && mobile != null){
var cc_Email = email.toLowerCase().trim();
var cc_MobilePhone = mobile.replace(/ +/g, "")
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_creditcardsalemanagementsystems?$select=emailaddress,new_mobileno", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var emailaddress = results.value[i]["emailaddress"];
var resultEmail = emailaddress.toLowerCase().trim();
var mobileno = results.value[i]["new_mobileno"];
var resultMobile = mobileno.replace(/ +/g, "");
if(cc_Email == resultEmail && cc_MobilePhone == resultMobile)
{
Xrm.Utility.confirmDialog("This record has already been created, do you want to continue or cancel", function () {
Xrm.Page.data.entity.save();
},
function (econtext) {
var eventArgs = econtext.getEventArgs();
if (eventArgs.getSaveMode() == 1)
{
eventArgs.preventDefault();
Xrm.Page.getAttribute("new_email").setValue(null);
Xrm.Page.getAttribute("new_mobilephone").setValue(null);
}
});
//alert("It is in Blacklist..");
}
else if(cc_Email == resultEmail && cc_MobilePhone != resultMobile){
alert("Email ID is Match..");
}else if(cc_Email != resultEmail && cc_MobilePhone == resultMobile){
alert("Mobile No is Match..");
}
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}else{
return true;
}
}
Help me Guys...
Thank You
*This post is locked for comments
I have the same question (0)Hi Dinesh,
You have not passed econtext to compareCredit function. Amend the code as per mentioned below and give a try. Thanks.
function preventAutoSave(econtext) {
var eventArgs = econtext.getEventArgs();
if (eventArgs.getSaveMode() == 70) {
eventArgs.preventDefault();
} else{
compareCredit(econtext);
}
}