Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by 1,579

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

  • ACECORP Profile Picture
    ACECORP 1,579 on at
    RE: Problem re-writing simple JavaScript for CRM V 9.x

    Thanks. That did it.

  • Verified answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: Problem re-writing simple JavaScript for CRM V 9.x

    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();

    .

    .

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans