Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

How to Get executionContext in DeveloperTools Console

  • 1.  How to Get executionContext in DeveloperTools Console

    TOP CONTRIBUTOR
    Posted Feb 10, 2020 12:36 PM
    Edited by Ryan Perry Feb 10, 2020 12:40 PM
    Hi guys, looking to level up my dev game.

    In Chrome Dev tools console, I can access the global context, by simply typing the getGlobalContext method within Xrm.Utility namespace.

    This is great for getting anything I want out of the global context (ie client, orgSettings, UserSettings, etc). But...

    How do I get the formContext in the Dev Console?
    Since an event handler is not firing and passing the executionContext, I don't have / know how to get to executionContext to access the getFormContext method. Simply typing to access either "formContext" or "executionContext" generate an undefined error.

    I'd like to do this to be able to quickly get/set values of option sets, lookups, partylists, etc. that contain more/different info than what is visible directly to the user.

    Thanks for any pointers in advance.

    ------------------------------
    Ryan Perry
    Business Systems Analyst
    Auric Solar
    ------------------------------
    The first step toward cloud success. - Migrate from CRM to D365 with expert guidance from Microsoft. I'm Ready


  • 2.  RE: How to Get executionContext in DeveloperTools Console

    TOP CONTRIBUTOR
    Posted Feb 10, 2020 01:47 PM
    Ryan,
    There are multiple ways of doing it:
    1. Use Xrm.Page to access the context.
    2. You can add a hander to onload and store context to some globally accessible variable and use it.
    3. Usually you would access object model from onload/onchange or other event handlers and context is available there when it's passed inside as a parameter.

    ------------------------------
    Andrew Butenko
    ------------------------------

    The first step toward cloud success. - Migrate from CRM to D365 with expert guidance from Microsoft. I'm Ready


  • 3.  RE: How to Get executionContext in DeveloperTools Console

    TOP CONTRIBUTOR
    Posted Feb 11, 2020 09:40 AM
    @Andrew Butenko,

    Thanks.  #1 isn't viable long-term, as we are moving to the UCI and Xrm.Page is deprecated.  Can you provide an example of #2? If storing, is it passed as a reference or by value? IE if other functions later change values, will referencing one stored onload see the updated values?  Is there not a way to update / retrieve these without having to configure a trigger to pass the execution context first? IE from somewhere other than an event passed executionContext, such as a formContext that is always loaded on form load.

    ------------------------------
    Ryan Perry
    Business Systems Analyst
    Auric Solar
    ------------------------------

    The first step toward cloud success. - Migrate from CRM to D365 with expert guidance from Microsoft. I'm Ready


  • 4.  RE: How to Get executionContext in DeveloperTools Console

    TOP CONTRIBUTOR
    Posted Feb 11, 2020 10:14 AM
    @Ryan Perry

    Technically, you will have the Xrm.Page as long as ClientGlobalContext.js.aspx exists.
    "NOTE: parent.Xrm will be removed after the removal of the ClientGlobalContext.js.aspx page; dates yet to be announced."
    https://docs.microsoft.com/en-us/power-platform/important-changes-coming

    In js, objects are always passed by reference (because you are passing the reference value to the object).

    The formContext exists somewhere, but the form events is the only supported way to get to it. What functionality are you looking for that is not available with Utility?

    ------------------------------
    Rex Kenley Tan, MCSA, MCSD
    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.
    ------------------------------

    The first step toward cloud success. - Migrate from CRM to D365 with expert guidance from Microsoft. I'm Ready


  • 5.  RE: How to Get executionContext in DeveloperTools Console

    TOP CONTRIBUTOR
    Posted Feb 11, 2020 12:15 PM
    Thanks, @Rex Kenley Tan​​

    I just completed updates to our scripts to make them UCI compliant.  During the process, some of the changes required checking for equality, rather than equivalency. IE when replacing "==" with "===" as recommended by Visual studio, typecasting became more important and some of our function calls required updates as I had previously passed in in integers (0,1) instead of boolean (true, false) values.  While it is possible to write a console.log() line, or add debugger to the code to see exactly what values are being stored / passed by two option sets (boolean true, false), global option sets getValue vs getText (integers vs strings), lookups (arrays of entityReferences), etc., I was looking for a faster way to check values using the developer console.

    If I could simply hit F12 and type formContext.getAttribute("attributename").getValue() or .getText(), to see what it exactly it would be returning, or start the debugger without having to have a "debugger;" line in the code already, that would be awesome.   .... On a similar note, I tried doing this with Xrm.Utility.getEntityMetaData to get metadata and got an object of methods, but not the actual metadata. What am I missing here?

    The project is complete, but as noted, You don't know what you don't know, and I'd like to step up my game / find faster ways to check things than having to edit the script, save, and publish it. Thanks again for your insight.

    ------------------------------
    Ryan Perry
    Business Systems Analyst
    Auric Solar
    ------------------------------

    The first step toward cloud success. - Migrate from CRM to D365 with expert guidance from Microsoft. I'm Ready


  • 6.  RE: How to Get executionContext in DeveloperTools Console

    TOP CONTRIBUTOR
    Posted Feb 11, 2020 12:59 PM

    @Ryan Perry

    Have you tried VS Code? It is WAY better than visual studio. You shouldn't have to be inundated with typecasting, unless you are using TS.

    Would you mind sharing some of your code? I am curious about the equivalency issues that you are having.

    Zero is considered falsy in js, but if you want "true boolean" you can !!0.

    getEntityMetaData is a promise. I haven't tried it but you need to add

    getEntityMetaData("appointment").then(r => {console.log();})


    Hopefully this would work.

    Send me your code, I can review it if you want.



    ------------------------------
    Rex Kenley Tan, MCSA, MCSD
    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.
    ------------------------------

    The first step toward cloud success. - Migrate from CRM to D365 with expert guidance from Microsoft. I'm Ready


  • 7.  RE: How to Get executionContext in DeveloperTools Console

    SILVER CONTRIBUTOR
    Posted Feb 24, 2020 05:59 PM
    Hi Ryan,

    I saw your question last week, and today I was doing something similar just for fun. I hope this will help you:

    Please let me know if you have any questions/concerns.

    Thanks
    Jay


    ------------------------------
    JayBuddhdev
    https://www.powerappsd365.com
    Regina SK Canada
    ------------------------------

    The first step toward cloud success. - Migrate from CRM to D365 with expert guidance from Microsoft. I'm Ready


  • 8.  RE: How to Get executionContext in DeveloperTools Console

    TOP CONTRIBUTOR
    Posted Feb 25, 2020 10:48 PM
    Thanks Jay,  As you and rex have noted, the Xrm.Page approach does work. Although deprecated, it looks like it will probably be around for a bit.
    This was mostly a pencil sharpening curiousity.  If anybody knows of a UCI supported way, that'd be cool, but not a high priority.

    ------------------------------
    Ryan Perry
    Business Systems Analyst
    Auric Solar
    ------------------------------

    The first step toward cloud success. - Migrate from CRM to D365 with expert guidance from Microsoft. I'm Ready


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