Hi,
I have created a page in the D365 portal(customer self-service portal) where user can submit their name and email address.
I want to restrict the users from entering duplicate records, which means if they enter the records once they should not be allowed to enter the record with the same email address.
If they do, there should be a message shown stating that the record is already present. Also, would like to show the status of the existing record along with the message.
I tried the below code, however, it is not restricting and the user can submit duplicate records.
if (window.jQuery) {
(function ($) {
if (typeof entityFormClientValidate !== 'undefined') {
var originalValidationFunction = entityFormClientValidate;
if (typeof originalValidationFunction === "function") {
entityFormClientValidate = async function () {
var isValid = await originalValidationFunction.apply(this, arguments);
if (!isValid) {
return false;
}
var inputEmailAddress = $("#emailaddress").val();
try {
const data = await ajaxRequest(`/_api/new_entityname?$select=new_entitynameid&$filter=emailaddress eq '${inputEmailAddress}'`);
console.log(data);
if (data.value.length > 0) {
alert("The email address already exists.");
return false;
} else {
return true;
}
} catch (error) {
console.error("There was an error with the AJAX operation:", error);
return false;
}
};
}
}
})(window.jQuery);
}
function ajaxRequest(url) {
return new Promise((resolve, reject) => {
$.ajax({
type: "GET",
url: url,
contentType: "application/json",
headers: {
"Prefer": "odata.include-annotations=*"
},
success: function (data, textStatus, xhr) {
resolve(data);
},
error: function (xhr, textStatus, errorThrown) {
reject(xhr);
}
});
});
}