Add new calculated field in a table taking the value from same table.

ManiNavManiNav Member Posts: 82
Hi Everyone,

I have created one new field 'VAT Amount' in purchase line table. and i want this to update as calculated field as:
VAT Amount= Amount including VAT - Amount; eg. VAT Amount=15-10=5;[field: 'Amount including VAT' and 'Amount' already is present]
SO, i tried the below code in VAT Amount field ONValidate trigger in Purchase line table, but its not updating the field.

VAT Amount - OnValidate()

PurchLine2.RESET;
PurchLine2.FINDSET;
REPEAT
PurchLine2.INIT;
VatAmount := PurchLine2."Amount Including VAT" -PurchLine2.Amount;
PurchLine2.VALIDATE("VAT Amount",VatAmount);
PurchLine2.MODIFY;
UNTIL PurchLine2.NEXT= 0;

Will you please guide me,what i am doing wrong.

Thanks,
Mani.

Best Answer

  • ManiNavManiNav Posts: 82
    Accepted Answer
    Hi edoderoo,

    Thank you for your guidelines.
    Actually I was adding that field in table to drag in page. so, i wrote the code in page OnAfter get record() trigger, its working fine now.

    "Vat Amount" := "Amount Including VAT" - Amount;
    Rec.Modify;

    Thanks,
    Mani.

Answers

  • ManiNavManiNav Member Posts: 82
    Hi Everyone,

    Please guide me for above query. it will be very useful for me.
    [As field: 'Amount including VAT' and 'Amount' is normal decimal, and my new field 'VAT Amount' is also normal decimal]

    Thanks,
    Mani.
  • edoderooedoderoo Member Posts: 76
    At first: PurchLine2 does not have any filters, so it will go through ALL the lines of all orders. If PurchLine2 is the same table as where this code is in, the PurchLine2.MODIFY will call "itself" (aka recursion), giving unpredictable results in this case.

    Please: indent the code with 2-3 spaces between REPEAT / UNTIL. For execution this will not be necessary, but the code becomes 100 times easier to read.

    The PurchLine2.INIT will manipulate your REPEAT / UNTIL loop.

    My first conclusion: this code does not make good sense. If you only want to fill your new field, you do not need the loop, but you can calculate your field with:

    VatAmount := "Amount Including VAT" - Amount;

    The modify will be done automatically by the system.
    IF User.Loves('Edo') THEN ok() ELSE currReport.genSkip;
  • ManiNavManiNav Member Posts: 82
    Accepted Answer
    Hi edoderoo,

    Thank you for your guidelines.
    Actually I was adding that field in table to drag in page. so, i wrote the code in page OnAfter get record() trigger, its working fine now.

    "Vat Amount" := "Amount Including VAT" - Amount;
    Rec.Modify;

    Thanks,
    Mani.
  • edoderooedoderoo Member Posts: 76
    Ah, on a page that will be different indeed. But be careful with functionality *on a page*. If you need to fill a data field in the table, the code should be in the table, as you never know what functionality is changing the line.amount field. It can be done from a report or code unit too, so it will not execute any code on a page, and you miss the functionality to fill this field.
    IF User.Loves('Edo') THEN ok() ELSE currReport.genSkip;
Sign In or Register to comment.