Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Why are these onLoad functions conflicting?

Posted on by 279

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 H Profile Picture
    Lucas H 279 on at
    RE: Why are these onLoad functions conflicting?

    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 H Profile Picture
    Lucas H 279 on at
    RE: Why are these onLoad functions conflicting?

    Do you think the function name could be messing this up?

    Unfortunately, these two need to be separate functions. The first is a universal script applied to all forms and the second is a script specific to this form.

  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    RE: Why are these onLoad functions conflicting?

    HI Lucas,

    I would suggest to use different function name or try to combine the business logic into one function.

    Thanks,
    Pradeep

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans