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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Unlock fields using javascript

(0) ShareShare
ReportReport
Posted on by 10

Hi,

I need to check whether certain fields are on the form and based on option set value condition i need to unlock the fields using javascript. I need to do same action for multiple forms in same entity. In few forms only few fields are available. can you please provide sample javascript for it. I'm using Dynamics 365 online version

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Based on your description here is what can help you:

    docs.microsoft.com/.../getvalue - how to get value for an attribute

    docs.microsoft.com/.../setdisabled - how to enable/disable the control

  • Suggested answer
    JayBuddhdevtest Profile Picture
    275 on at

    Hi,

    Here is a sample for you. The logic is that it won't allow to enter date of birth if the person is not male.

    function SetTheFieldState(executionContext){
        //Get the form context
        var formContext = executionContext.getFormContext();
        var Gender = formContext.getAttribute("new_gender").getValue();
        if(Gender === Male){
            //Set field as Editable
            formContext.getAttribute("new_dateofbirth").setDisabled(false);
        }
        else {
            //Set field as Read only
            formContext.getAttribute("new_dateofbirth").setDisabled(true);
        }
    }

    Note: Make sure pass execution context checkbox is checked for the function register.

    If you need more details on Client API reference, you can refer to: https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference

    I hope this helps. Please let me know if you have any questions/concerns.

    Thanks

    Jay

  • Suggested answer
    Carlo Velasco Profile Picture
    787 on at
  • MSCRM _Developer Profile Picture
    10 on at

    Thanks Jay. There ae multiple fields on form. how can i check whether few fields are available in the form using javascript.

    for example

    1. phone and email are in one form

    2. fax and primaryid in another form

    3. address and state in another form

    how to check that field is on form using javascript? The code should run and not fail because a field is missing

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    If the field is not on the form then

    formContext.getControl("fieldschemanamehere") returns null

    so in order to satisfy your requirement you can use syntax that is similar to following:

    function setFieldDisabled(formContext, fieldName, isDisabled) {

    var fieldControl = formContext.getControl(fieldName);

    if (!fieldControl) {

    return;

    }

    fieldControl.setDisabled(isDisabled);

    }

    and here is an example on how to use that function:

    setFieldDisabled(formContext, "telephone1", true);

    setFieldDisabled(formContext, "emailaddress1", true);

    ...e.t.c.

  • Suggested answer
    JayBuddhdevtest Profile Picture
    275 on at

    For JavaScript, it is necessary for you to know which field belongs where. You can pull up a field from different form by using following syntax:

    var quickViewMobilePhoneControl = formContext.getControl("contactQuickForm_contactQuickForm_contact_mobilephone");
    if (quickViewMobilePhoneControl.getAttribute().getValue() == null) {
        quickViewMobilePhoneControl.setVisible(false);
    }

    For more details you can read it here

    Please mark my answer as verified, if that helped you solved the original question asked.

    Thanks

    Jay

  • MSCRM _Developer Profile Picture
    10 on at

    thanks andrew. i need to check nearly 30+ fields so i need to include each field name and check and then disable it?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    You can use array of fields and looping through it disabling/enabling it. Something like following:

    var allFields = ["field1", "field2", "field3", ....];

    for (var i = 0; i < allFields.length; i++){

    setFieldDisabled(formContext, allFields[i], true/false);

    }

  • Suggested answer
    JayBuddhdevtest Profile Picture
    275 on at

    If you don't want to check it for all 30 fields you can simply do the following instead of adding the if statement.

    function SetTheFieldState(executionContext){
        //Get the form context
        var formContext = executionContext.getFormContext();
        var Gender = formContext.getAttribute("new_gender").getValue();
        console.log(Gender);
        if(Gender === Male){
            //Set field as Editable
            formContext.getAttribute("new_dateofbirth").setDisabled(false);
        }
        else {
            //Set field as Read only
            formContext.getAttribute("new_dateofbirth").setDisabled(true);
        }
    }

    And you can see what value is added to the console using developer tools in your browser. Console.log helps you to debug the issue, if the field is not found it will also give you an error.

    Did you look at the approach I mentioned your earlier?

    Please let me know if you have any questions/concerns. I am happy to help you.

    Thanks

    Jay

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans