Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Microsoft Dataverse
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");
}

 

 

 

 

  • ivan_apps Profile Picture
    2,187 Super User 2025 Season 1 on at
    Re: Power apps make fields required using javascript

    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.

  • Ethan_009 Profile Picture
    4,838 Super User 2025 Season 1 on at
    Re: Power apps make fields required using javascript

    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

  • Jonathan Manrique Profile Picture
    2,678 on at
    Re: Power apps make fields required using javascript

    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

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Apps - Microsoft Dataverse

#1
stampcoin Profile Picture

stampcoin 17

#2
ankit_singhal Profile Picture

ankit_singhal 11 Super User 2025 Season 1

#3
mmbr1606 Profile Picture

mmbr1606 9 Super User 2025 Season 1

Overall leaderboard

Featured topics