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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
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
I have the same question (0)
  • Suggested answer
    Dengliang Li Profile Picture
    Microsoft Employee on at
    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
     
  • Dengliang Li Profile Picture
    Microsoft Employee on at
    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?
     

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Hamza H Profile Picture

Hamza H 140 Super User 2026 Season 1

#2
Nagaraju_Matta Profile Picture

Nagaraju_Matta 128

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 70 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans