Skip to main content
Question

Need to Apply Conditional Coloring and Label on Custom Date Field in Cases Screen

  • August 19, 2025
  • 1 reply
  • 72 views

 

Hi Team,

I have a requirement on the Cases screen where I added a custom Date field. I want to apply conditional formatting to this field:

  • If the custom date is greater than the Reported Date, the field should appear Red.
  •  Else, it should be Green.

The below JavaScript runs, but it always goes to the else condition and never enters the if.

<script type="text/javascript">
function highlightWarrantyDate() {
// Get values from fields by their IDs
var reportedDateField = document.getElementById("ctl00_phF_form_t0_edReportedOnDateTime_text");
var warrantyDateField = document.getElementById("ctl00_phF_form_t0_NAWPREM03edUsrNAWPMWarrantyExpirationDate_text");

if (reportedDateField && warrantyDateField) {
var reportedDate = new Date(reportedDateField.value);
var warrantyDate = new Date(warrantyDateField.value);

if (reportedDate > warrantyDate) {
// Expired => Red background
warrantyDateField.style.backgroundColor = "red";
warrantyDateField.style.color = "white";
warrantyDateField.style.fontWeight = "bold";
} else {
// Valid => Green background
warrantyDateField.style.backgroundColor = "green";
warrantyDateField.style.color = "white";
warrantyDateField.style.fontWeight = "bold";
}
}
}

// Run after page loads
window.setTimeout(highlightWarrantyDate, 1000);
</script>

 

private void Page_Load(object sender, EventArgs e)
{
Page page = (Page)sender;
PX.Web.UI.PXDateTimeEdit myBox = (PX.Web.UI.PXDateTimeEdit)ControlHelper.FindControl("NAWPREM03edUsrNAWPMWarrantyExpirationDate", page);
if (myBox != null)
{
myBox.DataBinding += (object boxsender, EventArgs boxargs) =>
{
PX.Web.UI.PXDateTimeEdit box = boxsender as PX.Web.UI.PXDateTimeEdit;
if (box != null && box.Value != null)
{
DateTime expirationDate = (DateTime)box.Value;
if (expirationDate < DateTime.Now)
{
box.ForeColor = System.Drawing.Color.Red; // Expired
//box.Text = "Warranty Expired";
box.BackColor = System.Drawing.Color.LightPink;
}
else
{
box.ForeColor = System.Drawing.Color.Green; // Valid
//box.Text = "Warranty Valid";
box.BackColor = System.Drawing.Color.LightGreen;
}
}
};
}
}


public override void Initialize()
{
Page page = HttpContext.Current?.Handler as PXPage;
if (page != null)
{
page.Load += Page_Load;
}
}

I attempted this using JavaScript and the Page Load event, but the logic did not work as expected.


Note:- Will this JavaScript work when the Reported Date is modified, or only on Page Load? If not, what’s the best approach to make it dynamic

Could someone please help me with this?

Thanks,

1 reply

arpine08
Jr Varsity I
Forum|alt.badge.img+1
  • Jr Varsity I
  • August 8, 2026

Hi ​@vkumar68,

I suggest the following approach:

1. Add a JavaScript control to the screen in the Customization Editor and the following JavaScript:

var highlightWarrantyDate = function () {
if (px_all) {
var reportedDate = px_all.ctl00_phF_form_t0_edReportedOnDateTime;
var warrantyDate = px_all.ctl00_phF_form_t0_edUsrWarrantyDate;

if (reportedDate && warrantyDate) {
var reportedDateValue = ValidateDate(reportedDate.getValue());
var warrantyDateValue = ValidateDate(warrantyDate.getValue());

if (reportedDateValue && warrantyDateValue) {
var isLate = reportedDateValue > warrantyDateValue;

warrantyDate.elemText.style.setProperty('background-color', isLate ? 'red' : 'green', 'important');
warrantyDate.elemText.style.setProperty('color', 'white', 'important');
warrantyDate.elemText.style.setProperty('font-weight', 'bold', 'important');
} else {
warrantyDate.elemText.style.removeProperty('background-color');
warrantyDate.elemText.style.removeProperty('color');
warrantyDate.elemText.style.removeProperty('font-weight');
}
}
}
};

function ValidateDate(val) {
if (!val) return null;
var dt = new Date(val);
return isNaN(dt.getTime()) ? null : dt;
}

if (px_callback) {
px_callback.addHandler(highlightWarrantyDate);
}

Note: The control ID (ctl00_phF_form_t0_edUsrWarrantyDate) used in the example differs from yours. Please replace it with the corresponding control ID from your screen. Also, the example uses the control ID: ctl00_phF_form_t0_edReportedOnDateTime instead of ctl00_phF_form_t0_edReportedOnDateTime_text.

If either date is null or invalid, the custom styling is removed. You can adjust the validation, coloring, and null-handling logic according to your requirements.

The px_callback.addHandler() call ensures that the formatting is reapplied after Acumatica callbacks/refreshes.

2. Configure the Reported On and Warranty Expiration Date client events:

Reported On → Client Events  → ValueChanged = highlightWarrantyDate

Warranty Expiration Date → Client Events → ValueChanged = highlightWarrantyDate

The ValueChanged events ensure that the color is updated immediately when either date is manually changed.

Result: If the case is reported after the Warranty Expiration Date, the field is highlighted in red. Otherwise, while the warranty is still valid, it is highlighted in green.