web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Business Rule to show ...
Power Apps
Answered

Business Rule to show error message if provided column value already exists with a model-driven app

(1) ShareShare
ReportReport
Posted on by 36

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

 

 

 

Categories:
I have the same question (0)
  • StalinPonnusamy Profile Picture
    Super User 2024 Season 1 on at

    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

     

  • Shubhk Profile Picture
    36 on at

    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.

  • StalinPonnusamy Profile Picture
    Super User 2024 Season 1 on at

    Hi @Shubhk 

     

    I will create it and send it to you this evening.

     

  • Shubhk Profile Picture
    36 on at

    Thanks, will wait for it. 🙂

  • StalinPonnusamy Profile Picture
    Super User 2024 Season 1 on at

    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: 

     

    • Create new web resource for Javascript say Account.js
    • Copy the below code and update the above new file
    • Call the "verifyAccountNumber" method from onchange event of Account Number

     

    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

    • Add the reference file that you created above
    • Set the function name

    StalinPonnusamy_2-1643756239917.png

     

    2 Outputs

    1. Regular Alert

    StalinPonnusamy_0-1643756045923.png

    2. Form Notification

    StalinPonnusamy_1-1643756087042.png

     


    Thanks,
    Stalin - Learn To Illuminate

  • Verified answer
    StalinPonnusamy Profile Picture
    Super User 2024 Season 1 on at

    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

     

  • Shubhk Profile Picture
    36 on at

    Thanks a lot @StalinPonnusamy. It helped.

  • stevenpetersmi Profile Picture
    207 on at

    Hi

     

    Sorry for the late. I hope you can give me some insight on this as I am replacing the Account Number with the Reference Number, so I am getting errors on 

    Cannot read properties of undefined (reading 'getFormContext')

     

    I need to ask you some questions regarding coding.

     

    unction 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 + "')";

    Dataverse table as I am using column 

    "fin_ProjectReference_num" as number Value 
     
    function verifyRefernceNumber(executionContext) {
        var formContext = executionContext.getFormContext();
        var ProjectReference = formContext.getAttribute("fin_ProjectReference_num").getValue();
        var uniqueId="028d0060-bb07-4d9e-ab2e-080a18f1e8ec";
        try {
            var query = "accounts?$select=name,fin_ProjectReference_num&$filter=(fin_ProjectReference_num eq '" + ProjectReference + "')";
     
    As it returns an error with GetAttrribute
     
    Can you point out what is  accountNumber and accountnumber and which ones are coming from Dataverse, as I assume it more getAttribute("accountnumber")
    var accountNumber = formContext.getAttribute("accountnumber").getValue();

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 366 Most Valuable Professional

#2
11manish Profile Picture

11manish 184

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 99 Super User 2026 Season 1

Last 30 days Overall leaderboard