Category: JavaScript

Making a Field Mandatory Based on Forms via JavaScript in Dynamics 365

Hello Readers!

Entity Forms now come with security roles. You can select security roles and who should be allowed to see the form. This is helpful in scenarios where we have multiple forms for different departments. Let’s say we have two departments: Customer Service and Sales

Customer Service deals with people, while Sales deals with organizations.

Issue: Sales Dept. wants ‘Account/Company Name’ to be mandatory on “CONTACT” form , while Customer Service wants it to be non-mandatory. Both the departments do not want any change in the form design as it suits both.

Solution: Create/Copy a new contact form for either of the department. I am creating a new form for Sales department and naming it ‘Contact Sales Form’ (this form will be a copy of main information form).

Mandatory 1

Go to form properties.

Create a web resource and add it on load.

Mandatory2

Here’s the JavaScript I used:

function getFormName()

{

var Form = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();

if (Form == ‘Sales Contact Form’)

{

Xrm.Page.getAttribute(“smsmt_accountname”).setRequiredLevel(“required”);

}

else

{

Xrm.Page.getAttribute(“smsmt_accountname”).setRequiredLevel(“none”);

}

}

 

Hope you find it helpful!

Happy CRM’ing!