Technology. People. Progress.
Development

Change field requirement levels with JavaScript

Make Dynamics 365 fields required, recommended or optional when values change.

Change field requirement levels with JavaScript

Make fields Mandatory/Recommended/Optional On Form Using Javascript

In Dynamics 365 we can use setRequired method to make a field required/recommended/Optional using Javascript.

Scenario

  • We have a requirement of “mobile phone” field as recommended, “email” field as required and “last name” field as optional whenever the value of “Topic” field changes on Lead Entity.
  • On change of Topic field, JavaScript will execute and make the “mobile phone” field as recommended, “email” field as required and “last name” field as optional.
  • JavaScript (Event Handler) will be added on Lead entity. and On Change Event of Topic of lead. So, JavaScript function will trigger on change of Topic field.
Screenshot from the DVMSKE tutorial
Scenario — screenshot 1 View full size

Syntax

Below is the latest syntax available for making field required, recommended and optional in Dynamics 365 using JavaScript.

Code example
formContext.getAttribute("lastname").setRequiredLevel("none");
Code example
formContext.getAttribute("mobilephone").setRequiredLevel("recommended");
Code example
formContext.getAttribute("emailaddress1").setRequiredLevel("required");

Parameters

NameTypeRequiredDescription
setRequiredLevel(“none”)stringyesMake the field optional
setRequiredLevel(“recommended”)stringyesMake the field business recommended
setRequiredLevel(“required”)stringyesMake the field required.

setRequired Usecase Example

Below is the JavaScript function “onChangeOfTopicOfLead”

        • 1st create form context from execution context passed as 1st parameter
        • Get the value of topic field, if it is not null.
        • Make the lastname field optional.
        • Make the mobile phone field recommended.
        • Make the email field as required.

Code example
function onChangeOfTopicOfLead(executionContext)

{

debugger;

var formContext = executionContext.getFormContext();

var topicValue= formContext.getAttribute("subject").getValue();

if(topicValue!=null)

{

formContext.getAttribute("lastname").setRequiredLevel("none");

formContext.getAttribute("mobilephone").setRequiredLevel("recommended");

formContext.getAttribute("emailaddress1").setRequiredLevel("required");

} }

How to Register JavaScript on OnChange Event of Form

    • Create a New Solution and Name it appropriately, In your solution, add Lead entity then select lead form.
    • From component section select Lead entity added and open Form editor
    • From From editor , click on Form Properties.

How to Register JavaScript on OnChange Event of Form — screenshot 2
How to Register JavaScript on OnChange Event of Form — screenshot 2 View full size

    • Either Create new Web Resource or add JavaScript Function to existing Web Resource.

How to Register JavaScript on OnChange Event of Form — screenshot 3
How to Register JavaScript on OnChange Event of Form — screenshot 3 View full size

  • In the event handler, in control select the “Topic” field and event as on change. Click on add.

How to Register JavaScript on OnChange Event of Form — screenshot 4
How to Register JavaScript on OnChange Event of Form — screenshot 4 View full size

  • In the library select the library where you add your code. And in function copy your js function. And check both the checkboxes. Click on ok.

How to Register JavaScript on OnChange Event of Form — screenshot 5
How to Register JavaScript on OnChange Event of Form — screenshot 5 View full size

  • Click on save and publish. Wait until publish.

OUTPUT

OUTPUT — screenshot 6
OUTPUT — screenshot 6 View full size

Need help applying this to your business?

Talk to our team about your systems and next step.

Talk to our team
Open original image