Skip to main content

Dynamics V9 Set Field Requirement Level JavaScript

Quick reusable JavaScript function for setting the requirement level of a field.

The below sets the requirement level for a single field called new_myfieldname to required.

function setRequirementlevel(executionContext)
{
var formContext = executionContext.getFormContext();
//Requirement levels available are
//"none"
//"required"
//"recommended"
var level ="required";
formContext.getAttribute("new_myfieldname").setRequiredLevel(level);
}

Or if you want to set the levels of multiple fields at the same time

//Fields to set requirement levels
//[fieldname,requirementlevel]
var fields = [
["firstname","none" ],
["lastname","none" ],
["fullname","none" ],
["fullname1","none" ],
["emailaddress1","required" ],
["telephone1","none" ],
["address1_line1","none" ],
["address1_postalcode","none" ],
["address1_country","none" ],
["address1_city","none" ]
];
var formContext;
//call this function
function setRequirementLevel( executionContext)
{
//get formcontext
formContext = executionContext.getFormContext();
//run the set level function for each field
fields.forEach(setLevel);

}

function setLevel(item)
{

formContext.getAttribute(item[0]).setRequiredLevel(item[1]);
}

Charles Osei

Dynamics 365 Consultant @ RSM MCSE Business Applications Computer Science – University of Lincoln 2017 Nottingham D365/CRM user group Chapter Leader TDG – Community leader Twitter @charliedevxyz LinedIn https://www.linkedin.com/in/charlesosei/

Leave a Reply

%d bloggers like this: