cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Shubhk
Helper I
Helper I

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

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
StalinPonnusamy
Super User
Super User

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

 

View solution in original post

7 REPLIES 7
StalinPonnusamy
Super User
Super User

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
Helper I
Helper I

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.

Hi @Shubhk 

 

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

 

Thanks, will wait for it. 🙂

StalinPonnusamy
Super User
Super User

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

StalinPonnusamy
Super User
Super User

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
Helper I
Helper I

Thanks a lot @StalinPonnusamy. It helped.

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Top Solution Authors
Top Kudoed Authors
Users online (2,423)