Can i use codeunit to modify data in a table?
asembereng
Member Posts: 220
Hi
Can i use codeunit to modify data in a table. for example i have created new field but i want to copy data from the same table but different field to this new field created. Can codeunit do that? If so, how?
Thanks.
Can i use codeunit to modify data in a table. for example i have created new field but i want to copy data from the same table but different field to this new field created. Can codeunit do that? If so, how?
Thanks.
0
Comments
-
You can modify the table data through Codeunit or Report(Batch Job).Now or Never0
-
You should run through NAV development tutorials.
Basically, you setup a variable:
grecTable, DataType=Record, Subtype=table
In the code window, you can enter:grecTable.NewField := grecTable.OldField; grecTable.MODIFY;
This would be the simplest method, regardless of any validates on the fields and code in the OnModify.
We've now openned Pandora's Box for you. Now it's up to you to make sure you review your tutorials and documentation to make sure you don't destroy your database.Kristopher Webb
Microsoft Dynamics NAV Developer0 -
I am having similar problem.
Where actually can one place your code.
I created a new code unit with you code place on the onrun trigger.
but when i click on run, i have an error which reads "Employee no. does not exist"
Any ideas please? where do you thing the code is best place? any other sample code ?0 -
1. Get the record of the table you're trying to modify
2. Set the value on the record
3. MODIFY the recordConfessions of a Dynamics NAV Consultant = my blog
AP Commerce, Inc. = where I work
Getting Started with Dynamics NAV 2013 Application Development = my book
Implementing Microsoft Dynamics NAV - 3rd Edition = my 2nd book0 -
This is what i an doing:
Documentation() OnRun() IF Emp.GET("No.") THEN Emp."SSNo." := Emp."No."; Emp.MODIFY;what am i not doing correctly.
Its still gives me the same error.0 -
It should be something like this:
Emp.RESET; IF Emp.FINDSET(TRUE, FALSE) THEN BEGIN; REPEAT; Emp."SSNo." := Emp."No."; Emp.MODIFY(TRUE); UNTIL Emp.NEXT = 0; END;
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
IF Emp.GET('Some Value') THEN BEGIN Emp."SSNo." := Emp."No."; Emp.MODIFY; END;1:
You have to pass the GET method an actual value.
2:
Always use BEGIN and END statements. You NEED them when you have more than one line of code to execute, and I think it's a good habit to just always use them, so you can explicitly tell which lines are meant to be executed.0 -
I have use one value in the get and it does what i wanted, but how can i do it for all automatically?
i have tried tinoruijs code but is giving me eroror which says " You have specified an unknown variable FINDSET when i try to compile it. What is the datatype of FINDSET?0 -
kolaboy wrote:I have use one value in the get and it does what i wanted, but how can i do it for all automatically?
Have you tried the I wrote above?
Then you will loop through all employees.
If you want to filter on the employees, you will have to use something like:Emp.SETRANGE(blnField, TRUE);
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
DenSter wrote:I think it's a good habit to just always use them, so you can explicitly tell which lines are meant to be executed.
Certainly a good habit. I find it also makes the code easier to read.
Another good habit is using ; after every line when possible. The ; is used in the debugger and code coverage. If you don't use the ;, the debugger will skip the line. And Code Coverage won't mark it read.
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
I have tried both but the FINDSET is not working for me. Giving the same error.
DenSter code does it but on only the one specified. I need it done on all at once.Emp.SETRANGE(blnField, TRUE);
what is the datatype of blnField i made it boolean but does not work.Any ideas please0 -
kolaboy wrote:I have tried both but the FINDSET is not working for me. Giving the same error.
DenSter code does it but on only the one specified. I need it done on all at once.Emp.SETRANGE(blnField, TRUE);
what is the datatype of blnField i made it boolean but does not work.Any ideas please
Emp.SETRANGE(blnField, TRUE); was just an example of a filter on a boolean field.
Which version are you on? FINDSET is present in NAV from version 4.0 SP1 and later.
Maybe the code shoud be:Emp.RESET; IF Emp.FIND('-') THEN BEGIN; REPEAT; Emp."SSNo." := Emp."No."; Emp.MODIFY(TRUE); UNTIL Emp.NEXT = 0; END;
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
I am working on 4.0.
I did this but it gives me error like "Employee does not exist. Identification fields and value: No. = '463',SSNo.= '463'. " I have both No. and SSNo. as primary key.
what is the problem?
Thanks0 -
kolaboy wrote:I am working on 4.0.
I did this but it gives me error like "Employee does not exist. Identification fields and value: No. = '463',SSNo.= '463'. " I have both No. and SSNo. as primary key.
what is the problem?
Thanks
Is SSNo. part of the primary key?
Why?
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
Sorry i has worked. I just remove the new no. which is SSNo as a primary key and it worked. How then can it work when both are primary keys?0
-
kolaboy wrote:I am working on 4.0.
I did this but it gives me error like "Employee does not exist. Identification fields and value: No. = '463',SSNo.= '463'. " I have both No. and SSNo. as primary key.
what is the problem?
Thanks
The problem is that you have both No. and SSNo. as primary key.
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
kolaboy wrote:Sorry i has worked. I just remove the new no. which is SSNo as a primary key and it worked. How then can it work when both are primary keys?
Why do you want them both in the primary key?
Certainly when they are both the same, it has no use.
What are you trying to achieve with both the fields in the primary key?
Can't SSNo just be a secondary key?
:-k
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
I made SSNo. a primary key.
now i am trying it this way:Emp.RESET; IF Emp.FIND('-') THEN BEGIN; REPEAT; Emp."No." := Emp."SSNo."; Emp.MODIFY(TRUE); UNTIL Emp.NEXT = 0; END;
Error " Employee No. 55123 does not exist"
The issue is No. = 123 and SSNo. = 55123, now i want to replace no. = 123 by no. = 55123, This is what i am trying to acheive.
Any ideas.
Thanks0 -
kolaboy wrote:I made SSNo. a primary key.
It is not recommended to change the primary key. Why do you want to do that?kolaboy wrote:now i am trying it this way:Emp.RESET; IF Emp.FIND('-') THEN BEGIN; REPEAT; Emp."No." := Emp."SSNo."; Emp.MODIFY(TRUE); UNTIL Emp.NEXT = 0; END;
Error " Employee No. 55123 does not exist"
The issue is No. = 123 and SSNo. = 55123, now i want to replace no. = 123 by no. = 55123,
Any ideas.
Thanks
Are you sure No. isn't in the primary key anymore?
If it is in the primary key, you should use RENAME instead of MODIFY.
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
No. is the primary key. SSNo. is now a secondary key.
How can the code work the way i just posted?0 -
kolaboy wrote:No. is the primary key. SSNo. is now a secondary key.
How can the code work the way i just posted?
Before helping you with the code...
What do you want to achieve? And why?
(Perhaps I shoud have asked this question straight away..
)
I guess you want to change the primary key into a new number and store the old primary key in the field where the new number was stored? :-k
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
I guess you want to change the primary key into a new number and store the old primary key in the field where the new number was stored?
This is exactly what i want to achieve. Any idea please?
Thanks0 -
kolaboy wrote:I guess you want to change the primary key into a new number and store the old primary key in the field where the new number was stored?
This is exactly what i want to achieve. Any idea please?
Thanks
It's better to use another new field called "Old No.".
But the looping through the records and renaming them, could be tricky, depending on the range of the old and new numbers.Emp.RESET; IF Emp.FIND('-') THEN BEGIN; REPEAT; if Emp."SSNo." <> '' THEN BEGIN; Emp."Old No." := "No."; Emp.RENAME(Emp."SSNo."); Emp."SSNo." := ''; Emp.MODIFY(TRUE); END; UNTIL Emp.NEXT = 0; END;
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
Its the values of No. that i want to change using rename with the values of SSNo. and then give the values of No. to SSNo.
How about this:Emp.RESET; IF Emp.FIND('-') THEN BEGIN; REPEAT; IF Emp."SSNo." <> '' THEN BEGIN; "TemNo." := "No."; Emp."No." := "TemNo."; Emp.RENAME(Emp."No."); Emp."SSNo." := ''; Emp.MODIFY(TRUE); END; UNTIL Emp.NEXT = 0; END;
But this give an error which runs thus: "Employee No. '' does not exist"
Any idea?0 -
I think the MODIFY shoud be before the RENAME.
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
Hi kolaboy,
In the past I tried to do exactly what you are doing, but RENAME function did not work properly for me. I'm not sure but I think Navision gets really pissed of when asked to RENAME a record thru code.
The, the best turnaround for this (that I found) is that, if you REALLY need to rename your record, then you should create a new one with the new data and then delete the old one. This applies if you are trying to change any fields on the primary key.
When you create the new record, use TRANSFERFIELDS, and just change the Primary Key before INSERTING... and don't forget to DELETE the old record.
Marcelo Borges
D365 Business Central Solutions Architect
BC AL/NAV C/AL Developer
BC Repositories.com0 -
You mean mean this:
Emp.RESET; IF Emp.FIND('-') THEN BEGIN; REPEAT; IF Emp."No." <> '' THEN BEGIN; "TemNo." := "No."; Emp."No." := "TemNo."; Emp."No." := ''; Emp.MODIFY(TRUE); Emp.RENAME(Emp."No."); END; UNTIL Emp.NEXT = 0; END;
This still gives the same error.0 -
Good suggestion by Marcelo.
But..
It all depends on records connected to the record you're copying.
So if there are for example hour registration records connected to an employee, you have to modify this records to the new employee record...
The 'beauty' of RENAME is that it also renames the connected (by TableRelation) records.
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
To make the code below work, you have to add a field "Old No.".
Because else SSNo. will never be empty and a strange loop through the records can be possible. Depending on the old and new range of the numbers.Emp.RESET; IF Emp.FIND('-') THEN BEGIN; REPEAT; IF Emp."SSNo." <> '' THEN BEGIN; "TemSSNo." := "SSNo."; Emp."Old No." := Emp."No."; Emp."SSNo." := ''; Emp.MODIFY(TRUE); Emp.RENAME("TemSSNo."); END; UNTIL Emp.NEXT = 0; END;
Tino Ruijs
Microsoft Dynamics NAV specialist0 -
tinoruijs wrote:To make the code below work, you have to add a field "Old No.".
Because else SSNo. will never be empty and a strange loop through the records can be possible. Depending on the old and new range of the numbers.Emp.RESET; IF Emp.FIND('-') THEN BEGIN; REPEAT; IF Emp."SSNo." <> '' THEN BEGIN; "TemSSNo." := "SSNo."; Emp."Old No." := Emp."No."; Emp."SSNo." := ''; Emp.MODIFY(TRUE); Emp.RENAME("TemSSNo."); END; UNTIL Emp.NEXT = 0; END;
I have use the above code but it seems its doing it for one record in the middle of the file and the last three. Its skipping the rest and not doing anything.
What is may be wrong?0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.8K 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
- 328 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



