Anyone let me know what's wrong with the code
function TestContract(executionContext)
{
Xrm.Page.getAttribute("cra4a_contractorsname", "cra4a_country1", "cra4a_agreedproject", "cra4a_groundsitedate". "cra4a_projectstartdate", "cra4a_projecthandoverdate", "cra4a_totaltimetaken", "cra4a_timelinegap", "cra4a_reasonfordelay").setRequiredLevel("required");
}
As stated in other comments, Xrm.Page.getAttribute() takes a single string parameter of a field name. Would be nice to have a .getAttributes() function that did multiple but it’s not supported yet.
Consider also using ‘business rules’ to make field required or not. It’s the no-code way of doing the same thing and reduces your JavaScript footprint for less/easier maintenance in the future.
Hi @sourav456 ,
You can use array method to execute repeated steps faster
function TestContract(executionContext)
{
var formContext = executionContext.getFormContext();
var fields = ["cra4a_contractorsname", "cra4a_country1", "cra4a_agreedproject", "cra4a_groundsitedate", "cra4a_projectstartdate", "cra4a_projecthandoverdate", "cra4a_totaltimetaken", "cra4a_timelinegap", "cra4a_reasonfordelay"];
fields.forEach(fieldName => {
if(formContext.getAttribute(fieldName))
{
formContext.getAttribute(fieldName).setRequiredLevel("required");
}
else
{
console.log("Field does not exist or not on form")
}
});
}
Hope this helps
Hi @sourav456
The code is not correct
You must be as follows
function blockfield(executionContext)
{
var formContext = executionContext.getFormContext();
formContext.getAttribute("field1").setRequiredLevel("required");
formContext.getAttribute("field2").setRequiredLevel("required")
}
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
You can accept more than one post as a solution
stampcoin
17
ankit_singhal
11
Super User 2025 Season 1
mmbr1606
9
Super User 2025 Season 1