3iL
3iL

Reputation: 2176

Unable to disable time only field on CRM form

I'm trying to disable all fields on Dynamics 365 CE form using javascript using the following code:

Xrm.Page.data.entity.attributes.forEach(function (attribute) {
   var control = Xrm.Page.getControl(attribute.getName());

   if (control) {
      control.setDisabled(true)
   }
});

All fields on the form are disabled except the time only field. My form looks like this:

enter image description here

What am I doing wrong?

Upvotes: 0

Views: 284

Answers (2)

Mohammad Khaleel
Mohammad Khaleel

Reputation: 93

Please use the below code

 Xrm.Page.ui.controls.forEach(function (control, i) {
            if (control && control.getDisabled && !control.getDisabled()) {
                control.setDisabled(true);
            }
        });

Upvotes: 0

Henk van Boeijen
Henk van Boeijen

Reputation: 7948

You are using the Xrm.Page object, which is now deprecated.

Instead use the context passed to the onLoad function of your form. Try this:

function onLoad(context) {
   var formContext = context.getFormContext();

   formContext.data.attributes.forEach(a => {
      a.controls.forEach(c => {
         if (c.getControlType() !== "kbsearch")
            c.setDisabled(true);
      });
   });
}

According to MS Docs control type KB Search should be the only control type not supporting setDisabled().

Upvotes: 1

Related Questions