web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Never got javascript Namespaces. Doesn't seem to work

(0) ShareShare
ReportReport
Posted on by 2,175

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?

I have the same question (0)
  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    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);
        
    }

  • MikeC282 Profile Picture
    2,175 on at

    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.

  • Guido Preite Profile Picture
    54,086 Moderator on at

    because let is scoped

  • MikeC282 Profile Picture
    2,175 on at

    Ahhh yes off course!

    Thank you! You are a gentleman and a scholar!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans