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.
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.
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 ?
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.
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?
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:
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.
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.
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.
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.
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.
Thanks
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.com
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;
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?
Comments
Basically, you setup a variable:
grecTable, DataType=Record, Subtype=table
In the code window, you can enter:
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.
Microsoft Dynamics NAV Developer
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 ?
2. Set the value on the record
3. MODIFY the record
AP Commerce, Inc. = where I work
Getting Started with Dynamics NAV 2013 Application Development = my book
Implementing Microsoft Dynamics NAV - 3rd Edition = my 2nd book
what am i not doing correctly.
Its still gives me the same error.
Tino Ruijs
Microsoft Dynamics NAV specialist
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.
RIS Plus, LLC
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?
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:
Tino Ruijs
Microsoft Dynamics NAV specialist
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 specialist
DenSter code does it but on only the one specified. I need it done on all at once. 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:
Tino Ruijs
Microsoft Dynamics NAV specialist
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 specialist
The problem is that you have both No. and SSNo. as primary key.
Tino Ruijs
Microsoft Dynamics NAV specialist
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 specialist
now i am trying it this way:
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.
Thanks
It is not recommended to change the primary key. Why do you want to do that?
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 specialist
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 specialist
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.
Tino Ruijs
Microsoft Dynamics NAV specialist
How about this:
But this give an error which runs thus: "Employee No. '' does not exist"
Any idea?
Tino Ruijs
Microsoft Dynamics NAV specialist
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.
D365 Business Central Solutions Architect
BC AL/NAV C/AL Developer
BC Repositories.com
This still gives the same error.
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 specialist
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.
Tino Ruijs
Microsoft Dynamics NAV specialist
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?