Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications

|

Use JavaScript in Dynamics CRM to Set the Value in Read-Only Field on a Form

Post Author: Joe D365 |

We've covered some great ways to use JavaScript in Dynamics CRM 2011 in previous posts. This post focuses on a use that we recently employed for a client. In Microsoft Dynamics CRM 2011 or 4.0, you may want to use JavaScript in to set the value in read-only field on a form. However, you may have noticed that after the JavaScript sets the field to Read-Only, it does not save the value when the record is saved.

To clarify, consider the following scenario.

On the Account form, your goal is to have the "Account Number" field become Read-Only if a checkbox is checked. In this example, we'll name the checkbox "Freeze." What you may do to accomplish this is to have the "Account Number" field become Read-Only onChange of the "Freeze" checkbox. You would also want this JavaScript to fire onLoad as well. This will accomplish what you're attempting to do…almost.

Now, when you deploy this code (to a test system of course), you find something strange happens. Let's say a user edits the "Account Number" field, then checks the "Freeze" box immediately after. Upon saving the record, the "Account Number" field reverts back to the previous value. Why is this? Well, prior to checking the "Freeze" field, the "Account Number" field was editable. If we edit that field on the form, the actual field in the database is not modified until we save the record. So, if we edit the field, check the "Freeze" box then Save the record, the value will not be changed because the "Account Number" field was set as Read-Only as soon as we check the box (from the onChange event) prior to saving the record.

To meet this requirement, you could use option #1 below, or use a combination of the two.

#1    Have the JavaScript code fire onLoad of the form ONLY. Not onChange of the "Freeze" field.

Here is the code for the onLoad event:

function accountNumberReadOnly()
{
var freeze = Xrm.Page.data.entity.attributes.get("po_freeze");
var optionSetValue = freeze.getValue();
var optionSetText = freeze.getText();
if (optionSetText == "yes")
{ Xrm.Page.ui.controls.get("accountnumber").setDisabled (true) }
else
{ Xrm.Page.ui.controls.get("accountnumber").setDisabled (false) }

The downside of this method alone is that even though the user checks the box, the Account Number field is not yet Read-Only. To resolve this, add step #2 to the equation.

#2    In your onChange event for the "Freeze" field, instead of using the code above, simply utilize some code to "Save" the record. When a user checks the "Freeze" box, the record is saved and reloaded. The code in #1 will then fire onLoad, thus setting he "Account Number" field as Read-Only.

Here is the code for the onChange event:

function saveRecord()
{
var freeze = Xrm.Page.data.entity.attributes.get("po_freeze");
var optionSetValue = freeze.getValue();
var optionSetText = freeze.getText();
if (optionSetText == "yes")
{ Xrm.Page.data.entity.save(); }
}

Didn't get your fill of JavaScript yet? Here are some more posts on how to use JavaScript in Dynamics CRM:

Happy CRM'ing!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

5 comments on “Use JavaScript in Dynamics CRM to Set the Value in Read-Only Field on a Form”

  1. This seems rather complicated to me.
    We usually use
    ctrl = Xrm.Page.getAttribute(strControlname);
    if (ctrl != null)
    ctrl.setSubmitMode("always");
    after setting the value.
    Regards,
    Jan Nebendahl

  2. I agree with Jan, simply using the setSubmitMode for the field which needs to be saved regardless of being read-only has always seemed a good solution.

    Forcing a save on changing a field might have undesired effects (ie the user can't cancel their other changes by abandoning the form) quite apart from the interruption to the user's flow in filling in the form while they wait for it to reload.

    1. Hi Javed - The best way is probably to create a field of type integer. You can then specify even a min and max value. This is done when we create the field itself in the customization area.

  3. I tried this verbatim and I get "accountNumberReadOnly" is undefined. Any thoughts?

PowerObjects Recommends