Working with text fields

In this recipe, we will be retrieving information from the Phone field of a Contact entity. We can work in the same fashion with any other default text fields on any entity form, or with custom fields.

Getting ready

If you have a solution created from the previous chapter, open that solution. If not, create a new solution package. We will be saving all configurations as part of a solution package.

How to do it...

In order to associate our script with a field on the Contact form, we will follow these steps:

  1. Navigate to Settings | Solutions.
  2. Open the previously created solution package or create a new one.
  3. Select Entities, and click on Add Existing to add the Contact entity, as shown in the following screenshot:
    How to do it...
  4. Scroll down until you find Contact and select it. Then, click on OK, as shown in the following screenshot:
    How to do it...
  5. Now your solution will include customization options for Contact:
    How to do it...
  6. Click on Web Resources and add a new JScript resource. We will name it JSContact, as shown in the following screenshot:
    How to do it...
  7. Save and publish, then close the window.
  8. Expand Contact and select Forms. Open the default Information main form by double-clicking on it.
    How to do it...
  9. Double-click on the business phone field. We will be adding a script to the OnChange event of this field. The script will execute when the focus is moved off this field. This functionality is similar to capturing the OnBlur standard JavaScript form event.
  10. On the Field Properties window, select the Events tab at the top.
    How to do it...
  11. In the Event List section, expand Form Libraries, and add the previously added JavaScript resource, as shown in the following screenshot:
    How to do it...
  12. In the Event Handlers section, make sure the selected event is OnChange, and add a new function named ReadBusinessPhone. Make sure the enabled checkbox is selected, then click on OK
    How to do it...
  13. Click on OK again to close the Field Properties window.
  14. Save and publish the Contact form, and then close it.
  15. Before we can test this, we have to add the JavaScript function that will be executed to our web resource we have referenced. Let's go back to our solution package, to Web Resource, and double-click on the JSContact resource.
  16. In the window that opens, click on the Text Editor button. Alternatively, if we have created the JavaScript resource in another text editor, we can just browse to it and load it. It will override the existing file, so be careful not override other functions in the same resource.
    How to do it...
  17. Add the following code to your resource. Pay close attention to the casing, as JavaScript is case sensitive.
    function ReadBusinessPhone()
    {
       var myBusinessPhone;
       myBusinessPhone = Xrm.Page.getAttribute("telephone1").getValue();
       alert("You have entered: " + myBusinessPhone);
    }
  18. Save and publish your resource, then close the Edit Content window.
  19. Save the solution, and then close it.
  20. Now you can test your script. The end result should be similar to the following screenshot:
    How to do it...

How it works...

While our sample script does not really do much other than retrieve the input and return it in a pop-up window, from here on you can do more complex things, such as formatting and validation. Those are standard JavaScript string operations in most cases.

There's more...

Once you have the value of the text field captured in your script, you can do more interesting processing. The following examples describe some of the common actions I use on regular basis.

Retrieving the field name to be used in the script

Looking at the Field properties, as described in step 10, if we open the Details tab, the Name field holds the actual field name we have to use in JavaScript:

Retrieving the field name to be used in the script

Formatting phone numbers

One common formatting I implement on all phone number fields is the formatting of the number. In order to make it readable and user friendly, we can check to make sure the length is either 10 or 11 characters, and then format it for North America in the following way:

+1-XXX-XXX-XXXX

Remove "-" before the count to make sure you are getting the correct length, and also check that all input is numeric.

Validating Country/Region against State/Province relationship

Another common validation that can be implemented is the validation of Country/Region versus State/Province. Out of the box, the State/Province field comes before the Country/Region field, so we can read the value in State/Province, and based on that prepopulate the Country value.

Writing information back to the text field

Once we have processed the value retrieved from the text field, we can write it back to the same or another text field. The line of code to do so is as follows:

Xrm.Page.getAttribute("telephone1").setValue(myBusinessPhone);

This assumes that we will be writing the information to a field named telephone1, and the variable that stores the processed string is named myBusinessPhone.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.46.109