Skip to main content
Solved

Conditionally formatting a selector in Modern UI Based on Custom Field

  • July 29, 2026
  • 9 replies
  • 71 views

Forum|alt.badge.img

I’ve used some of the samples for conditional formatting based on values, and I think I’m missing some small bit.

I’ve added the code below.

On the first field, referencing the custom field, it will always return “false”. That field is in the extended “CROpportunityHeader” which is the class for the view “Opportunity”. Slightly interesting point, if I intentionally misspell the custom field name it does the same thing, leading to believe that I need to do something to indicate it’s in a DAC Extension.

On the second colored field, when I reference “CuryAmount” rather than the custom field it seems to works as expected.

template>

 

  <style>

    [data-has-value="true"] input {

    background-color: #d4edda !important;

    color: #155724 !important;

    }

    [data-has-value="false"] input {

    background-color: #2bb14a !important;

    color: #155724 !important;

    }

 

    [data-rating="green"] input {

        background-color: #155724 !important;

    }

    [data-rating="red"] input {

        background-color: #721c24 !important;

    }

  </style>

  

    <field append="#phF_form_PXLayoutRule0" name="UsrTCAPParentOpportunityID" data-has-value.bind="Opportunity.UsrTCAPParentOpportunityID.value == null ? 'true' : 'false'"></field>

 

    <template modify="#fsHeader_C">

        <field name="UsrTEKMarginDollars"  data-rating.bind="Opportunity.CuryAmount.value >= 1000 ? 'green' : 'red'"></field>

 

    </template>    

 

</template>

 

Code from the .ts:

export interface CROpportunity_AcuPro extends CROpportunityHeader { }

 export class CROpportunity_AcuPro{

     UsrTCAPParentOpportunityID: PXFieldState<PXFieldOptions.CommitChanges>;

     UsrTEKMarginDollars: PXFieldState<PXFieldOptions.Disabled>;

}

Best answer by travisk91

I considered that, but it’s 100% for sure a null in the DB. I think it being having selector is breaking it, but I can’t figure out why.

So this is my solution:

Added this to the DAC Extension:

        #region UsrTCAPIsParent

        [PXBool()]

        [PXUIField(DisplayName = "Is Parent")]

        public bool? UsrTCAPIsParent

        {

            get => this.UsrTCAPParentOpportunityID != null;

        }

        public abstract class usrTCAPIsParent : PX.Data.BQL.BqlString.Field<usrTCAPIsParent> { }

        #endregion

And then changed the html to this:

    <field name="UsrTCAPParentOpportunityID" data-has-value.bind="Opportunity.UsrTCAPIsParent.value == true ? 'true' : 'false'"></field>

9 replies

darylbowman
Captain II
Forum|alt.badge.img+17

Just an observation that if I’m reading it right, the binding to ‘data-has-value’ is ‘true’ when OpportunityID is null. That’s backwards in my opinion. That’s not the issue, right?


darylbowman
Captain II
Forum|alt.badge.img+17

...if I intentionally misspell the custom field name it does the same thing...

How are you compiling this? If it’s a local build, are you certain it is being built?


Forum|alt.badge.img
  • Author
  • Jr Varsity II
  • July 29, 2026

You are correct that it is backwards (and it’s fixed in my live code), but that’s not the problem. It is never reading the value for stuff in the DAC extension.


darylbowman
Captain II
Forum|alt.badge.img+17

Given that I think you’re using the pattern I posted here, looking back at the project I used it in, I’m doing exactly what you are in TS:

export interface CRQuoteHeader_DXSalesQuoteGrossProfit extends CRQuoteHeader { }
export class CRQuoteHeader_DXSalesQuoteGrossProfit {

UsrDXCommissionRating: PXFieldState<PXFieldOptions.Readonly>;
}

 


Forum|alt.badge.img
  • Author
  • Jr Varsity II
  • July 29, 2026

Local in the screens folder, sure it’s getting rebuilt because I’m making changes that I can see. 

For example I’ve changed this one: <field name="UsrTEKMarginDollars"  data-rating.bind="Opportunity.CuryAmount.value >= 1000 ? 'green' : 'red'"></field> 

Several times, changing the colors, and values so I know the changes are getting there. I’ve also tried every combination of cases and restarting the server and app pool.


darylbowman
Captain II
Forum|alt.badge.img+17

Try this instead:

<field append="#phF_form_PXLayoutRule0" name="UsrTCAPParentOpportunityID" data-has-value.bind="Opportunity.UsrTCAPParentOpportunityID?.value == null ? 'true' : 'false'"></field>

If it changes to being always ‘true’, then the field itself is the problem.


darylbowman
Captain II
Forum|alt.badge.img+17

 Wait, if OpportunityID is a string, after the first time it’s set, it won’t ever be null again. It will be an empty string (""). 

Do this instead:

<field append="#phF_form_PXLayoutRule0" name="UsrTCAPParentOpportunityID" data-has-value.bind="Opportunity.UsrTCAPParentOpportunityID?.value ? 'true' : 'false'"></field>

 


Forum|alt.badge.img
  • Author
  • Jr Varsity II
  • Answer
  • July 29, 2026

I considered that, but it’s 100% for sure a null in the DB. I think it being having selector is breaking it, but I can’t figure out why.

So this is my solution:

Added this to the DAC Extension:

        #region UsrTCAPIsParent

        [PXBool()]

        [PXUIField(DisplayName = "Is Parent")]

        public bool? UsrTCAPIsParent

        {

            get => this.UsrTCAPParentOpportunityID != null;

        }

        public abstract class usrTCAPIsParent : PX.Data.BQL.BqlString.Field<usrTCAPIsParent> { }

        #endregion

And then changed the html to this:

    <field name="UsrTCAPParentOpportunityID" data-has-value.bind="Opportunity.UsrTCAPIsParent.value == true ? 'true' : 'false'"></field>


Forum|alt.badge.img

@travisk91 Hi,

Have you tried with if.bind

example  if.bind="Document.UsrTest.value == 1"

<field name="UsrTCAPParentOpportunityID" if.bind="Opportunity.UsrTCAPIsParent.value == true ? 'true' : 'false'"></field>

in my case, I am using if.bind for conditionally fields/tabs displaying

I am not sure, it may work in your case.