Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Never got javascript Namespaces. Doesn't seem to work

Posted on by 2,095

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,095 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,081 Super User 2024 Season 1 on at
    RE: Never got javascript Namespaces. Doesn't seem to work

    because let is scoped

  • MikeC282 Profile Picture
    MikeC282 2,095 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,081 Super User 2024 Season 1 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans