官术网_书友最值得收藏!

Working with number fields

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:

  • Whole Number – this format includes values from -2,147,483,648 to 2,147,483,647 on the default format of None. Additional formats include the following:
    • Duration – this is a drop-down list box with values in minutes, hours and days
    • Time Zone – this is a drop-down list box with time zone options
    • Language – this is a drop-down list box of available languages for the user
  • Floating Point Number – this format includes values from 0 to 1,000,000,000 and the precision can be configured anywhere from 0 to 5 decimal places.
  • Decimal Number – while this format includes the same range of values as the floating point number, the precision can be configured from 0 to 10 decimal places.

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.

Note

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.

Getting ready

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.

How to do it...

In order to work with a number field follow these steps:

  1. Create a new form field, named new_number of type Whole Number, of default format None. Add it to the form.
  2. Add a new JScript resource named JSNumbers.js.
  3. Insert the following code:
    function ReadNumberField()
    {
       var myNumber;
       myNumber = Xrm.Page.getAttribute("new_number").getValue();
       alert("The number in the field is: " + myNumber);
    }
  4. Save and publish the resource.
  5. Attach the function to the OnChange event of the field. Save and publish the form again.
  6. Run the form and change the value in the field to 100. You should be getting the following popup:
    How to do it...

Reading a value from a number field is identical to reading a value from any other regular text field.

There's more...

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);
   }
主站蜘蛛池模板: 个旧市| 鄂伦春自治旗| 长宁县| 原阳县| 英山县| 孟津县| 垦利县| 贺州市| 海口市| 松潘县| 巩义市| 同江市| 陈巴尔虎旗| 武陟县| 浦东新区| 武威市| 晋宁县| 儋州市| 敦化市| 额尔古纳市| 西昌市| 霍山县| 乌什县| 宁都县| 阿图什市| 宝兴县| 门头沟区| 广州市| 涿鹿县| 台前县| 托克托县| 德阳市| 井陉县| 开封市| 渑池县| 武功县| 隆林| 西乌珠穆沁旗| 石阡县| 务川| 军事|