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

Announcements

News and Announcements icon
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,695 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,189 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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 834

#2
Valantis Profile Picture

Valantis 533

#3
Haque Profile Picture

Haque 410

Last 30 days Overall leaderboard