Solved

Can I stop FiledUpdated event from being called?

  • 13 September 2021
  • 5 replies
  • 27 views

Hopefully I explain this correctly.  On the Project Revenue Budget tab I’ve added two new fields: Unit Cost and Mark Up.  When the values in these fields change we want to update the Unit Rate.  I’m using the FieldUpdated event to do this and everything works great.  The problem is that we also want to allow the user to enter the Unit Rate manually and when that happens it needs to update the Mark Up.  This also works fine but the issue is that when the Mark Up is updated from the change in Unit Rate the FieldUpdating event on the Mark Up is called and then updates the Unit Rate again.  Currently my Mark Up rounds to two decimal places.  Because of the rounding on the Mark Up the Unit Rate is then recalculated incorrectly.  

Questions:

1 - Can I change my precision on the Mark Up to more than two decimal places to give me more precision and prevent rounding? If so, the how?

2 - Can I prevent the code from running in the FieldUpdated event for Mark Up when the Unit Rate is changed?

3- Should I be using a different event for this?

icon

Best answer by ddunn 13 September 2021, 22:12

When you use:

e.Cache.SetValueExt<PMBudgetExt.usrMarkUp>(row, markUp.Value * 100);

You’re invoking the business logic.  If you want to update a field without invoking the logic then update the field like:

c.MyFieldToUpdate = NewFieldValue;

Then you can update the cache:

theGraph.Transactions.Update(row);  or theGraph.Document.Update(row);

I think that will get you what you want.

View original

5 replies

Userlevel 2
Badge

Can you post your code from your VS project where you are setting those field values?

        protected void _(Events.FieldUpdated<PMRevenueBudget, PMBudgetExt.usrUnitCost> e)
{
PMRevenueBudget row = e.Row;
PMBudgetExt rowExt = row.GetExtension<PMBudgetExt>();
if (rowExt.UsrMarkUp.HasValue && rowExt.UsrUnitCost.HasValue)
{
decimal? markup = rowExt.UsrMarkUp / 100;
if (rowExt.UsrUnitCost < 0)
throw new PXSetPropertyException(ProjectUnitCostAndMarkUp.Messages.NegativeUnitCost);
decimal? unitPrice = rowExt.UsrUnitCost / (1 - markup.Value);
e.Cache.SetValue<PMRevenueBudget.curyUnitRate>(row, unitPrice.Value);
decimal? budgetedAmount = unitPrice.Value * row.Qty;
e.Cache.SetValue<PMRevenueBudget.curyAmount>(row, budgetedAmount.Value);
e.Cache.SetValue<PMRevenueBudget.curyRevisedAmount>(row, budgetedAmount.Value);
}
}

protected void _(Events.FieldUpdated<PMRevenueBudget, PMBudgetExt.usrMarkUp> e)
{
PMRevenueBudget row = e.Row;
PMBudgetExt rowExt = row.GetExtension<PMBudgetExt>();
if (rowExt.UsrMarkUp.HasValue && rowExt.UsrUnitCost.HasValue)
{
decimal? markup = rowExt.UsrMarkUp / 100;
if (markup >= 1)
throw new PXSetPropertyException(ProjectUnitCostAndMarkUp.Messages.MarkupToLarge);
if(markup < 0)
throw new PXSetPropertyException(ProjectUnitCostAndMarkUp.Messages.NegativeMarkUp);
decimal? unitPrice = rowExt.UsrUnitCost / (1 - markup.Value);
e.Cache.SetValue<PMRevenueBudget.curyUnitRate>(row, unitPrice.Value);
decimal? budgetedAmount = unitPrice.Value * row.Qty;
e.Cache.SetValue<PMRevenueBudget.curyAmount>(row, budgetedAmount.Value);
e.Cache.SetValue<PMRevenueBudget.curyRevisedAmount>(row, budgetedAmount.Value);
}
}


protected void _(Events.FieldUpdated<PMRevenueBudget, PMBudget.curyUnitRate> e)
{

PMRevenueBudget row = e.Row;
PMBudgetExt rowExt = row.GetExtension<PMBudgetExt>();
decimal? markUp = 0;
if(row.CuryUnitRate.HasValue && rowExt.UsrUnitCost.HasValue)
{
if(row.CuryUnitRate.Value != 0)
{
markUp = 1 - (rowExt.UsrUnitCost.Value / row.CuryUnitRate.Value);
}
}
e.Cache.SetValueExt<PMBudgetExt.usrMarkUp>(row, markUp.Value * 100);
}

 

Userlevel 2
Badge

When you use:

e.Cache.SetValueExt<PMBudgetExt.usrMarkUp>(row, markUp.Value * 100);

You’re invoking the business logic.  If you want to update a field without invoking the logic then update the field like:

c.MyFieldToUpdate = NewFieldValue;

Then you can update the cache:

theGraph.Transactions.Update(row);  or theGraph.Document.Update(row);

I think that will get you what you want.

@ddunn thanks for your help.

I just removed the line you said and replaced it with this line:

rowExt.UsrMarkUp = markUp * 100;

And it seems to be working perfect now.

I’m new to acumatica and don’t understand what you mean by this: theGraph.Transactions.Update(row);  or theGraph.Document.Update(row);

Is theGraph PMRevenueBudget, PMBudgetExt or something else?  

Userlevel 2
Badge

I’m reasonably new, too. 

In your case the graph will be Base because you’re within a a graph extension.

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2020  Acumatica, Inc. All rights reserved