Skip to main content

Notifications

Announcements

No record found.

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

Java script in UI

(0) ShareShare
ReportReport
Posted on by 8
Hi all
 
We are on-prem 9.1 and important i am not a developer but can read and understand the code, and happy to follow any guidance on the below issue.
 
We have the below script to highlight a field background colour when it has a specific value.
 
This code has always worked like a charm in classic interface but on UI it stopped working, after putting alerts i see that it never finds element value, and need your help to fix this:
 
code finds alert 1 and alert 3 but skipping alert 2
 
function Setcolour1() {
var a = Xrm.Page.getAttribute(/test_field1/).getValue();
if(a == 906010000) 
{
alert(1);
var element = window.parent.document.getElementById(/test_field1/);
if (element) {
alert(2);
                element.style.backgroundColor = /#FFCCCB/;
            }
//window.parent.document.getElementById (/test_field1/).style.backgroundColor = /#FFCCCB/ ;

}
alert(3);
}
 
Thank you
PS
  • Dengliang Li Profile Picture
    Dengliang Li Microsoft Employee on at
    Java script in UI
    Hi,
     
    Has the issue been solved?
    Have there been any updates? Please feel free to ask me if you have any questions!
    If it has been resolved, can you mark the response as an answer?
     
  • Suggested answer
    Dengliang Li Profile Picture
    Dengliang Li Microsoft Employee on at
    Java script in UI
    Hi,
     
    This may be due to the fact that the script was executed before the page element had finished loading, so the element was not found.
    You can add a callback function that calls the entire function at regular intervals until the element is found and stops.
     
    function Setcolour1() {
        var a = Xrm.Page.getAttribute(/test_field1/).getValue();
        if (a == 906010000) {
            alert(1);
            var element = window.top.document.getElementById(/test_field1/);
            if (element) {
                alert(2);
                element.style.backgroundColor = /#FFCCCB/;
            }else{
               
                //Calls the entire function every 10 milliseconds until the element is found then stops.
                setTimeout(Setcolour1,10)
            }
            //window.parent.document.getElementById (/test_field1/).style.backgroundColor = /#FFCCCB/ ;
     
        }
        alert(3);
    }
     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more.
    If you have further questions, please feel free to contact me .
     
    Best Regards,
    Dengliang Li
     

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,569 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans