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

The contents of the error details file can be seen below.
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)