CRM 2011 – how to update read only fields with Javascript

I have had an annoying problem which had me pulling out my hair in frustration when I was testing some javascript.

I had created a field called arranged by and it was a user lookup which was read only because I didn’t want people to set it, I wanted to set it when someone booked in a date and then fill in the field.

What was happening was I was setting the field, which you can read about here but then it saved but on the reload the field was blank, AGHGHGHGG.

I then read this forum question – read only field isn’t updated after javascript save

So it turns out that read only fields are not submitted to the database to be saved.  Which in one way is good because it means less traffic going up the pipe to the database but very annoying for me who is trying to set the read only value.

So if you want to save a read only field you need to set the SubmitMode to be always, which ensures the value is sent to the database.

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

8 thoughts on “CRM 2011 – how to update read only fields with Javascript

  1. kivikall July 25, 2012 / 6:54 am

    Good tip! I have a problem with my onSave function though. I’m using XMLHttpRequest to retrieve related information that I would like to set into my read only field. The function is launched when ever another field is changed (onChange) or when the form is saved (onSave). onChange works just fine, but onSave has no effect. I was wondering could it be that onSave doesn’t wait the execution of the function?

    Like

    • Markus Skergeth August 20, 2013 / 6:54 am

      I’m guessing you have solved this on your own some time ago but just for completeness:
      If you want to do asynchronous actions in onSave, you have to cancel the default action in the onSave function (ExecutionObj.getEventArgs().preventDefault();) and then do the save in the callback function.

      Like

  2. C Isaza October 1, 2013 / 4:29 pm

    It had tried several other options and workarounds. It’s great you posted this on the Internet!

    Like

  3. RichCRM June 17, 2016 / 12:34 pm

    Huge help! I had the same hair-tearing problem…

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.