Write and debug scripts for CRM for tablets

 

Applies To: Dynamics CRM 2013

Microsoft Dynamics CRM for tablets uses the same form definitions that are used in the web application. Any JavaScript code you add to form event handlers or ribbon commands in the web application may also run within CRM for tablets. There are some differences you need to be aware of.

In this topic

Some Xrm.Page or window object functions won’t work in CRM for tablets

Specify what code will run in CRM for tablets

Specify what commands will display in CRM for tablets

Be aware of differences between CRM for tablets and the web application in a browser

Debugging scripts for CRM for tablets

Some Xrm.Page or window object functions won’t work in CRM for tablets

CRM for tablets doesn’t allow any functions that can block execution of scripts. Common JavaScript functions such as window.alert, window.confirm, and window.prompt won’t work as you expect or will simply throw errors.

Use the Xrm.Utility alertDialog and confirmDialog functions to display messages to users. These functions operate differently than window functions because they don’t stop the processing of scripts until the user closes them. They provide callback functions to allow for an asynchronous response to user input. Using window.confirm, window.prompt, or any other native window function that blocks execution of scripts will throw an error.

Note

If you use window.alert in your form scripts, the message you set will automatically be displayed using Xrm.Utility.alertDialog without a callback function specified, but this is temporary and is already considered deprecated. You should move any code using window.alert to use Xrm.Utility.alertDialog instead.

The following methods won’t work in CRM for tablets.

Xrm.Page.context.getCurrentTheme

Xrm.Page.ui ViewPort Methods

Xrm.Page.data.entity.getDataXml

Xrm.Page.ui control Web resource and IFRAME control methods

Xrm.Page.ui.formSelector object methods

Xrm.Page.ui tab.setDisplayState

Xrm.Page.ui.navigation.items collection

Xrm.Utility.openWebResource

Xrm.Page.ui.refreshRibbon

Most of these functions will simply execute an empty function and return nothing. If you have code that expects a return value from one of these functions it will be undefined.

Specify what code will run in CRM for tablets

When the capabilities of the client API are different you should include code similar to the following to separate the logic you apply for each client.

var isCrmForTablets = (Xrm.Page.context.client.getClient() == "Mobile")
if (isCrmForTablets)
{
 // Code for CRM for tablets only goes here,
}
else
{
 // Code for web browser or CRM for Outlook only goes here.
}
// Code for any client goes here.

Specify what commands will display in CRM for tablets

When you use the <JavaScriptFunction> (RibbonDiffXml) action in your custom command bar (ribbon) commands you can also separate your code using Xrm.Page.context.client.getClient. If the action you want to perform won’t work using CRM for tablets you should include display rules so that the commands won’t appear in CRM for tablets. By default, any command defined will display in CRM for tablets unless you explicitly configure it not to. As a rule, you should define the following display rule and include it in every command unless you know it will work using CRM for tablets.

<DisplayRule Id="My.HideOnModern">
 <CommandClientTypeRule Type="Modern"
                        InvertResult="true" />
</DisplayRule>

Be aware of differences between CRM for tablets and the web application in a browser

Other than the functions described in Some Xrm.Page or window object functions won’t work in CRM for tablets, you should also be aware that there are other differences.

  • Composite Fields
    CRM for tablets implements composite fields differently. CRM for tablets won’t include composite attributes. It automatically substitutes the constituent attributes and display them instead. More information: Write scripts for composite attributes.

  • Web resources and IFRAMEs
    CRM for tablets doesn’t support the display of web resources or IFRAMEs in forms. If any of your code interacts with these types of controls you can expect that the controls won’t appear in a CRM for tablets form and you should apply the guidance in Specify what code will run in CRM for tablets so that that code is only run in the web browser client.

Debugging scripts for CRM for tablets

Because CRM for tablets is an app the normal JavaScript debugging tools you can use in the browser aren’t available. We recommend the following procedure to test and debug your form scripts and ribbon commands

  1. Test your scripts thoroughly using the web application.

  2. As part of testing using the web browser, reverse the criteria when you check for the client using Xrm.Page.context.client.getClient and verify that the logic is appropriate for the experience people should have when using CRM for tablets. The APIs available for CRM for tablets is a subset of the APIs available for the web browser, so you should be able to test your alternate flow in the web browser.

  3. If possible, install CRM for tablets on a computer with Windows 8 and Microsoft Visual Studio installed. Include debugger statements where you want to launch the debugger using Visual Studio.

  4. Finally, use Xrm.Utility.alertDialog to display values from your code within CRM for tablets.

See Also

Write code for Microsoft Dynamics CRM 2013 forms
Use form and field events
Use the Xrm.Page object model
Write scripts for composite attributes
Form scripting quick reference
Client-side programming reference
Command bar or ribbon presentation