I came across a very interesting behavior in Microsoft Dynamics CRM 2011 and found out that if the field in any form is set to Disabled or is a Read-Only field the value is not saved into the database. Many of us may not be aware of this and so thought it would be a good topic for a blog. 

If we have a JScript on the On-Load event of a form which calculates and sets the value of any field, it will not be saved in the database if the field is Read-Only or a disabled field. This will hold true whether we set the field to Read-Only through CRM  UI or if we are using the setDisabled() method in JScript for that particular  entity. 

For instance, if we open Properties of a Field in Customizations we can make the field as Read-Only.

 

 In my case, I observed that  the value of the field is properly set  when I re-opened the entity form because there was an OnLoad() event that was resetting it  for me. In other words, when I closed the Entity, the field went to the database as NULL, and when I reopened it JScript set it's value again, making it look like everything was working properly.  

The lesson here is that even though JScript can change the value of a disabled field in the user interface, the value won’t be saved. 

But we can override this behavior with Xrm.Page.data.entity Attribute Method setSubmitMode.   

Xrm.Page.getAttribute("fieldName").setSubmitMode("always"); 

  
We can create a function in our library:

 
SaveEvent(fieldName) 
{
Xrm.Page.getAttribute(fieldName).setSubmitMode("always");
}
 

 

Then in the OnSave event, we can call the function on the field. This forces CRM to save the value of the field.

 

 

 getSubmitMode

getSubmitMode method returns a string indicating when data from the attribute will be submitted when the record is saved.

If Xrm.Page.ui.getFormType() == 1 (Create), a ‘dirty’ submit mode will submit the value only if it is not null. If the record is being updated the ‘dirty’ submit mode will submit the value if it has changed.

attributeObj.getSubmitMode() Returns Value of Type: String

Returns one of the three possible values:

  • always
  • never
  • dirty 

The default value for editable fields is "dirty", which means that the value will be submitted to the server only when that data value is changed, taking performance  into consideration. For fields that do not get updated after the initial save of the record, such as createby, the default value is “never”. To force an attribute value to be submitted whether it has changed or not, use  the setSubmitMode function with the mode parameter set to “always”.