Skip to main content
Solved

Сalculate the value of a field when form opened

  • October 28, 2025
  • 2 replies
  • 50 views

Forum|alt.badge.img+2

Hello everyone. I need to calculate the value of a field that is not stored in the database. I need to do this every time the BAccount form is opened. What is the best way to do this so that it only happens once when it is opened?

Best answer by aleksejslusar19

@bihalivan15, try this approach - add RowSelecting Event Handler in your graph extension, for instance:

 

public class BAccountMaintExt : PXGraphExtension<BAccountMaint>
{
private bool _isCalculated = false;

protected virtual void _(Events.RowSelecting<BAccount> e)
{
if (e.Row == null) return;

// Check if we're in the primary view and haven't calculated yet
if (!_isCalculated)
{
CalculateCustomField(e.Row);
_isCalculated = true;
}
}

private void CalculateCustomField(BAccount account)
{
// Your calculation logic here
string calculatedValue = PerformComplexCalculation(account);

// Set the unbound field value
var ext = account.GetExtension<BAccountExt>();
if (ext != null)
{
ext.CustomField = calculatedValue;
}
}

private string PerformComplexCalculation(BAccount account)
{
// Replace with your actual calculation logic
return $"Calculated value for {account.AcctName}";
}
}


 

2 replies

Forum|alt.badge.img+4

I’m not sure there’s a way to calculate it only once when the form is opened.
Perhaps the FieldSelecting event would be suitable for you — or you could consider using the PXDBScalar or PXUnboundDefault attribute.


Forum|alt.badge.img+1
  • Jr Varsity III
  • Answer
  • October 28, 2025

@bihalivan15, try this approach - add RowSelecting Event Handler in your graph extension, for instance:

 

public class BAccountMaintExt : PXGraphExtension<BAccountMaint>
{
private bool _isCalculated = false;

protected virtual void _(Events.RowSelecting<BAccount> e)
{
if (e.Row == null) return;

// Check if we're in the primary view and haven't calculated yet
if (!_isCalculated)
{
CalculateCustomField(e.Row);
_isCalculated = true;
}
}

private void CalculateCustomField(BAccount account)
{
// Your calculation logic here
string calculatedValue = PerformComplexCalculation(account);

// Set the unbound field value
var ext = account.GetExtension<BAccountExt>();
if (ext != null)
{
ext.CustomField = calculatedValue;
}
}

private string PerformComplexCalculation(BAccount account)
{
// Replace with your actual calculation logic
return $"Calculated value for {account.AcctName}";
}
}