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

    GOLD CONTRIBUTOR
    Posted Jan 15, 2019 08:27 AM
    Edited by Adam Posegate Jan 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

    GOLD CONTRIBUTOR
    Posted Jan 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

    GOLD CONTRIBUTOR
    Posted Jan 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 Feb 01, 2019 07:41 AM
    Edited by Rex Kenley Tan Feb 01, 2019 07:42 AM
    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

    GOLD CONTRIBUTOR
    Posted Feb 07, 2019 10:34 AM
    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 Feb 07, 2019 10:42 AM

    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 Feb 08, 2019 04:39 AM
    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 Feb 08, 2019 06:58 AM
    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.
    ------------------------------



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

    SILVER CONTRIBUTOR
    Posted Feb 19, 2019 02:03 PM
    ​What about the system JavaScript files which are showing in the Dynamics 365 V9 JavaScript Validator list? we have to modify those as well.

    Thanks

    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------



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

    GOLD CONTRIBUTOR
    Posted Feb 19, 2019 02:17 PM
    That's a great question - I am not sure of the best way to proceed with system files. I would have assumed those get updated automatically by Microsoft. Perhaps @Rex Kenley Tan​ can give some perspective?

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



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

    TOP CONTRIBUTOR
    Posted Feb 19, 2019 02:23 PM
    Edited by Rex Kenley Tan Feb 19, 2019 02:23 PM

    I would NOT recommend touching any OOTB js files. Let MS do that. If you need to change certain core functionalities then use a custom js file that override that core functionality for that page. 

    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.
    ------------------------------



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

    GOLD CONTRIBUTOR
    Posted Feb 19, 2019 02:18 PM
    Only modify your custom JavaScript web resources.

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



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

    SILVER CONTRIBUTOR
    Posted Feb 19, 2019 03:42 PM

    We can modify the JavaScript to have executionContext as first parameter but for dependencies how to pass executionContext parameter if the dependencies is of type Application Ribbons rather than form?

    I really appreciate all the responses I am getting in this CRM user group forum.

    Thanks a lot.

    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------



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

    GOLD CONTRIBUTOR
    Posted Feb 19, 2019 04:34 PM
    I haven't tested this, but here are two resources.

    This post includes a screenshot of Ribbon Workbench and the Crm Paramter value: https://stackoverflow.com/questions/48202316/how-to-get-formcontext-in-ribbon-command-of-dynamics-365-9-0

    This article describes what it looks like in the ribbon Xml: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/customize-dev/pass-dynamics-365-data-page-parameter-ribbon-actions

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



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

    SILVER CONTRIBUTOR
    Posted Feb 20, 2019 10:39 AM

    addOnKeyPress must be replace with Use a custom control

    how to do this one?
    Thanks for all the responses.

    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------



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

    GOLD CONTRIBUTOR
    Posted Feb 21, 2019 03:59 AM
    You can't at the moment - they removed that functionality before the replacement was available...

    It's meant to be coming in the April update though, so we wait and see.

    ------------------------------
    Mark Carrington
    Chief Technologist
    Data8
    Chester
    ------------------------------



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

    TOP CONTRIBUTOR
    Posted Feb 21, 2019 07:16 AM
    Jeebo

    Try these events

    https://developer.mozilla.org/en-US/docs/Web/Events/keydown
    https://developer.mozilla.org/en-US/docs/Web/Events/beforeinput

    ------------------------------
    Rex Kenley Tan, MCP
    Tallmadge 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.
    ------------------------------



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

    GOLD CONTRIBUTOR
    Posted Feb 21, 2019 09:21 AM
    Be careful with using actual events. Microsoft recommends not using the actual DOM and will likely remove direct access to fields in the future.

    From the link below:

    We do not recommend using jQuery in form scripts and ribbon commands. Most of the benefit provided by jQuery is that it allows for easy cross-browser manipulation of the DOM. This is explicitly unsupported within form scripts and ribbon commands. Restrict your scripts to use the objects/methods available in the Xrm object model.

    Best practices: Client scripting in Customer Engagement apps
    Microsoft remove preview
    Best practices: Client scripting in Customer Engagement apps
    View this on Microsoft >

    I'm working on a special set of tools to make form scripting much easier. It will be ready in a few weeks and will be released first on CRMUG.

    ------------------------------
    Nick Hance
    Principal Developer; President
    Reenhanced
    www.javascriptforcrm.com
    www.buildbettersoftware.com
    Green Lane PA
    ------------------------------



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

    TOP CONTRIBUTOR
    Posted Feb 21, 2019 09:39 AM

    Nick

    The article is talking about jQuery, not DOM events. Once you have the DOM object (by supported methods), there is nothing MS can do to restrict it unless they intend to break the W3C standards. This is very unlikely as well considering MS is moving to chromium with their browser platform.

    Cheers!



    ------------------------------
    Rex Kenley Tan, MCP
    Tallmadge 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.
    ------------------------------



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

    SILVER CONTRIBUTOR
    Posted Feb 22, 2019 01:06 PM
    ​After replacing Xrm.Page with executionContext() in webresources .js file I am getting below javascript error:
    Has anybody come across below error or know what is the cause of it?

    js error

    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------



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

    GOLD CONTRIBUTOR
    Posted Feb 22, 2019 01:17 PM
    This looks like you'll need to look at your log file and the source you modified in order to understand where the error is. Is there any way to share a redacted version of what you changed so we can help?

    ------------------------------
    Nick Hance
    Principal Developer; President
    Reenhanced
    www.javascriptforcrm.com
    www.buildbettersoftware.com
    Green Lane PA
    ------------------------------



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

    GOLD CONTRIBUTOR
    Posted Feb 22, 2019 01:24 PM
    It's not a direct replacement from Xrm.Page to executionContext(). You'll need to check to box to pass the context and then modify the function you're calling like this:

    FormOnLoad: function (executionContext) {

    var formContext = executionContext.getFormContext();

    Then, you can do things like:

    formContext.ui.tabs.get()
    formContext.getAttribute()
    formContext.getAttribute().getValue()




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



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

    SILVER CONTRIBUTOR
    Posted Feb 22, 2019 01:35 PM
    ​I am doing all that below:

    function HideCustomProcessingStatus(executionContext) {

    // fetch all attributes on the form

    var formContext = executionContext.getFormContext();​

    var CustomProcessingStatusAttribute = formContext.getAttribute("xxxxxx");

    if (CustomProcessingStatusAttribute != null) {

    var privileges = CustomProcessingStatusAttribute.getUserPrivilege();

    if (privileges.canRead == false) {

    formContext.getControl("xxxxxx").setVisible(false);

    }

    }

    }

    error



    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------



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

    TOP CONTRIBUTOR
    Posted Feb 22, 2019 01:55 PM

    Jeebo

    Your code "looks" correct (although it could be optimize).

    What's getting my attention is the the error message "is undefined at eval code". It looks to me like Crm is not loading your js file properly. Try delete, add and then publish it again and see if that works.

    Cheers!



    ------------------------------
    Rex Kenley Tan, MCP
    Tallmadge 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.
    ------------------------------



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

    SILVER CONTRIBUTOR
    Posted Mar 07, 2019 11:48 AM
    ​Rex Kenley

    Based on your suggestion I have deleted web resource and added them back in the solution. Clicked Publish.
    But still form is throwing same JavaScript error. Any body faced similar issues when they changed the Xrm.Page to FormContext for CRM v9.1 in custom JavaScript.

    Regards





    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------



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

    TOP CONTRIBUTOR
    Posted Mar 07, 2019 02:04 PM
    Jeebo

    Try getting a screenshot of the F12 Source window to see exactly where it is bombing out.

    ------------------------------
    Rex Kenley Tan, MCP
    Tallmadge 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.
    ------------------------------



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

    SILVER CONTRIBUTOR
    Posted Mar 07, 2019 04:01 PM

    Rex Kenley

    I am getting error at 1st line in the function

    const formContext = executionContext.getFormContext();​
    line.
    it says invalid character.

    not sure the reason why?

    Regards


    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------



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

    SILVER CONTRIBUTOR
    Posted Mar 29, 2019 10:17 AM
    ​Finally it turn out there was space after getFormContext() that was creating issues. Once I deleted the space everything working good.
    One more question how to pass executionContext object to the javascript when the call is from html page within IFrame. Also what about AutoComplete Feature on onKeyPress. As "onKeyPress" event is deprecated as well I don't know what is the replacement custom control will look like.


    ------------------------------
    Jeebo Mathews
    Agricorp
    ------------------------------



If you've found this thread useful, dive deeper into User Group community content by role