Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Never got javascript Namespaces. Doesn't seem to work

Posted on by 2,079

Hi all,

So I've constantly read that namespacing is important in javascript web resources but it never seems to work for me. Everytime I try to namespace something it always says the javascript function doesn't exist....

So for example if I want to trigger a function on javascript load, below is my sample web resource code:

let Sdk = Sdk || {}; //Create namespace

Sdk.testFunction = function name(executionContext) {

    let formContext = executionContext.getFormContext();
    // Get Record ID
    let recordId = formContext.data.entity.getId();
    // console log record ID
    console.log(recordId);
    
}

So the function I then trigger on load is Sdk.testFunction

But when I reload the Dynamics form it throws a error that this function doesn't exist.

Just confused. Do I need to wrap this in a IIFE?

  • MikeC282 Profile Picture
    MikeC282 2,079 on at
    RE: Never got javascript Namespaces. Doesn't seem to work

    Ahhh yes off course!

    Thank you! You are a gentleman and a scholar!

  • Guido Preite Profile Picture
    Guido Preite 54,065 Moderator on at
    RE: Never got javascript Namespaces. Doesn't seem to work

    because let is scoped

  • MikeC282 Profile Picture
    MikeC282 2,079 on at
    RE: Never got javascript Namespaces. Doesn't seem to work

    Thank you kind sir! That worked perfectly.

    Another question. Why can't we declare namespaces using let? I've been trying to move all my code to ES6 and above these days and just curious why let wouldn't.

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,065 Moderator on at
    RE: Never got javascript Namespaces. Doesn't seem to work

    afaik you can't use let to define a namespace, you need to use var

    also you have "name" after function that is not correct, this code should work:

    var Sdk = Sdk || {}; //Create namespace
    
    Sdk.testFunction = function(executionContext) {
        let formContext = executionContext.getFormContext();
        // Get Record ID
        let recordId = formContext.data.entity.getId();
        // console log record ID
        console.log(recordId);
        
    }

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,620 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,884 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,150

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans