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?
Solved
Сalculate the value of a field when form opened
Best answer by aleksejslusar19
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}";
}
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.