Skip to main content
Post a question

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id : n8oUhBw8j2j7U30arZos77
Customer experience | Sales, Customer Insights,...
Suggested answer

Color labels on form

Like (4) ShareShare
ReportReport
Posted on 31 Jan 2025 16:02:58 by 46
Hello,
 
Does anyone know how to make a label on a form a different color?
  • Suggested answer
    JeroenS Profile Picture
    JeroenS 50 on 04 Feb 2025 at 19:05:22
    Color labels on form
    Perhaps not exactly what were asking but could be of interest depending on the reason why you would like to use colors.
    If you don't want to use JavaScript or PCF Controls, but you do want to use somehow colors in views, you can use the Power Apps Grid Control.
    This control has a property Enable OptionSet Colors.
    When you are setting colors on your optionset it does look pretty nice.
     
    If have written a blog post about this a while back if you are interested in setting this up:
  • CU31101219-0 Profile Picture
    CU31101219-0 46 on 03 Feb 2025 at 15:17:56
    Color labels on form
    I am trying this, but keep getting an error, anyone know why?
     
    function highlightFieldLabels(executionContext) {
        var formContext = executionContext.getFormContext();
        // Target a specific field by logical name (e.g., "firstname")
        var fieldControl = formContext.getControl("syn_marketcodelookup"); // Replace with your field's logical name
        
        // Get the label element by the field's ID
        var labelElement = document.querySelector('label[for="' + fieldControl.getName() + '"]');
        
        if (labelElement) {
            // Apply CSS styles to the field label
            labelElement.style.backgroundColor = "yellow";
            labelElement.style.color = "red";
            labelElement.style.fontWeight = "bold";
        }
    }
     
     
     
    TypeError: Cannot read properties of undefined (reading 'getFormContext')
        at highlightFieldLabels (https://hmedev.crm.dynamics.com/%7b000010308425969%7d/webresources/new_Account_ColorChange:2:40)
        at b._executeFunctionInternal (https://content.powerapps.com/resource/uci-infra-bus/scripts/app.18c8b156931e16e9d083bd7ee820dca0.js:14:1212544)
        at b.execute (https://content.powerapps.com/resource/uci-infra-bus/scripts/app.18c8b156931e16e9d083bd7ee820dca0.js:14:1210978)
        at https://content.powerapps.com/resource/uci-infra-bus/scripts/app.18c8b156931e16e9d083bd7ee820dca0.js:4:148863
        at i (https://content.powerapps.com/resource/uci-infra-bus/scripts/app.18c8b156931e16e9d083bd7ee820dca0.js:14:99373)
        at re._executeIndividualEvent (https://content.powerapps.com/resource/uci-infra-bus/scripts/app.18c8b156931e16e9d083bd7ee820dca0.js:4:148837)
        at re._executeEventHandler (https://content.powerapps.com/resource/uci-infra-bus/scripts/app.18c8b156931e16e9d083bd7ee820dca0.js:4:145655)
        at Object.execute (https://content.powerapps.com/resource/uci-infra-bus/scripts/app.18c8b156931e16e9d083bd7ee820dca0.js:4:142708)
        at w._executeSyncAction (https://content.powerapps.com/resource/uci-infra-bus/scripts/app.18c8b156931e16e9d083bd7ee820dca0.js:14:560779)
        at w._executeSync (https://content.powerapps.com/resource/uci-infra-bus/scripts/app.18c8b156931e16e9d083bd7ee820dca0.js:14:560506)
    Error Details:
    Event Name: onload
    Function Name: highlightFieldLabels
    Web Resource Name: new_Account_ColorChange
    Solution Name: Active
    Publisher Name: DefaultPublisherorg1939cae2
     
  • CU31101219-0 Profile Picture
    CU31101219-0 46 on 03 Feb 2025 at 13:33:47
    Color labels on form
    This custom control doesn't look like it adds color, just allows you to show static text with ability to make it bold and/or italic.
     
    But you can use components to achieve this, such as this one:
    Custom Label | PCF Gallery
  • Suggested answer
    Saif Ali Sabri Profile Picture
    Saif Ali Sabri 726 Super User 2025 Season 1 on 01 Feb 2025 at 10:50:37
    Color labels on form

    AI was used to create this answer
    Yes, you can change the color of a label on a form in Microsoft Dynamics 365 Business Central (BC) using AL code. However, note that the ability to customize colors directly on labels is limited in the base functionality of BC. You can achieve this by using the Style and StyleExpr properties for controls on pages or reports.

    Here’s how you can do it:


    Steps to Change Label Color on a Form:

    1. Open the AL Project:

      • Open your AL project in Visual Studio Code or the development environment you are using.

    2. Locate the Page or Report:

      • Find the page or report where the label is located.

    3. Use the Style and StyleExpr Properties:

      • Use the Style property to define the appearance of the control (e.g., label, field).

      • Use the StyleExpr property to dynamically set the style based on a condition.


    Example: Changing Label Color on a Page

    Here’s an example of how to change the color of a label on a page:

    al
    Copy
    page 50100 "My Custom Page"
    {
        layout
        {
            area(Content)
            {
                field("My Field"; "My Field")
                {
                    ApplicationArea = All;
                    Style = Strong; // Applies a strong (bold) style
                    StyleExpr = ShouldHighlight; // Dynamically applies style based on a condition
                }
                label(MyLabel)
                {
                    ApplicationArea = All;
                    Caption = 'This is a colored label';
                    Style = Favorable; // Applies a green color
                }
            }
        }
    
        var
            ShouldHighlight: Boolean;
    
        trigger OnAfterGetRecord()
        begin
            // Set the condition for dynamic styling
            ShouldHighlight := "My Field" > 100;
        end;
    }

    Available Style Options:

    The Style property supports the following options:

    • Standard: Default style.

    • Strong: Bold text.

    • Attention: Red color (used for warnings or errors).

    • Favorable: Green color (used for positive indicators).

    • Unfavorable: Red color (used for negative indicators).

    • Ambiguous: Yellow color (used for warnings or uncertain states).

    • Subordinate: Gray color (used for less important information).


    Limitations:

    • The Style property is primarily designed for fields, not labels. For labels, you can only use the Style property if the label is part of a field or group.

    • If you need more advanced customization (e.g., custom colors), you may need to use client-side extensions or JavaScript for additional styling.


    Alternative: Use a Field Instead of a Label:

    If you need more flexibility, consider using a field with a Caption instead of a label. Fields support the Style and StyleExpr properties, allowing you to change colors dynamically.


    Example: Using a Field as a Colored Label

    al
    Copy
    field("Dummy Field"; '')
    {
        ApplicationArea = All;
        Caption = 'This is a colored label';
        Editable = false;
        Style = Favorable; // Applies a green color
    }
  • Suggested answer
    Jimmy Passeti | MVP Profile Picture
    Jimmy Passeti | MVP 276 Super User 2025 Season 1 on 31 Jan 2025 at 17:55:33
    Color labels on form
    Hello,
     
    Unfortunately, it's not possible to do this natively.
    But you can use components to achieve this, such as this one:
    Custom Label | PCF Gallery
     
    Regards,
    Jimmy Passeti | Microsoft MVP
    âś… Please mark as Verified if this answered your question!

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading complete