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

Announcements

No record found.

News and Announcements icon
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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 184 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 125

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans