Client Side Validation for Dynamics 365 and Power Apps Forms With JavaScript and Business Rules

Leave a comment

In Dynamics 365 and Power Apps, when we create forms we will probably require users to fill out certain fields. Fields are defined as Business Required, Business Recommended, and Optional. Required fields mean the user needs to enter data into the field before a record can be saved. The Dynamics 365 Power Platform field definition allows us to set which types of fields fall into which category. Once these are defined, we may want to change these, e.g. from required to not required, based on business logic on the form. Let’s look at how to do this.

I have a new custom entity My New Entity. Let’s add some fields to the entity:

Let’s create 3 fields:

  • My Optional Field – new_myoptionalfield
  • My Business Recommended Field – new_mybusinessrecommendedfield
  • My Business Required Field – new_mybusinessrequiredfield

And let’s add these fields to the form:

Now let’s create a new record for this entity. We see below, with no fields entered:

If we try to save this as is, we get 2 error messages, saying the Name field and My Business Required field need to have data, as these are Business Required fields:

Let’s enter data into these fields and click Save:

We are able to save this record. Note we don’t need data in the My Business Recommended or My Optional Field.

Now let’s say we want to make the My Business Required Field to be not required always, for example, if the My Business Recommended Field is set to “TEST”.

To do this, we can either use Business Rules to set up this rule, or we can use JavaScript.

Let’s first look at Business Rules. In the form designer, select Business Rules, then New Business Rule:

We see below:

With the Condition selected, we can set the condition to My Business Required Field = TEST:

And select Add Set Business Required Action to the Business Rule:

And set the My Business Required Field to Not Business Required:

Save and Activate the rule:

Now, enter TEST into the Business Recommended Field. We see as soon as we tab off the field, the My Business Required Field no longer has the red * indicating it is required, and we are able to save this record as it is without the My Business Required field populated:

Now let’s add some JavaScript to do something similar. If the My Optional Field is populated, set the My Business Required field to not required.

We’ll create a script the usual way, adding an OnLoad to the Form, passing the context:

We will use addOnChange on the new_myoptionalfield field, so if the field is populated, it will make the new_mybusinessrequiredfield field to not required, otherwise set it back to required.

function OnLoad(executionContext) {
   var formContext = executionContext.getFormContext();
   formContext.getAttribute("new_myoptionalfield").addOnChange(DisplayAlert);
}

function DisplayAlert(executionContext) {
     var formContext = executionContext.getFormContext();
     var MyOptionalField = formContext.getAttribute("new_myoptionalfield");
     var MyRequiredField = formContext.getAttribute("new_mybusinessrequiredfield");
     if (MyOptionalField.getValue() != null) {
          MyRequiredField.setRequiredLevel("none");
     } else {
          MyRequiredField.setRequiredLevel("required");
     }
}

Save and Publish the customization.

Now, create a new record. We see the My Business Required field has a red required *, with the Optional field blank:

Entering an Optional value removes the required field requirement:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *