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,068

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,068 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,057 Super User on at
    RE: Never got javascript Namespaces. Doesn't seem to work

    because let is scoped

  • MikeC282 Profile Picture
    MikeC282 2,068 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,057 Super User 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);
        
    }

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans