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

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
    2,142 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
    54,077 Moderator on at
    RE: Never got javascript Namespaces. Doesn't seem to work

    because let is scoped

  • MikeC282 Profile Picture
    2,142 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
    54,077 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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,467 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,066 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans