Lock/Unlock Composite fields using Java Script in Dynamics CRM

By | September 4, 2015

Traditionally we use the following way to lock or unlock the fields programmatically,

Xrm.Page.getControl(fieldname).setDisabled(true/false);

But there could be some cases where we would need to lock/unlock the composite fields programmatically.

For example, consider the following scenario,

Suppose that we are populating the Bill To address on Quote as the address of the customer selected on the quote and we want this address should not be changed and should not be editable. In this case we would need to lock the Bill To address fields programmatically.

As we cannot access the composite fields directly, we would need to use some different way to lock the composite fields. The following code snippet shows how to lock the composite fields using JScript.

///This function will lock/unlock the fields defined in the fields array

///It takes the input parameter as “state”, whose value can be “True” or “False”. Depending on the value of this parameter the field will get locked or unlocked.

function setFieldReadOnly(state){

var fields = new Array();

try {

fields = ["billto_line1", "billto_line2", "billto_line3", "billto_postalcode", "billto_stateorprovince", "billto_city", "billto_country"];

//loop through each field of the composite field Bill To

$.each(fields, function (index, item) {

if (Xrm.Page.getControl(“billto_composite_compositionLinkControl_”+item)) {

//lock or unlock the fields

Xrm.Page.getControl(“billto_composite_compositionLinkControl_”+item).setDisabled(state);

}

});

} catch (e) {

Xrm.Utility.alertDialog(e);

}

}

As you can see in the above code snippet, we have to access the composite field as composite field name with the field that belongs to the composite field.

For example, to access the composite field “billto_line1”, we have used

Xrm.Page.getControl(“billto_composite_compositionLinkControl_billto_line1”).setDisabled(true/false);

You can call this function on any event (OnLoad,OnSave,OnChange).lock

Similarly to lock or unlock the field “address1_line1” of the composite field “Address1” on Account we have to use following code

Xrm.Page.getControl(“address1_composite_compositionLinkControl_address1_line1”).setDisabled(true/false);

Conclusion:

To lock/unlock the fields of the Composite field, we have to access the field as, Xrm.Page.getControl(compositeFieldName_compositionLinkControl_fieldBelongToCompositeField).setDisabled(state);

Get more from your Dynamics CRM..Look Beyond “CRM” with  QuickBooks Integration. Read more about Inolink here. For more details get in touch on crm@inogic.com.

Leave a Reply