web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Power apps make fields...
Power Apps
Unanswered

Power apps make fields required using javascript

(0) ShareShare
ReportReport
Posted on by 84

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");
}

 

 

 

 

I have the same question (0)
  • Jonathan Manrique Profile Picture
    2,687 on at

    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

  • Ethan_009 Profile Picture
    4,838 Moderator on at

    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

  • ivan_apps Profile Picture
    2,187 Moderator on at

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard