Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

Update deprecated

Jump to Best Answer
  • 1.  Update deprecated

    TOP CONTRIBUTOR
    Posted Jun 30, 2020 07:47 AM
    I am using a piece of JavaScript to change forms, which I found on the Microsoft CRM Forums a long time ago. Now the Solution Checker is showing some issues with the code, so I need some help on how to update it.

    The code used is similar to this:
    var FormSwitcher = FormSwitcher || {};
    FormSwitcher._defaultFormName = "Information";
    
    FormSwitcher._redirectToForm = function (formName, tryDefault) {
        var currentForm = Xrm.Page.ui.formSelector.getCurrentItem();
        var availableForms = Xrm.Page.ui.formSelector.items.get();
    
        // if we dont check to see if we're already on the form we'll get into an infinite loop 
        if (currentForm.getLabel().toLowerCase() != formName.toLowerCase()) {
            for (var i in availableForms) {
                var form = availableForms[i];
                // try to find a form based on the name 
                if (form.getLabel().toLowerCase() == formName.toLowerCase()) {
                    form.navigate(); // redirect once we find it 
                    return true;
                }
            }
        }
        else {
            return false;
        }
    
        // nothing found, redirect to default 
        if (tryDefault && currentForm.getLabel().toLowerCase() != FormSwitcher._defaultFormName) {
            return FormSwitcher._redirectToForm(FormSwitcher._defaultFormName, false);
        }
    
        return false;
    }
    
    function FormOnLoad()
    {
        var typeofcontact_STAFF = 1;
        var typeofcontact_TEACHER = 2;
        var typeofcontact_STUDENT = 3;
        var typeofcontact = Xrm.Page.getAttribute('typeofcontact').getValue();
    
        switch (typeofcontact)
        {
            case typeofcontact_STAFF:
                FormSwitcher._redirectToForm("Staff Form", true);
                break;
            case typeofcontact_TEACHER:
                FormSwitcher._redirectToForm("Teacher Form", true);
                break;
            case typeofcontact_STUDENT:
                FormSwitcher._redirectToForm("Student Form", true);
                break;
            default:
                FormSwitcher._redirectToForm(FormSwitcher._defaultFormName, false);
                break;
        }
    }
    These are the issues reported by the solution checker:

    Issue 1 - web-use-client-context:
    'Xrm.Page' references or accesses a deprecated API in the client context object model. Replace this call with the following client context API: 'ExecutionContext.getFormContext'

    Line: 5, 6 and 36

    Issue 2 - web-use-strict-equality-operators:
    For most comparisons the strict equality comparison algorithm is the appropriate choice as it will return the expected results. The strict operators handle null and undefined values as expected.

    Line: 9, 13 and 24

    Issue 3 - web-use-strict-mode:
    Strict mode is a way to introduce better error-checking into your code. Code should run in strict mode wherever possible.

    Line: 4


    I tried reading the guides on how to fix this but I didn't get very far 😀

    #Technical

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


  • 2.  RE: Update deprecated

    Posted Jul 01, 2020 08:38 AM
    Hi Niels,
    For your first listed error you'll need to switch the code to formContext.ui.formSelector.getCurrentItem();. Reference can be found here. https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/formcontext-ui-formselector.

    Scott

    ------------------------------
    Scott Florance
    Business Applications Consultant
    KTL Solutions, Inc, Frederick MD
    ------------------------------

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


  • 3.  RE: Update deprecated

    TOP CONTRIBUTOR
    Posted Jul 01, 2020 08:43 AM
    For issue 2 it's stating that in places where you do a comparison with == or !=, it's recommending that you change those to === and !==. Here's some information about strict equality operators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality#:~:text=The%20strict%20inequality%20operator%20(%20!%3D%3D,different%20types%20to%20be%20different.


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

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


  • 4.  RE: Update deprecated

    TOP CONTRIBUTOR
    Posted Jul 01, 2020 08:46 AM
    For issue 3 it's recommending that you enter 'use strict'; at the top of your js web resource file. More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#:~:text=JavaScript's%20strict%20mode%2C%20introduced%20in,different%20semantics%20from%20normal%20code.

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

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


  • 5.  RE: Update deprecated

    TOP CONTRIBUTOR
    Posted Jul 02, 2020 05:01 AM
    @Scott Florance

    Do I need to define formContext somwhere before I can replace Xrm.Page.ui.formSelector.getCurrentItem(); with formContext.ui.formSelector.getCurrentItem();?

    I get this error: ReferenceError: formContext is not defined

    @Andy Arndt I was able to fix Issue 2 - web-use-strict-equality-operators. However, I still get an issue related to Issue 3 - web-use-strict-mode:.

    I tried adding 'use strict'; add line 1. But I'm still getting the issue. I also tried with "use strict"; but it doesn't change anything. Should it be placed somewhere else?



    ------------------------------
    BR,
    Niels
    ------------------------------

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


  • 6.  RE: Update deprecated
    Best Answer

    Posted Jul 02, 2020 06:34 AM
    Hey Niels,

    for the formContext, yes you will also need to
    1. ensure on the form properties where the js "FromOnLoad" is called, ensure the pass execution context is checked
    see screenshot here https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/clientapi-execution-context
    2. update function FormOnLoad() to
    function FormOnLoad(executionContext)
    3. Insert the following to define the formContext variable in the FormOnLoad function
    var formContext = executionContext.getFormContext(); // get formContext
    4. in the switch statement
    add the formContext var to each FormSwitcher.xxx call
    5. add the formContext arg to line 4
    FormSwitcher._redirectToForm = function (formContext, formName, tryDefault)

    Finally just like what @Scott Florance stated, now you can replace all Xrm.Page.ui.formSelector with formContext.ui.formSelector
    and Xrm.Page with formContext

    I must say I didn't test it, but that should it.
    Xun

    ------------------------------
    Xun Dong
    Thomas Miller
    ------------------------------

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


  • 7.  RE: Update deprecated
    Best Answer

    Posted Jul 02, 2020 06:43 AM
    And regarding the use strict mode for js.

    it looks like https://community.dynamics.com/365/f/dynamics-365-general-forum/389442/powerapps-solution-checker-use-strict-issue

    Currently you have to 'use strict' within every new function  But not at top level.

    function imStrict(){
      "use strict";
      // ... your code ...
    }


    ------------------------------
    Xun Dong
    Thomas Miller
    ------------------------------

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


  • 8.  RE: Update deprecated

    TOP CONTRIBUTOR
    Posted Jul 02, 2020 07:18 AM
    @Xun Dong Thanks the input. That fixed the issue with the strict mode.

    Can you elaborate on number 4 - or perhaps give an example? I'm not sure where exactly to add the to the switch statement.

    I think I got everything else.

    ------------------------------
    BR,
    Niels
    ------------------------------

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


  • 9.  RE: Update deprecated

    Posted Jul 02, 2020 08:44 AM
    @Niels Lønberg,
    @Xun Dong​​ can correct me if I am wrong but I believe this is what he was referring to in 4, passing executionContext each place you call FormSwitcher._redirectToForm as well as on the function itself so that it can carry into the formContext.

    var FormSwitcher = FormSwitcher || {};
    FormSwitcher._defaultFormName = "Information";
    
    FormSwitcher._redirectToForm = function (executionContext, formName, tryDefault) {
        var formContext = executionContext.getFormContext();
        var currentForm = formContext.ui.formSelector.getCurrentItem();
        var availableForms = formContext.ui.formSelector.items.get();
    
        // if we dont check to see if we're already on the form we'll get into an infinite loop 
        if (currentForm.getLabel().toLowerCase() != formName.toLowerCase()) {
            for (var i in availableForms) {
                var form = availableForms[i];
                // try to find a form based on the name 
                if (form.getLabel().toLowerCase() == formName.toLowerCase()) {
                    form.navigate(); // redirect once we find it 
                    return true;
                }
            }
        }
        else {
            return false;
        }
    
        // nothing found, redirect to default 
        if (tryDefault && currentForm.getLabel().toLowerCase() != FormSwitcher._defaultFormName) {
            return FormSwitcher._redirectToForm(executionContext, FormSwitcher._defaultFormName, false);
        }
    
        return false;
    }
    
    function FormOnLoad()
    {
        var typeofcontact_STAFF = 1;
        var typeofcontact_TEACHER = 2;
        var typeofcontact_STUDENT = 3;
        var typeofcontact = Xrm.Page.getAttribute('typeofcontact').getValue();
    
        switch (typeofcontact)
        {
            case typeofcontact_STAFF:
                FormSwitcher._redirectToForm(executionContext, "Staff Form", true);
                break;
            case typeofcontact_TEACHER:
                FormSwitcher._redirectToForm(executionContext, "Teacher Form", true);
                break;
            case typeofcontact_STUDENT:
                FormSwitcher._redirectToForm(executionContext, "Student Form", true);
                break;
            default:
                FormSwitcher._redirectToForm(executionContext, FormSwitcher._defaultFormName, false);
                break;
        }
    }​​



    ------------------------------
    Scott Florance
    Business Applications Consultant
    KTL Solutions, Inc, Frederick MD
    ------------------------------

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


  • 10.  RE: Update deprecated

    Posted Jul 02, 2020 09:06 AM

    Hya,
    @Scott Florance, your suggestion is not far off.

    @Niels Lønberg,
    What I mean by no. 4 is almost as what Scott suggested, but pass in the already defined var formContext instead of the executionContext.
    so the switch statement originally between line 38 and 52 would look like the following instead..

        switch (typeofcontact)
        {
            case typeofcontact_STAFF:
                FormSwitcher._redirectToForm(formContext,"Staff Form", true);
                break;
            case typeofcontact_TEACHER:
                FormSwitcher._redirectToForm(formContext,"Teacher Form", true);
                break;
            case typeofcontact_STUDENT:
                FormSwitcher._redirectToForm(formContext,"Student Form", true);
                break;
            default:
                FormSwitcher._redirectToForm(formContext,FormSwitcher._defaultFormName, false);
                break;
        }




    ​​​​​​

    ------------------------------
    Xun Dong
    Thomas Miller
    ------------------------------

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


  • 11.  RE: Update deprecated

    TOP CONTRIBUTOR
    Posted Jul 03, 2020 02:37 AM
    Thanks for the help @Scott Florance, @Andy Arndt and @Xun Dong. With your help I was able to fix all the issue:
    ​​​

    I'll definitively update my CV with the title 'Copy/Paste Developer' 🙂

    ------------------------------
    BR,
    Niels
    ------------------------------

    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