Why unchanged string fields that were setSubmitMode('always') are sent to server side with empty string and not null?

496 views Asked by At

Unchanged string fields that were setSubmitMode('always') are sent to server side with empty string, and not null.

Steps to reproduce:

  1. Let's say we have two fields: jk_name of String type, and jk_dummy of any type.
  2. Set jk_name value to null. Save the record.
  3. Call setSubmitMode('always') on the String type field:

    Xrm.Page.getAttribute("jk_name").setSubmitMode("always");
    
  4. Change jk_dummy value; don't change jk_name value.

  5. Save the record.
  6. Inside your plugin, watch your target's Attributes, and you'll see:

    target -> Attributes -> Keys   -> [0] -> "jk_name"
    target -> Attributes -> Values -> [0] -> ""
    

    meaning, the unchanged jk_name field is passed to the plugin with an empty string value.

Now, compare this to the case where you'd have another field, e.g., jk_code of Integer type. Repeat the steps above, and now you'd see:

    target -> Attributes -> Keys   -> [0] -> "jk_code"
    target -> Attributes -> Values -> [0] -> null

meaning, the unchanged jk_code field is passed to the plugin with a null value.

To generalize:

Unchanged string fields that were setSubmitMode('always') are sent to server side with empty string, while unchanged non-string fields that were setSubmitMode('always') are sent to server side with a null.

Is there a specific reason CRM does that?

2

There are 2 answers

1
jasonscript On

I wonder if this is a result of the primitives used by CRM. See here for a blog post from Guido Preite where he describes the different attribute types and default values

I've copied the main table here:

CRM TYPE                .NET TYPE           CAN HOLD NULL?  DEFAULT VALUE
----------------------+-------------------+----------------+---------------
Single Line of Text     string              Yes 
Option Set              OptionSetValue      Yes 
Two Options             bool                No              false
Image                   byte[]              Yes 
Whole Number            int                 No              0
Floating Point Number   double              No              0.0
Decimal Number          decimal             No              0
Currency                Money               Yes 
Multiple Lines of Text  string              Yes 
Date and Time           DateTime            No              DateTime.MinValue
Lookup                  EntityReference     Yes
2
Mick Healy On

HeyJude

When using the setSubmitMode("always") on a field means that the field will always be included on when saving when using the form where you have set this using JS for a given field.

The function of setSubmitMode from MS docs is as follows - "Sets whether data from the attribute will be submitted when the record is saved." This means the field will always be save to the database when a save occurs on this form.

https://learn.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/attributes/setsubmitmode

To understand why you are receiving this field blank in your target entity you need to understand how the target entity which is passed in the execution context to the plugin behaves.

The target entity will always contain the fields the fields which have changed. On create this means all fields filled out or where submit mode is always. On update all fields which have changed or in your case fields that have been always included in save.

On the converse we can use setSubmitMode to never commit anything to the database by using the "never" parameter. The third option is "dirty" which is the default behaviour of all fields on the form.

I hope this answers your question :)