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