web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Problem re-writing simple JavaScript for CRM V 9.x

(0) ShareShare
ReportReport
Posted on by 1,589

I understand Xrm.Page has been depricated in Dynamics 365 V 9.x and I am trying to write my code the new way.

The original snippet I have that capitalizes the first character of every word in a text field is shown below, but since it is not V 9 compliant, as it contains Xrm.Page I am trying to re-write it.

function UpperCaseField(fieldName) {
   var value = Xrm.Page.getAttribute(fieldName).getValue();
   //VALUE NOT NULL, CHANGE TEXT TO UPPERCASE
   if (value != null) {
      Xrm.Page.getAttribute(fieldName).setValue(value.charAt(0).toUpperCase() + value.substr(1).toLowerCase());
      Xrm.Page.ui.clearFormNotification('1');
   }
   //VALUE EQUAL TO NULL,DISPLAY WARNING NOTIFICATION
   else
   {
   	  Xrm.Page.ui.setFormNotification('Cannot be BLANK','WARNING','1');
   }
}


The re-written code shown below where I replaced Xrm.Page with the new formContext code

function UpperCaseField(fieldName) {
    var formContext = executionContext.getFormContext();
    var value = formContext.getAttribute(fieldName).getValue();
    alert (value);
    //VALUE NOT NULL, CHANGE TEXT TO UPPERCASE
    if (value != null) {
        formContext.getAttribute(fieldName).setValue(value.charAt(0).toUpperCase() + value.substr(1).toLowerCase());
        formContext.ui.clearFormNotification('1');
    }
        //VALUE EQUAL TO NULL,DISPLAY WARNING NOTIFICATION
    else {
        formContext.ui.setFormNotification('Cannot be BLANK', 'WARNING', '1');
    }
}

However, that doesn't seem to work and I get the following error

6574.scripterror.png

The contents of the error details file can be seen below.

UpperCaseField@this.url.has.been.redacted.for.security/.../GeneralApplicationFunctions.js
@this.url.has.been.redacted.for.security/.../ClientApiWrapper.aspx line 159 > eval:1:1
RunHandlerInternal@this.url.has.been.redacted.for.security/.../ClientApiWrapper.aspx
RunHandlers@this.url.has.been.redacted.for.security/.../ClientApiWrapper.aspx
ExecuteHandler@this.url.has.been.redacted.for.security/.../ClientApiWrapper.aspx
$De_1@this.url.has.been.redacted.for.security/.../formcontrols.js
executeHandler@this.url.has.been.redacted.for.security/.../formcontrols.js
executeHandlerByDescriptor@this.url.has.been.redacted.for.security/.../formcontrols.js
getHandler/<@this.url.has.been.redacted.for.security/.../formcontrols.js
getHandler/<@this.url.has.been.redacted.for.security/.../global.ashx
fireOnChange@this.url.has.been.redacted.for.security/.../formcontrols.js
setValueInternal@this.url.has.been.redacted.for.security/.../formcontrols.js
setValue@this.url.has.been.redacted.for.security/.../formcontrols.js
$48_2@this.url.has.been.redacted.for.security/.../formcontrols.js
Function.createDelegate/<@this.url.has.been.redacted.for.security/.../MicrosoftAjax.js
b@this.url.has.been.redacted.for.security/.../MicrosoftAjax.js
trigger@this.url.has.been.redacted.for.security/.../jquery-2.1.1.min.js
trigger@this.url.has.been.redacted.for.security/.../jquery-2.1.1.min.js
e.event.trigger@this.url.has.been.redacted.for.security/.../jquery-2.1.1.min.js
trigger/<@this.url.has.been.redacted.for.security/.../jquery-2.1.1.min.js
each@this.url.has.been.redacted.for.security/.../jquery-2.1.1.min.js
each@this.url.has.been.redacted.for.security/.../jquery-2.1.1.min.js
trigger@this.url.has.been.redacted.for.security/.../jquery-2.1.1.min.js
n.fn[b]@this.url.has.been.redacted.for.security/.../jquery-2.1.1.min.js
focus@this.url.has.been.redacted.for.security/.../jquery_ui_1.8.21.min.js
revertFocusBackToValueContainer@this.url.has.been.redacted.for.security/.../formcontrols.js
onKeyDownHandler@this.url.has.been.redacted.for.security/.../formcontrols.js
Function.createDelegate/<@this.url.has.been.redacted.for.security/.../MicrosoftAjax.js
b@this.url.has.been.redacted.for.security/.../MicrosoftAjax.js

I also tried re-writing another of of my snippets that did the same thing.

The original code is

function setFirstLetterToCapital(attributeName) {
    var attribute = Xrm.Page.getAttribute(attributeName);

    if (attribute != null) {
        var attributeValue = attribute.getValue();
        if (attributeValue != null) {
            // Split into each word 
            var attributeSplit = attributeValue.split(' ');
            for (var word in attributeSplit) {
                attributeSplit[word] = capitalizeWord(attributeSplit[word]);
            }

            var capitalized = attributeSplit.join(' ');

            attribute.setValue(capitalized);
        }
    }
} 

The re-written code is shown below.

function setFirstLetterToCapital(attributeName) {
    var formContext = executionContext.getFormContext();
    var attribute = formContext.getAttribute(attributeName);

    if (attribute != null) {
        var attributeValue = attribute.getValue();
        if (attributeValue != null) {
            // Split into each word 
            var attributeSplit = attributeValue.split(' ');
            for (var word in attributeSplit) {
                attributeSplit[word] = capitalizeWord(attributeSplit[word]);
            }

            var capitalized = attributeSplit.join(' ');

            attribute.setValue(capitalized);
        }
    }
}


The same problem occurs so clearly I am missing something.

What on earth am I missing?

Any help, advise, suggestions, or recommendations would be greatly appreciated.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at

    Make sure you check the checkbox “Pass the execution context as first parameter” in formeditor for that control onchange and form onload events.

    Then slightly change the code like below:

    function UpperCaseField(executionContext, fieldName) {

       var formContext = executionContext.getFormContext();

    .

    .

  • ACECORP Profile Picture
    1,589 on at

    Thanks. That did it.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans