On the Sales Order Subform, I have included 2 new field. Cost B and Profit Percentage.
If for Item Bicycle I have added a Cost B to be 75 (LCY) then the profit percentage would be P% = Selling Price - Cost Price / Cost Price * 100
However, this only works if quantity = 1.
How do I factor in the scenario where Quantity > 1.
Here is the code I have written;
modify("Unit Price")
{
trigger OnAfterValidate()
var
Rec_Items: Record Item;
begin
Rec_Items.Get(Rec."No.");
rec."Profit Percentage" := ((rec.Amount - (Rec.Quantity * "Cost B")) / (rec.Quantity * "Cost B") ) * 100
end;
}
Answers
Error In Your formula
You try (LineAmount - LineCost)/LineCost
usually Profit mean ProfitAmount/LineAmount
and when Quantity and/or "Cost B" will be zero then You will get a DivisionByZero error
in this case take a look how this "Profit %" is calculated in Item table for example
And be sure that Your rec.Amount also contain quantity factor