最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dynamics crm - CRM Javascript How to Disable Fields on the Form Header - Stack Overflow

programmeradmin1浏览0评论

We're on CRM 2016 and I have a requirement to disable all fields on the Contact form when a certain condition is met. This is the code that I use

var attributes = Xrm.Page.data.entity.attributes.get();
for (var i in attributes) {
    var myattribute = Xrm.Page.data.entity.attributes.get(attributes[i].getName());
    var myname = myattribute.getName();
    if (Xrm.Page.getControl(myname) != null) {
        //alert(myname);
        Xrm.Page.getControl(myname).setDisabled(true);
    }
}

The code works perfectly except for some reason I cannot ever get to the fields on the header. Somehow getControl always returns null for the header fields. All fields on the header are not locked, but the footer itself is locked by default and I'm unable to unlock it.

Is it possible to disable header fields in CRM forms? Do I need to find a way to unlock the header on the form? I tried disabling the field itself with below code but no luck.

Xrm.Page.getControl("mycustomfield").setDisabled(true);

We're on CRM 2016 and I have a requirement to disable all fields on the Contact form when a certain condition is met. This is the code that I use

var attributes = Xrm.Page.data.entity.attributes.get();
for (var i in attributes) {
    var myattribute = Xrm.Page.data.entity.attributes.get(attributes[i].getName());
    var myname = myattribute.getName();
    if (Xrm.Page.getControl(myname) != null) {
        //alert(myname);
        Xrm.Page.getControl(myname).setDisabled(true);
    }
}

The code works perfectly except for some reason I cannot ever get to the fields on the header. Somehow getControl always returns null for the header fields. All fields on the header are not locked, but the footer itself is locked by default and I'm unable to unlock it.

Is it possible to disable header fields in CRM forms? Do I need to find a way to unlock the header on the form? I tried disabling the field itself with below code but no luck.

Xrm.Page.getControl("mycustomfield").setDisabled(true);
Share Improve this question edited Jul 20, 2018 at 2:23 jasonscript 6,1783 gold badges30 silver badges45 bronze badges asked Jul 19, 2018 at 19:44 ichachanichachan 6672 gold badges12 silver badges36 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

I have to add "header_" into my field names in order to make it work

Xrm.Page.getControl("header_mycustomfield").setDisabled(true);

Like answered in another SO thread, you can disable all the controls in header in single shot without hard coding the names.

To optimize your code in question for disabling form controls entirely, use this:

Xrm.Page.ui.controls.forEach(function (control) {
            if (control.setDisabled) {
                control.setDisabled(false);
            }
        });
// Tested On CRM v8.2

// lock a header control
Xrm.Page.getControl("header_statuscode").setDisabled(true);

// this won't work if statusreason is hosted in the header section
Xrm.Page.getControl("statuscode").setDisabled(true);

// lock all controls on form, including header controls
Xrm.Page.ui.controls.forEach(function (control) {
    if (control.setDisabled) {
        control.setDisabled(true);
    }
});

Using formContext you can also disable it.

 if (formContext.getControl("accountid").getAttribute()!= null)  
     formContext.getControl("accountid").setDisabled(true); 
发布评论

评论列表(0)

  1. 暂无评论