Friday, 2 March 2012

ForceSubmit vs setSubmitMode in CRM 2011 Javascript

Previously, we used
 crmForm.all.[fieldname].ForceSubmit = true;  
in CRM 4.0 to force the CRM system to submit the form data on disabled fields.

But in CRM 2011, the method is changed to
 Xrm.Page.getAttribute([fieldname]).setSubmitMode("always");  
but the old CRM 4.0 object model is still supported for backward compatibility so that setting true flag to ForceSubmit property seems to be working. But actually, there is a difference between using ForceSubmit and setSubmitMode to save the data in the disabled fields in CRM 2011.

In CRM 2011, there's a Xrm.Page.data.entity.getIsDirty() method, which returns a Boolean value that indicates if any fields in the form have been modified. When we set true flag to ForceSubmit property of the disabled field, the IsDirty flag of the entity won't be changed when the value of that disabled field is updated from the JavaScript code. But when we use setSubmitMode, the IsDirty flag of the entity will be changed when the value of that disabled field is updated.

So, there won't be any difference if setting the value of disabled field is based on the onChange value of another attribute because the entity level IsDirty flag will be updated by the onChange of the latter. But in some scenarios, the value of the disabled field is set by the Ribbon Button event and if we use ForceSubmit entity level IsDirty flag won't be updated which will cause the Xrm.Page.data.entity.save() not working since the method only check the entity level IsDirty flag to perform the action.
In my case, the button event change the value of the disabled field but couldn't force the CRM form to save and ended up finding the difference of these two.

In Conclusion: We should always use the new object model for new developments and we need to replace all the codes which used ForceSubmit with setSubmitMode if the JavaScript codes are from the upgraded CRM 4.0 system.

The parameter values for setSubmitMode function has to be one of the following 3

  • always
  • never
  • dirty

where dirty is default value for editable fields which means that the value will be submitted to the server only when that data value is changed,  never means the value will never be submitted and always means the value will always be submitted regardless of the value change. (the values for the last 2 are quite self-explanatory)
You can read more details on MSDN article.

5 comments:

  1. Very nice post!

    Thank you ;)

    ReplyDelete
  2. Great post, you didn't touch on 'never' and 'dirty' options.

    ReplyDelete
    Replies
    1. Thanks for pointing that out. I've added more info about the other 2 options.

      Delete
  3. Really helpful post..

    ReplyDelete
  4. Very helpful article. Especially re: never and dirty parameters for setSubmitMode.

    ReplyDelete