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?
Page 1 / 1
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
}
}
- In the Field Updated Event Handler I see the first parameter is missing. It should be something like _(Events.FieldUpdated<Contract, ContractExt.usrOnBudget> e)
- In CalculateCompletedPerc your las line of code is never going to be executed as you are returning anyways before that point.
Also I suggest
- move your decimal percentage = (actualCostQty / actualRevenueAmount) * 100; into the if so you won’t get error in case you have actualRevenueAmount zero.
- assign either zero or your calculated formula to a variable and return at the end of your method
- Use Base.Projects.Update(contract) as you already have it initiated in the Base.
- Instead of using LINQ you can use GroupBy <AggregateTo<Sum<YourField>>> and tally your project revenue and cost.
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.