Dela via


Write and debug scripts for CRM for tablets

 

Applies To: Dynamics CRM 2015

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 72a66f93-92df-42b9-a8fd-b6125c7fe83b#BKMK_alertDialog and 72a66f93-92df-42b9-a8fd-b6125c7fe83b#BKMK_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.d7d0b052-abca-4f81-9b86-0b9dc5e62a66#BKMK_getcurrenttheme

Xrm.Page.ui0375b206-d2bf-4034-a41d-fb476ecb4438#BKMK_ViewPort

Xrm.Page.data.entity.fbaf2e7a-db2f-448f-bd24-6b3ca1ccb28e#BKMK_getDataXml

Xrm.Page.ui control51828fe3-f6ff-4f97-80ed-b06b3a354955#BKMK_webResource

Xrm.Page.ui.0375b206-d2bf-4034-a41d-fb476ecb4438#BKMK_formSelector object methods

Xrm.Page.ui tab.205390b9-3eda-4a2b-87cb-2a8f83f953da#BKMK_tabsetDisplayState

Xrm.Page.ui.0375b206-d2bf-4034-a41d-fb476ecb4438#BKMK_navigation collection

Xrm.Utility.72a66f93-92df-42b9-a8fd-b6125c7fe83b#BKMK_OpenWebResource

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.**d7d0b052-abca-4f81-9b86-0b9dc5e62a66#BKMK_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.**d7d0b052-abca-4f81-9b86-0b9dc5e62a66#BKMK_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.**72a66f93-92df-42b9-a8fd-b6125c7fe83b#BKMK_alertDialog to display values from your code within CRM for tablets.

See Also

Write code for Microsoft Dynamics CRM 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

© 2016 Microsoft. All rights reserved. Copyright