Skip to main content

In acumatica project screen(PM301000) i have add two custome fields. those are UsrOnBudget and UsrCompletedPrec. i need to develop when i check UsrOnBudget check box get PMRevenueBudget.CuryAmount and divide by PMCostBudget.CuryAmount And that value show on UsrCompletedPrec field. Those To Customized Field locate in CT.ContractExt. How can I achieve it?

This is my code. actualRevenueAmount and actualCostQty  assigned their value as actualRevenueAmount + actualCostQty . Not give correct output

ex: PMRevenueBudget.CuryAmount = 20

      PMCostBudget.CuryAmount = 10

 

      actualRevenueAmount = 30

      actualCostQty = 30

using PX.Data;
using PX.Data.BQL;
using PX.Data.BQL.Fluent;
using PX.Objects.CA;
using PX.Objects.CT;
using PX.Objects.PM;
using System.Linq;

namespace Lauges
{
public class ProjectEntryExt : PXGraphExtension<ProjectEntry>
{
#region Event Handlers

protected void _(Events.FieldUpdated<ContractExt.usrOnBudget> e)
{
var row = e.Row as Contract;


if (row != null && (bool)e.NewValue == true)
{
CalculateCompletedPerc(row);

}
}

private decimal CalculateCompletedPerc(Contract contract)
{


decimal actualRevenueAmount = PXSelect<PMRevenueBudget,
Where<PMRevenueBudget.projectID, Equal<Current<Contract.contractID>>>>
.Select(Base).FirstTableItems.Sum(rb => rb.CuryAmount ?? 0);


decimal actualCostQty = PXSelect<PMCostBudget,
Where<PMCostBudget.projectID, Equal<Current<Contract.contractID>>>>
.Select(Base).FirstTableItems.Sum(cb => cb.CuryAmount ?? 0);


decimal percentage = (actualCostQty / actualRevenueAmount) * 100;

if (actualRevenueAmount != 0)
{
contract.GetExtension<ContractExt>().UsrCompletedPercentage = percentage;

return percentage;
}
else
{

return 0;
}

Base.Caches typeof(Contract)].Update(contract);

}

#endregion
}
}

 


@tharinduweerasooriya90  I can see two issues in your code

  1. In the Field Updated Event Handler I see the first parameter is missing. It should be something like _(Events.FieldUpdated<Contract, ContractExt.usrOnBudget> e) 
  2. In CalculateCompletedPerc your las line of code is never going to be executed as you are returning anyways before that point.

Also I suggest

  1. move your decimal percentage = (actualCostQty / actualRevenueAmount) * 100; into the if so you won’t get error in case you have actualRevenueAmount zero.
  2. assign either zero or your calculated formula to a variable and return at the end of your method
  3. Use Base.Projects.Update(contract) as you already have it initiated in the Base.
  4. Instead of using LINQ you can use GroupBy <AggregateTo<Sum<YourField>>> and tally your project revenue and cost.

Reply