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 :
Microsoft Dynamics CRM (Archived)

How to call Namespaced javascript function

(0) ShareShare
ReportReport
Posted on by

Hi,

I have created javascript file with the namespace pattern, code is as below...

var MyNamespace = MyNamespace || {};

MyNamespace = function (Xrm, $, undefined) // Start namespace definition

{
"use strict";

// Start of private functions
var _MyTestFunction = function ()
{
alert("Works fine....");
}

// Start of public variables and functions return object
return {

//// Start of public functions
MyTestFunction: _MyTestFunction,
//// End of public functions
}
}(window.Xrm, jQuery);

From the normal web application We call the function with the following syntax.

MyNamespace.MyTestFunction

But its not working from the CRM form. When I called it onload form event. It throws following error.

Script Error
One of the scripts for this record has caused an error. For more details, download the log file.
TypeError: Object doesn't support property or method 'MyTestFunction' at eval code (eval code:1:1)

How can we call this function from the CRM form properties? Please help me if I am missing anything.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    For example:

    if (typeof (YourNamespace) == 'undefined') {

       window.YourNamespace = {};

    }

    if (typeof (YourNamespace.Functions) == 'undefined') {

       window.YourNamespace.Functions = {};

    }

    YourNamespace.Functions.Actions= {

    }

    It works for me every time. 

  • gordonj Profile Picture
    255 on at

    Did you ever resolve this?

    I have the same issue.  Seems to only effect Internet Explorer 11 (or at least only that browser throws the error message).

    I feel certain I have seen namespaces used before in CRM.  However the Form events seem to throw an error.

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Could you try with the follwoing line to declare the namspace, we had similar issue, the old way didnt work and we had to do the following

    if (typeof (MyNamespace) === 'undefined') { MyNamespace  = function () { }; }

  • Suggested answer
    Charles Abi Khirs Profile Picture
    3,569 on at

    Hello,

    The JS file (account entity) can be looks like this:

    Var X = X || {};

    Var X.Form = X.Form || {};

    Var X.Form.Account = function (){

       function onLoad(){

       //Do whatever you need

       }

       function onSave(){

       //Do whatever you need

       }

       return {

          OnLoad: onLoad,

          OnSave: onSave

          };

    }();

    Once you add the JS file to your entity form, you can call now the function in your js as following: X.Form.Account.OnLoad

    Hope this helps,

    If found helpful, please mark the answer as verified to help other community members.

    Charles Abi Khirs.

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans