Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

TypeError: Cannot read properties of null (reading 'getParent')

(0) ShareShare
ReportReport
Posted on by 5

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.

pastedimage1683040212535v1.png

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

  • Suggested answer
    XM-22040801-0 Profile Picture
    XM-22040801-0 11 on at
    RE: TypeError: Cannot read properties of null (reading 'getParent')

    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/.../

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: TypeError: Cannot read properties of null (reading 'getParent')

    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.

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

News and Announcements

Announcing Category Subscriptions!

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 Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans