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)

Javascript

(0) ShareShare
ReportReport
Posted on by 274

Hello there first thank you for looking into this. I am trying to define my javascript using namespace and $self.

I am also trying to define my fields using  

var $self = FIELD = {
		AccountName: "new_accountname",
		RelationType: "new_relationtype"
	};

When I try to run this code on the form I am getting an error, the error is (

One of the scripts for this record has caused an error. For more details, download the log file.
TypeError: Unable to get property 'onLoad' of undefined or null reference at eval code (eval code:1:1))

//JavaScript source code

if (typeof CONTOSO == "undefined") {
	CONTOSO = {_namespace: true};
}

CONTOSO.Account = (function() {
	var $self = {};
	
	var $self = FIELD = {
		AccountName: "new_accountname",
		RelationType: "new_relationtype"
	};
	$self.onLoad = function () {
		showHide(FIELD.AccountName, FIELD.RelationType);
	}
	
	$self.FIELD.AccountName = function () {
		showHide(FIELD.AccountName);
	}
	
	function showHide() {
		if (){
			
		}
		else if (){
			
		}
	}
})();
 

*This post is locked for comments

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

    how does your form call for onLoad look like?

  • MituCRMing Profile Picture
    274 on at

    CONTOSO.Account.onLoad

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    You should start from basics of JS and not try to do something complex from the start. I reworked your code to make it run without issues (at least it should not throw any errors):

    if (typeof CONTOSO == "undefined") {
        CONTOSO = {_namespace: true};
    }
    
    CONTOSO.Account = (function() {
        var FIELD = {
            AccountName: "new_accountname",
            RelationType: "new_relationtype"
        };
    
        function onLoad() {
            alert(FIELD.AccountName);
        }
    
        return {
            onLoad: onLoad
        };
    })();


  • MituCRMing Profile Picture
    274 on at

    Hi Andrii, I was hoping to use $self and I am returning self.  I am also planning to call the function onChange. Please kindly see my code below. Thanks

    //JavaScript source code

    if (typeof CONTOSO == "undefined") {

    CONTOSO = {_namespace: true};

    }

    CONTOSO.Account = (function() {

    var $self = {};

    var $self = FIELD = {

    AccountName: "new_accountname",

    RelationType: "new_relationtype"

    };

    //OnLoad

    $self.onLoad = function () {

    showHide(FIELD.AccountName, FIELD.RelationType);

    }

    $self.FIELD.AccountName = function () {

    showHide(FIELD.AccountName);

    }

         $self.FIELD.AccountName = function () {

    showHide(FIELD.AccountName);

    }

    //onChange

         $self.FIELD.AccountNameonChange = function () {

    showHide(FIELD.AccountName, FIELD.RelationType, "");

    }

    function showHide() {

    if (){

    }

    else if (){

    }

    }

        return $self;

    })();

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Before I will go through your code one more time can you please answer following questions:

    1. What's your experience working with JavaScript?

    2. Where did you get that idea using $self?

    3. Even if you use $self what's the goal of returning it?

  • MituCRMing Profile Picture
    274 on at

    I am a beginner on JavaScript, I saw a blog post about $self. The reason I am using it is to have a standard when I use it in the event handler.

    So for my onLoad I will use: Contoso. Account. onLoad

    for onChange: Contoso.Account.new_accountname.onChange

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    So if you are a beginner I believe it's for good to listen to guys who are bit experienced than you (I work with CRM and JavaScript since 2008). So if you want to listen then I would suggest not to use things you do not understand yet. I will suggest to use easier for understanding code. If you want use namespaces it's good. Here is your code with usage of namespaces:

    if (typeof CONTOSO === "undefined") {
        CONTOSO = {__namespace: true};
    }
    
    if (typeof CONTOSO.Account === "undefined") {
        CONTOSO.Account = { __namespace: true };
    }
    
    CONTOSO.Account.onLoad = function() {
        //put code of your handler here
    }
    
    if (typeof CONTOSO.Account.new_accountname === "undefined") {
        CONTOSO.Account.new_accountname = { __namespace: true };
    }
    
    CONTOSO.Account.new_accountname.onChange = function() {
        //put code of your handler here
    }


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