Announcements
Hi,
I am trying to add an onChange event on Dynamics 365 to change the background color of statecode field when an oportunity is Open, Won or Lost.
I am getting "TypeError: Cannot read properties of null (reading 'getParent')" when using this JS code:
-----
function onChangeStateCode() {
var statecodeField = Xrm.Page.getAttribute("statecode");
var statecodeValue = statecodeField.getValue();
var statecodeControl = Xrm.Page.getControl("statecode");
var backgroundColor = "";
if (statecodeValue === 0) { // 0 represents the Open state
backgroundColor = "#ffffcc"; // Yellow
} else if (statecodeValue === 1) { // 1 represents the Won state
backgroundColor = "#ccffcc"; // Light Green
} else if (statecodeValue === 2) { // 2 represents the Loss state
backgroundColor = "#ffcccc"; // Light Red
} else {
backgroundColor = ""; // White
}
statecodeControl.getParent().style.backgroundColor = backgroundColor;
}
// Register the onChange event handler for the statecode field
Xrm.Page.getAttribute("statecode").addOnChange(onChangeStateCode);
-----
Thank you,
Tomas Cossara
Hi,
Totally agree with Andrew Butenko, for your need, I invite you to create a PCF or use an existing one. Take a look at https://pcf.gallery/
For example, would this one be suitable? pcf.gallery/.../
Hello Tomas,
According to the SDK getParent function of the control returns the section it belongs to - learn.microsoft.com/.../getparent but not the DOM object behind the control.
Why it doesn't return the section is the other question but the whole approach you use - changing the background of the field using DOM injection is absolutely unsupported and not recommended by Microsoft.
What you can do is create your own PCF control that will do it in a supported way.
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,370 Most Valuable Professional
nmaenpaa 101,156