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)

OnChange Javascript constantly returning ReferenceError: undefined

(0) ShareShare
ReportReport
Posted on by 205

This is driving me crazy. I have a script on the Contract entity that is supposed to fire onChange for one date field (activeon) and then set the value of another date field (expireson) depending on the selected value of a picklist (new_contractterm). No matter what I change, I keep getting:

ReferenceError: 'OnChange_EffectiveDate' is undefined
   at eval code (eval code:1:1)
   at RunHandlerInternal (mycrmenvironment.crm.dynamics.com/.../ClientApiWrapper.aspx)
   at RunHandlers (mycrmenvironment.crm.dynamics.com/.../ClientApiWrapper.aspx)
   at ExecuteHandler (mycrmenvironment.crm.dynamics.com/.../ClientApiWrapper.aspx)
   at Mscrm.TurboForm.Control.CustomScriptsManager.prototype.$Ce_1 (mycrmenvironment.crm.dynamics.com/.../formcontrols.js)
   at Mscrm.TurboForm.Control.CustomScriptsManager.prototype.executeHandler (mycrmenvironment.crm.dynamics.com/.../formcontrols.js)
   at Mscrm.TurboForm.Control.CustomScriptsManager.prototype.executeHandlerByDescriptor (mycrmenvironment.crm.dynamics.com/.../formcontrols.js)
   at Anonymous function (mycrmenvironment.crm.dynamics.com/.../formcontrols.js)
   at Anonymous function (mycrmenvironment.crm.dynamics.com/.../global.ashx)
   at Mscrm.TurboForm.Control.Data.DataAttributeBase.prototype.fireOnChange (mycrmenvironment.crm.dynamics.com/.../formcontrols.js)


Here's the script in question:

function OnChange_EffectiveDate(){
var effDate = new Date(Xrm.Page.getAttribute('activeon').getValue());
var term = Xrm.Page.getAttribute('new_contractterm').getValue();
		if (effDate != null) && (term != null) {
			if (term == 100000000) {
				var termDate = effDate.setFullYear(effDate.getFullYear() + 100);
				Xrm.Page.getAttribute('expireson').setValue(termDate);
				}
			else if (term == 100000001) {
				var termDate = effDate.setFullYear(effDate.getFullYear() + 1);
				Xrm.Page.getAttribute('expireson').setValue(termDate);
				}
			else if (term == 100000002) {
				var termDate = effDate.setFullYear(effDate.getFullYear() + 2);
				Xrm.Page.getAttribute('expireson').setValue(termDate);
				}
			else if (term == 100000003) {
				var termDate = effDate.setFullYear(effDate.getFullYear() + 4);
				Xrm.Page.getAttribute('expireson').setValue(termDate);
				}
			else if (term == 100000004) {
				var termDate = effDate.setFullYear(effDate.getFullYear() + 5);
				Xrm.Page.getAttribute('expireson').setValue(termDate);
				}
		}
}


If anyone can help me out before I put a fist through my monitor, I'd greatly appreciate it.

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    You code looks correct. Do you have other code in the same JavaScript file?

  • ChadF Profile Picture
    205 on at

    Hi Andrew,

    Yes, there are two other functions in the same library. Here are the full contents:

    function OnChange_EffectiveDate(){
        var effDate = new Date(Xrm.Page.getAttribute('activeon').getValue());
    	var term = Xrm.Page.getAttribute('new_contractterm').getValue();
    		if (effDate != null) && (term != null) {
    			if (term == 100000000) {
    				var termDate = effDate.setFullYear(effDate.getFullYear() + 100);
    				Xrm.Page.getAttribute('expireson').setValue(termDate);
    				}
    			else if (term == 100000001) {
    				var termDate = effDate.setFullYear(effDate.getFullYear() + 1);
    				Xrm.Page.getAttribute('expireson').setValue(termDate);
    				}
    			else if (term == 100000002) {
    				var termDate = effDate.setFullYear(effDate.getFullYear() + 2);
    				Xrm.Page.getAttribute('expireson').setValue(termDate);
    				}
    			else if (term == 100000003) {
    				var termDate = effDate.setFullYear(effDate.getFullYear() + 4);
    				Xrm.Page.getAttribute('expireson').setValue(termDate);
    				}
    			else if (term == 100000004) {
    				var termDate = effDate.setFullYear(effDate.getFullYear() + 5);
    				Xrm.Page.getAttribute('expireson').setValue(termDate);
    				}
    		}
    }
    
    
    function OnChange_ContractName(){
        var ContractName = Xrm.Page.getAttribute('new_contractnameid').getValue();
        if (ContractName != null) {
            Xrm.Page.getAttribute('title').setValue(ContractName[0].name);
        }
    }
    
    function OnSave_datacheck(_Context){
        var docloc = Xrm.Page.getAttribute('new_documentlocation').getValue();
        if (docloc == null  || docloc.indexOf ('.pdf') == -1){
            alert ('Please enter the contract file location in the Document Location field.');
            _Context.getEventArgs().preventDefault();
        }
    }


  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello,

    I found there is some syntax error , there should be if statement with braces close and end .  Try with this -

    function OnChange_EffectiveDate(){
        var effDate = new Date(Xrm.Page.getAttribute('activeon').getValue());
        var term = Xrm.Page.getAttribute('new_contractterm').getValue();
        if (effDate != null && term != null) {
            if (term == 100000000) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 100);
                Xrm.Page.getAttribute('expireson').setValue(termDate);
            }
            else if (term == 100000001) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 1);
                Xrm.Page.getAttribute('expireson').setValue(termDate);
            }
            else if (term == 100000002) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 2);
                Xrm.Page.getAttribute('expireson').setValue(termDate);
            }
            else if (term == 100000003) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 4);
                Xrm.Page.getAttribute('expireson').setValue(termDate);
            }
            else if (term == 100000004) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 5);
                Xrm.Page.getAttribute('expireson').setValue(termDate);
            }
        }
    }


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

    So here are fixes you can apply:

    if (effDate != null) && (term != null) {

    change to

    if (effDate != null && term != null) {

    Also you can use https://codebeautify.org/jsvalidate to check if code is valid.

  • ChadF Profile Picture
    205 on at

    Thanks; I made your suggested change and published, but I'm still getting the same result.

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at

    the code is okay now - try publish again and open in a new browser.

    otherwise - try to debug and see where it is failing.

  • ChadF Profile Picture
    205 on at

    I stepped away to get a coffee and when I came back it decided now it was time to work, even without publishing again. ¯\_(ツ)_/¯

    Thanks for your help, guys.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello ,

    Field name should be with publisher name like new_expireson  (publisher name new_  )

    function OnChange_EffectiveDate(){
        var effDate = new Date(Xrm.Page.getAttribute('activeon').getValue());
        var term = Xrm.Page.getAttribute('new_contractterm').getValue();
        if (effDate != null && term != null) {
            if (term == 100000000) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 100);
                Xrm.Page.getAttribute('expireson').setValue(termDate);
            }
            else if (term == 100000001) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 1);
                Xrm.Page.getAttribute('expireson').setValue(termDate);
            }
            else if (term == 100000002) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 2);
                Xrm.Page.getAttribute('expireson').setValue(termDate);
            }
            else if (term == 100000003) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 4);
                Xrm.Page.getAttribute('expireson').setValue(termDate);
            }
            else if (term == 100000004) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 5);
                Xrm.Page.getAttribute('expireson').setValue(termDate);
            }
        }
    }
    
    
    
    
    function OnChange_ContractName() {
        var ContractName = Xrm.Page.getAttribute('new_contractnameid').getValue();
        if (ContractName != null) {
            Xrm.Page.getAttribute('title').setValue(ContractName[0].name);
        }
    }
    
    function OnSave_datacheck(_Context) {
        var docloc = Xrm.Page.getAttribute('new_documentlocation').getValue();
        if (docloc == null || docloc.indexOf('.pdf') == -1) {
            alert('Please enter the contract file location in the Document Location field.');
            _Context.getEventArgs().preventDefault();
        }
    }


  • ChadF Profile Picture
    205 on at

    Nah, those are default system fields; they don't have the publisher prefix. It's sorted out now, though, thanks.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Here is corrected code -

    function OnChange_EffectiveDate(){
        var effDate = new Date(Xrm.Page.getAttribute('new_activeon').getValue());
        var term = Xrm.Page.getAttribute('new_contractterm').getValue();
        if (effDate != null && term != null) {
            if (term == 100000000) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 100);
                Xrm.Page.getAttribute('new_expireson').setValue(termDate);
            }
            else if (term == 100000001) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 1);
                Xrm.Page.getAttribute('new_expireson').setValue(termDate);
            }
            else if (term == 100000002) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 2);
                Xrm.Page.getAttribute('new_expireson').setValue(termDate);
            }
            else if (term == 100000003) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 4);
                Xrm.Page.getAttribute('new_expireson').setValue(termDate);
            }
            else if (term == 100000004) {
                var termDate = effDate.setFullYear(effDate.getFullYear() + 5);
                Xrm.Page.getAttribute('new_expireson').setValue(termDate);
            }
        }
    }
    
    
    
    
    function OnChange_ContractName() {
        var ContractName = Xrm.Page.getAttribute('new_contractnameid').getValue();
        if (ContractName != null) {
            Xrm.Page.getAttribute('new_title').setValue(ContractName[0].name);
        }
    }
    
    function OnSave_datacheck(_Context) {
        var docloc = Xrm.Page.getAttribute('new_documentlocation').getValue();
        if (docloc == null || docloc.indexOf('.pdf') == -1) {
            alert('Please enter the contract file location in the Document Location field.');
            _Context.getEventArgs().preventDefault();
        }
    }


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