Message Image  

Customer Engagement & Dynamics CRM Forum

 View Only
  • 1.  Why are these onLoad functions conflicting?

    GOLD CONTRIBUTOR
    Posted Dec 23, 2021 10:33 AM

    I have two scripts each containing a function that triggers on the form onLoad event. For some reason whichever one loads second I get an error that that onLoad method does not exist. The really weird thing is that I have another script on another form with an almost identical function to the second one which loads just fine with the first function. What could be causing these two functions to conflict? Any thoughts are appreciated!

    First function:

    var E360 = E360 || {};
    E360.Contact = E360.Contact || {};
    let formContext = null;
    
    (function () {
    'use strict';
    E360.Contact.onLoad = function(executionContext) {
        formContext = executionContext.getFormContext();
    
        //Display warning
        let dnc = formContext.getAttribute('preferredcontactmethodcode').getValue();
        let dnp = formContext.getAttribute('new_e360recruitmentengagement').getValue();
        let isMinor = formContext.getAttribute('new_isminor').getValue();
    
        if (dnp) {
            dnp = dnp[0].id;
            dnp = dnp.substring(1, dnp.length - 1);
        }
    
        if(dnp === 'A851D412-CD61-EC11-8F8F-000D3A336682') {
            formContext.ui.setFormNotification("----", 'WARNING', 'dnpWarning');
        }
        else if (dnc === 100000001) {
            formContext.ui.setFormNotification("----", 'WARNING', 'dncWarning');
        }
        else if(isMinor) {
            formContext.ui.setFormNotification("----", 'WARNING', 'minorWarning');
        }
    
        // Control Audit History tab
        const tabObj = formContext.ui.tabs.get("Audit History");
        const userRoles = Xrm.Utility.getGlobalContext().userSettings.roles
        const sysAdmin = userRoles.get("4f9acaa5-20c9-e811-a968-000d3a3463ea"); // System Administration role GUID
    
        if (sysAdmin) {
        tabObj.setVisible(true);
        }
        else {
        tabObj.setVisible(false);
        }
    
        //Hide BPF
        formContext.ui.process.setVisible(false);
    }....
    })()
    

    Second function:

    var EBI = EBI || {};
    EBI.Contact = EBI.Contact || {};
    let formContext = null;
    
    (function() {
    'use strict';
    EBI.Contact.onLoad = function(executionContext) {
        formContext = executionContext.getFormContext();
    
        //Control RFI Follow-Up Tab
        const tabRfiFu = formContext.ui.tabs.get("Follow-Up Call");
        const rfiField = formContext.getAttribute('new_ebirfifurecord').getValue();
    
        if (rfiField) {
            tabRfiFu.setVisible(true);
        }
        else {
            tabRfiFu.setVisible(false);
        }
    
        // Initialize button
        const buttons = [
            formContext.getControl("WebResource_sendsmsresponsebutton"),
            formContext.getControl("WebResource_ebicallcontactbutton")
        ]
        buttons.forEach(function(button) {
            if (button) {
                button.getContentWindow().then(
                    function (contentWindow) {
                        contentWindow.InitializeButton(executionContext);
                    },
                    function(){
                        formContext.ui.formSelector.items.get(formContext.ui.formSelector.getCurrentItem().getId()).navigate();
                    }
                );
            };
        })
    }.....
    })()


    ------------------------------
    Lucas Hewitt
    Enterprise Application Specialist
    Ethnos360
    ------------------------------


  • 2.  RE: Why are these onLoad functions conflicting?

    GOLD CONTRIBUTOR
    Posted Dec 23, 2021 12:14 PM
    To add to the frustration, I've reduce my first function works perfectly fine on its own but the second one throws an error no matter what I change!

    It can be totally empty and the only onLoad event and I will still get an error that the method doesn't exist:

    var EBI = EBI || {};

    EBI.Contact = EBI.Contact || {};

    let formContext = null;

    (function() {

    'use strict';

    EBI.Contact.onLoad = function(executionContext) {

    }

    })();


    ------------------------------
    Lucas Hewitt
    Enterprise Application Specialist
    Ethnos360
    ------------------------------



  • 3.  RE: Why are these onLoad functions conflicting?

    Posted Dec 24, 2021 10:35 AM
    Hi Lucas, I noticed there is a semicolon missing at the end of the second line here:

        // Control Audit History tab
        const tabObj = formContext.ui.tabs.get("Audit History");
        const userRoles = Xrm.Utility.getGlobalContext().userSettings.roles
        const sysAdmin = userRoles.get("4f9acaa5-20c9-e811-a968-000d3a3463ea"); // System Administration role GUID
    


    ------------------------------
    Dave Storey
    Ocean Pearl Consulting
    Nepean ON
    ------------------------------



  • 4.  RE: Why are these onLoad functions conflicting?

    GOLD CONTRIBUTOR
    Posted Dec 24, 2021 02:21 PM
    Thanks for taking a look at my code! Unfortunately, that doesn't seem to have fixed anything. =(

    I've copied the contents of the second function into my first function as a temporary fix and it seems to be working normally, but I'd still like the second function to work as intended.

    I've also tested the other functions within the library that the second function is in and those all work fine when called by an HTML button on the same form. It really seems like something within the EBI.Contact.onLoad function is preventing it from being parsed correctly and thus throwing the error but I'm completely stumped about what it could be.

    ------------------------------
    Lucas Hewitt
    Enterprise Application Specialist
    Ethnos360
    ------------------------------



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