In this chapter, we will be working with a variation of the text field, and the number fields. By default, Dynamics CRM allows us a few fields with various number formats and data types associated.
The following field types are standard in Dynamics CRM:
Addressing these field types in script uses the same syntax, but we have to be aware of how we initially define them, as the data type cannot be changed after creation.
The only way to change the data type defined for a field is to remove the field from the form, publish the form, and then remove the field from the entity and republish it. Afterwards we can recreate the required field with the updated data type. Pay close attention when taking this approach, as trying to re-import the managed solution after this change will fail. The reason for this is that you are trying to do exactly what you were prevented from doing in the first place.
Open the solution from the previous recipe if not already opened. We will use the same solution package to store this new customization.
The client-side scripting is the same for these field types, and JavaScript will handle automatically the data type of the value we are reading.
In order to work with a number field follow these steps:
new_number
of type Whole Number, of default format None. Add it to the form.JSNumbers.js
.function ReadNumberField() { var myNumber; myNumber = Xrm.Page.getAttribute("new_number").getValue(); alert("The number in the field is: " + myNumber); }
OnChange
event of the field. Save and publish the form again.Reading a value from a number field is identical to reading a value from any other regular text field.
The difference between these fields is in the data that we can push back in this field, for example, if we try the following code to add a text value to a field defined as a Whole Number:
try { Xrm.Page.getAtribute("new_number").setValue("AAA"); } catch(err) { alert("Error: " + err.message); }
3.145.180.81