Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

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

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,321 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

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,558 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,647 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans