Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Need help understanding this Javascript code

Posted on by 235

I have the following javascript code snippet, while I understand what's it doing, I fail to understand the whole object creation "Sample". Is this some best practice to write JS code?

var Sample = window.Sample || {};
(function () {
    console.log('file loaded');
    // Show/hide tabs based on Relationship Type
    this.showHideTabs = function (executionContext) {
        console.log('method called');
        var formContext = executionContext.getFormContext();
        var relType = '';
        if (formContext.getAttribute("customertypecode") != null) {
            relType = formContext.getAttribute("customertypecode").getText();
        }

        // Show all first and then hide based on selection
        formContext.ui.tabs.get('tab_ShipToAccounts').setVisible(false);
        formContext.ui.tabs.get('tab_CustomerAssets').setVisible(false);
        formContext.ui.tabs.get('tab_WorkOrders').setVisible(false);
        formContext.ui.tabs.get('tab_Plants').setVisible(false);
        formContext.ui.tabs.get('tab_ShipToAssets').setVisible(false);
        formContext.ui.tabs.get('tab_ShipToWOs').setVisible(false);
        console.log(relType);
        if (relType == 'Ship-To') {
            formContext.ui.tabs.get('tab_CustomerAssets').setVisible(true);
            formContext.ui.tabs.get('tab_WorkOrders').setVisible(true);
            formContext.ui.tabs.get('tab_Plants').setVisible(true);
        }
        else if (relType == 'Sold-To') {
            formContext.ui.tabs.get('tab_ShipToAccounts').setVisible(true);
            formContext.ui.tabs.get('tab_ShipToAssets').setVisible(true);
            formContext.ui.tabs.get('tab_ShipToWOs').setVisible(true);
        }
    }
    
}).call(Sample);


*This post is locked for comments

  • PragmaticDevs Profile Picture
    PragmaticDevs 235 on at
    RE: Need help understanding this Javascript code

    Thanks Gautam! This was really helpful.

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Need help understanding this Javascript code

    Hi,

    You may know that we are attaching JavaScript in the form in various event like on change of field  , onload of a form or on save of a form.

    Sometimes in our project we may need to create lot of  Javascript files/web reaource  for different entity. As a result sometimes we may forgot to know which function is for which form ,so to simplify this you can use namespace with some meaningful name where you can include your all JavaScript function for a form or particular entity.

    Here in your code it's did similar  , I can see there is a namespace defined called "Sample" under that one function included called "showHideTabs". Let say this function you can attach in the form on load so you have to use "Sample.showHideTabs"( with name space name) and pass execution context as first parameter in the event handler.

    Inside the function "showHideTabs" we need to get the particular formcontext from the executiinContext parameter to get all control and context information of the form. Have a look below reference for more information-

    docs.microsoft.com/.../client-scripting-best-practices

    docs.microsoft.com/.../walkthrough-write-your-first-client-script

    docs.microsoft.com/.../understand-clientapi-object-model

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Need help understanding this Javascript code

    it's to create a namespace, useful to differentiate from other functions in case they have the same name

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans