web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Colored Grid Script not working in CRM 2016 update anymore.

(0) ShareShare
ReportReport
Posted on by

Hello,

I had a custom unsupported script in our CRM2013 which colorized some Sub grid entries.

Now in CRM 2016 it won't work anymore and i can't figure out how to get it to work again.

i gues it's cause i used document.getElementsByTag. Some suggestions would be nice

Here's the Code which is working in CRM 2013

Thanks

Kai

function alterGridRecordsFaVc() {
    var gridId  = "accountopportunitiesgrid";
    var gridTH = parent.document.getElementById(gridId);
    if (gridTH == null) {
        setTimeout(alterGridRecordsFaVc, 2000);
        return;
    }
    var headers = gridTH.getElementsByTagName("TH");
    var tdata = gridTH.getElementsByTagName("TD");
    for (var n = 0; n < headers.length; n++)  {
        if (headers[n].innerText == "Status") {
            for (var i = 0; i < tdata.length; i++)  {
                if (tdata[i].innerText == "Offen"){ 
                    //tdata[i].style.backgroundColor = "blue";
                    tdata[i].style.color = "blue";
                }
                if (tdata[i].innerText == "Verloren"){ 
                    //tdata[i].style.backgroundColor = "red";
                    tdata[i].style.color = "red";
                }
                if (tdata[i].innerText == "Gewonnen"){ 
                    //tdata[i].style.backgroundColor = "red";
                    tdata[i].style.color = "green";
                }
            }
        }
    }
    for (var n = 0; n < headers.length; n++)  {
        if (headers[n].innerText == "Sonderkonditionen vereinbart?") {
            for (var i = 0; i < tdata.length; i++)  {
                if (tdata[i].innerText == "Nein"){ 
                    //tdata[i].style.backgroundColor = "blue";
                    tdata[i].style.color = "black";
                }
                if (tdata[i].innerText == "Ja"){ 
                    //tdata[i].style.backgroundColor = "grey";
                    tdata[i].style.color = "red";
                }                
            }
        }
    }
}


*This post is locked for comments

I have the same question (0)
  • Joshua Wright Profile Picture
    410 on at

    I don't know how much longer this will work as the 2011 endpoint is being removed and turbo forms has had issues with this, but I was able to get this working with the below setup.

    //Assignment Status|Completed=rgba(0,255,0,.2);Rejected=rgba(255,0,0,.2);Not Started=rgba(0,0,255,.2)
    
    function FormatGrid(formatCondition) {
        var conditionArr = formatCondition.split("|");
        var colorArr = conditionArr[1].split(";");
    
        var hdText = conditionArr[0].toString();
    
        var tblHeaders = window.top.document.getElementById('contentIFrame0').contentWindow.document.getElementById('gridBodyTable').getElementsByTagName('TH');
    
        var hdIndex = 0; var i = 0;
        var chkKeyWord = "";
    
        //Loop through the headers to find the index of hdText
        for (i = 0; i < tblHeaders.length; i++) {
            if (hdText == tblHeaders[i].innerText) {
                hdIndex = i;
                break;
            }
        } //end of for
    
        var rows = window.top.document.getElementById('contentIFrame0').contentWindow.document.getElementById('gridBodyTable').rows;
        var ctr = 0;
    
        for (i = 0; i < rows.length; i++) {
            for (var j = 0; j < colorArr.length; j++) {
                if ((rows[i].cells[hdIndex].innerText).substring(rows[i].cells[hdIndex].innerText.length - 1, rows[i].cells[hdIndex].innerText.length) == "\n")
                    chkKeyWord = (rows[i].cells[hdIndex].innerText).substring(0, rows[i].cells[hdIndex].innerText.length - 1);
                else
                    chkKeyWord = rows[i].cells[hdIndex].innerText;
    
                if (chkKeyWord == (colorArr[j].split("="))[0].toString()) {
                    rows[i].style.backgroundColor = (colorArr[j].split("="))[1].toString();
                    break;
                }
    
                chkKeyWord = "";
            }
        }
    
    }
    
    

  • Suggested answer
    Community Member Profile Picture
    on at

    I test your code with CRM 2016 online, I think MS made some changes on the form XML and HTML. In order to make the code works again, I made some changes:

    1. At first, this method is not supported by Microsoft and may not work in the future. I only test with IE11 and chrome, not sure how the code works in other browsers.

    3. "var gridTH = parent.document.getElementById(gridId)" changes to var gridTH = document.getElementById(gridId), the parent is removed.

    4. getElementbyTagName is still works, but tdata[i].style.color = "red" is no longer work, however, the style.backgroundColor is working well. So you have to find the exact field element in HTML to add color to CSS, in my subgrid is something like: "tdata[i].getElementsByTagName("a")[0].style.color"

    5. Go to system settings to change use the legacy form rendering engine to Yes in the General Tab, based on Microsoft, this change may affect the performance.

    Hope this is helpful for you and below is my sample code:

    function alterGridRecordsFaVc() {
    var gridId = "opportunityproductsGrid";
    var gridTH = document.getElementById(gridId);
    if (gridTH == null) {
    setTimeout(alterGridRecordsFaVc, 2000);
    return;
    }
    var headers = gridTH.getElementsByTagName("TH");
    var tdata = gridTH.getElementsByTagName("TD");
    for (var n = 0; n < headers.length; n++) {
    if (headers[n].innerText == "Unit") {
    for (var i = 0; i < tdata.length; i++) {
    if (tdata[i].innerText == "Primary Unit"){
    tdata[i].style.backgroundColor = "yellow";
    // tdata[i].style.color = "blue";
    tdata[i].getElementsByTagName("a")[0].style.color = "red"
    }
    }
    }
    }
    }

  • Community Member Profile Picture
    on at

    Hi Joshua

    i Can't get this one to work.

    var rows = window.top.document.getElementById('contentIFrame0').contentWindow.document.getElementById('accountopportunitiesgrid').rows;

    Comes back as undefined.

    i gues it's cause the grid is not fully loaded when the script fires, but i can't get it to work that the script fires AFTER the complete form is loaded.

    thx

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans