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

ManiNav
Member Posts: 120
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.
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.
0
Best 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.2
Answers
-
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.0 -
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;1 -
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.2 -
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;0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions