Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

How I updated Javascript resources from Xrm.Page. to ExecutionContext.getFormContext for D365 v9

  • 1.  How I updated Javascript resources from Xrm.Page. to ExecutionContext.getFormContext for D365 v9

    Posted 01-15-2019 08:27 AM
    Edited by Adam Posegate 01-15-2019 08:48 AM
    When developing our own javascript solutions, most online resources were written when Xrm.Page was the way to go. Now that Xrm.Page is going to be deprecated (eventually), I want to make sure those resources, customizations, and configurations will continue working without a second thought.

    This is what I did. If you have feedback as to better practices, please add that in your reply and I can make updates.

    1. Download and install the XrmToolBox for Microsoft Dynamics CRM/365 CE (website link)
    2. Install the plugin Dynamics 365 V9 Javascript Validator
    3. Click 'Retrieve JScript Webresources' and click 'Scan'
    4. This identifies the resources that need updating.
    Follow these steps for each resource
    1. Login as Administrator, in the sitemap select "Settings > Customizations > Customize the System"
    2. Locate the web resource. (You can also do this by locating the specific unmanaged solutions you used to install these)
    3. Open the resource and click 'Text Editor'
    4. Ensure the function names are written to include "executionContext" in them. Example: function FormOnLoad() should become function FormOnLoad(executionContext).
    5. Add this variable:
      var formContext = executionContext.getFormContext();
    6. Locate any calls to Xrm.Page. and replace them with formContext. Example:
      if(Xrm.Page.getAttribute("parentaccountid").getValue() != null)

      becomes

      if(formContext.getAttribute("parentaccountid").getValue() != null)
    7. Save the changes.
    8. Click 'Show Dependencies' and one at a time, open the forms that use this resource.
    9. If this is done on form load, then click 'Form Properties'. 
    10. Edit the event handler, and ensure "Pass execution context as first parameter" is checked (set to true).
    11. Repeat this step for any field that calls this script.
    12. 'Save' and 'Publish' the form.


    #beginner #admin #technical

    ------------------------------
    Adam Posegate
    Fox Valley Technical College
    Appleton WI
    ------------------------------


  • 2.  RE: How I updated Javascript resources from Xrm.Page. to ExecutionContext.getFormContext for D365 v9

    SILVER CONTRIBUTOR
    Posted 01-15-2019 08:45 AM
    Adam,

    This looks like a good approach to me. I think in addition to checking the box to pass the execution context as the first parameter, you'll need to have your executionContext variable specified in your onload function.

    function FormOnLoad(executionContext)

    ------------------------------
    Andy Arndt
    Minitab, Inc.
    State College PA
    ------------------------------



  • 3.  RE: How I updated Javascript resources from Xrm.Page. to ExecutionContext.getFormContext for D365 v9

    Posted 01-15-2019 08:50 AM
    That's really helpful - I added it to the steps. Thank you.

    ------------------------------
    Adam Posegate
    Fox Valley Technical College
    Appleton WI
    ------------------------------



  • 4.  RE: How I updated Javascript resources from Xrm.Page. to ExecutionContext.getFormContext for D365 v9

    TOP CONTRIBUTOR
    Posted 16 days ago
    Edited by Rex Kenley Tan 16 days ago
    Adam

    Just a few tidbits

    1.) Stop using var, use either let or const. In this case it should be const.
    2.) Use js "falsy", it is less redundant.

    formContext.getAttribute("parentaccountid").getValue() != null
    =>
    !!formContext.getAttribute("parentaccountid").getValue()

    I would recommend reading the es2018 standard. The browser js engine has been updated and it would be a shame to not take advantage of all the new js enhancements.

    Cheers!

    ------------------------------
    Rex Kenley Tan, MCP
    Akron OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------



  • 5.  RE: How I updated Javascript resources from Xrm.Page. to ExecutionContext.getFormContext for D365 v9

    Posted 10 days ago
    Thank you! Is using "Let" as simple as replacing "var formContext = executionContext.getFormContext();​" to "let formContext = executionContext.getFormContext();​" ?

    Is it used in the same way throughout, or is there new re-coding required?

    ------------------------------
    Adam Posegate
    Fox Valley Technical College
    Appleton WI
    ------------------------------



  • 6.  RE: How I updated Javascript resources from Xrm.Page. to ExecutionContext.getFormContext for D365 v9

    TOP CONTRIBUTOR
    Posted 10 days ago

    Adam

    Yes, or you can also use const since this object is immutable.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const


    If you need to know why you should stop using var,
    https://scotch.io/tutorials/understanding-hoisting-in-javascript


    Cheers!



    ------------------------------
    Rex Kenley Tan, MCP
    Akron OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------



  • 7.  RE: How I updated Javascript resources from Xrm.Page. to ExecutionContext.getFormContext for D365 v9

    Posted 9 days ago
    Be a little careful with ES6+ recommendations. Some clients still use ancient browsers and hardly anything in ES6 (including let, const) is supported properly.

    A lot of people still think IE is the way to go. Sadly.

    ------------------------------
    Ben Bartle
    IBM
    ------------------------------



  • 8.  RE: How I updated Javascript resources from Xrm.Page. to ExecutionContext.getFormContext for D365 v9

    TOP CONTRIBUTOR
    Posted 9 days ago
    Ben

    This is why it is essential that you have a good tooling system utilizing babel and webpack.
    https://babeljs.io/docs/en/babel-preset-env
    https://flaviocopes.com/babel/

    So have no fear, babel is here. Cheers!


    ------------------------------
    Rex Kenley Tan, MCP
    Akron OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------