How to assign a calculated value to a field in a table

Hello Guys, I am a beginer in Dynamice 365 FO. I encountered a problem during practice. My English is really not good so my expression may not be clear enough, please forgive me. :pray:

I have two tables: GoodsPrice table and SalesRecord table.

The GoodsPrice table looks like this.

1711612165370(1)

It has three fields: GoodsID, GoodsName and Price.

The SalesRecord table looks like this.

1711613151280(1)

It has five fields: SaleID, GoodsID(Having a lookup relation with the GoodsPrice table), GoodsName, Sales and TortalPrice.

Now I want to get the value of Price field based value of GoodsID field. And then calculate a value by multiplying value of Sales field and value of Price field. Finally, assign this value to the TortalPrice field.

1711613427640

But I have no idea how to achieve this effect. I once wanted to use the display method to implement this effect, but it seems that display method is not a field that really exists in the table. I don’t know how to assign this calculation result to the fields in the table.

I would really be appreciated if anyone can help me with this.:pray:

Start simple and add more logic step by step.

First of all, you want to do something when the value of GoodsId field changes. You can do that in modifiedField() method in your table. For example:

public void modifiedField(fieldId _fieldId)
{
    super(_fieldId);

    switch (_fieldId)
    {
        case fieldNum(YourTable, GoodsId):
            info(strFmt("GoodsId set to %1", this.GoodsId));
            break;

When you run it, you should get an infolog message every time you change the value of GoodsId.

Then you want to set a value of another field. You can start with a hard-coded value to test that you’re able to to do it; you’ll add the value retrieval logic later. For example:

case fieldNum(YourTable, GoodsId):
    this.TotalPrice = 42;
    break;

If it all works, the next step will be getting the price from GoodsId. I(deally, you’ve already implemented find() method on your GoodsPrice table. If so, you’ll simple pass the ID there and get the price field. Like this:

GoodsPrice::find(2).Price

Using it in modifiedField() is simple:

int price = GoodsPrice::find(this.GoodsId).Price;

Multiplying two values is simple too:

price * this.Sales

And then you just need to assign the calculated value to TotalPrice, instead of the hard-coded number we used before:

this.TotalPrice = price * this.Sales;
1 Like

Thank you very much! I will try to do as you say.