Thursday, February 28, 2013

Make URL Fields Clickable In Read-Only Forms

In CRM 4.0 when a form is read-only, Email, URL and ticker fields were still able to be opened with a double click. In CRM 2011 this functionality changed so that the field was is totally disabled. With a little JavaScript we can fix that.
function OnLoad() {
    //Read-only forms
    if (Xrm.Page.ui.getFormType() == 4) {
        //Enable specific fields
        var list = ["websiteurl", "emailaddress1", "tickersymbol"];
        EnableFields(list);
        //Or enable all email, ticker, and url fields
        EnableAll();
    }
}

function EnableFields(list) {
    for (var i = 0; i < list.length; i++) {
        Xrm.Page.ui.controls.get(list[i]).setDisabled(false);
        //Prevents message about a change if the user
        //edits the field (still can't save in read-only mode)
        Xrm.Page.getAttribute(list[i]).setSubmitMode("never");
    }
}

Or instead of listing out the fields, you can loop through all of them apply the same logic to all the Email, Ticker, and URL fields.
function EnableAll() {
    var attributes = Xrm.Page.data.entity.attributes.get();
    for (var i in attributes) {
        var name = attributes[i].getName();
        var format = attributes[i].getFormat();
        if (format == "email" || format == "tickersymbol" 
            || format == "url") {
            Xrm.Page.ui.controls.get(name).setDisabled(false);
            Xrm.Page.getAttribute(name).setSubmitMode("never");
        }
    }
}

With any luck this will enable the fields and allow them to be double clicked to launch the URL. The end user can still change the field but since the form is still in read-only mode, there will be no option to save. Setting the submit mode will suppress the pop up warning that the field is dirty if a change was made when the form is closed.