Hi,
I need to display error message in my model-driven app if the entered value for a column already exists in the table, that is the field should contain unique values and if user is entering existing value (extra space(s) at the beginning or ending of the value should not be considered as different) then an error message needs to be displayed in the model-driven app and restrict from saving this record. How can we use Business Rule here? Please answer in detail.
Thanks & Regards
Solved! Go to Solution.
Hi @Shubhk
I hope this resolves the issue. Please let us know if anything needs on your post. We can help with this.
Please mark the post as Solved If I have answered your question.
Please give it a Thumbs Up if you find the suggestion helpful
Thanks,
Stalin - Learn To Illuminate
Hi @Shubhk
In a model-driven app, there is no option to display error messages using the Business rules based on existing data.
Option 1: Write Javascript onchange of the control. Javascript is capable to check the existing data and display the error message.
Option 2: Write Pre-Validation Plugin to verify and show popup or form notification when data exists. This action will happen when you save the form.
In my opinion, option 1 is the right approach for business.
Thanks,
Stalin - Learn To Illuminate
Thanks @StalinPonnusamy : Do you have any snippet or link for option1? like how the table in dataverse gets accessed from Javascript and the existence of data is verified then showing the error if the data exist.
Thanks.
Thanks, will wait for it. 🙂
Hi @Shubhk
Here is the Javascript code to check entry exists or not
The below check the account number that exists on the Account entity.
Setup:
function verifyAccountNumber(executionContext) {
var formContext = executionContext.getFormContext();
var accountNumber = formContext.getAttribute("accountnumber").getValue();
var uniqueId="028d0060-bb07-4d9e-ab2e-080a18f1e8ec";
try {
var query = "accounts?$select=name,accountnumber&$filter=(accountnumber eq '" + accountNumber + "')";
//Create a request
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/" + query, false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.send();
if(req.readyState == 4)
{
//Success
if (req.status == 200) {
var results = JSON.parse(req.response);
var recordCount = results.value.length;
if (recordCount > 0) {
alert("Account Number Already Exists");
formContext.ui.setFormNotification('Account Number Already Exists', 'WARNING', uniqueId);
}
}
//Failure
else {
alert("Error: " + req.responseText);
}
}
} catch (e) {
Xrm.Utility.alertDialog(e);
}
}
Call the method
2 Outputs
1. Regular Alert
2. Form Notification
Thanks,
Stalin - Learn To Illuminate
Hi @Shubhk
I hope this resolves the issue. Please let us know if anything needs on your post. We can help with this.
Please mark the post as Solved If I have answered your question.
Please give it a Thumbs Up if you find the suggestion helpful
Thanks,
Stalin - Learn To Illuminate
This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.
Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.
User | Count |
---|---|
190 | |
69 | |
50 | |
38 | |
28 |
User | Count |
---|---|
243 | |
112 | |
91 | |
91 | |
71 |