How to modify all table fields (record to record)

kamilolszewski_
Member Posts: 4
Hello everyone
I am writing a script that is supposed to copy Item data from one company to another.
I have a problem with Items that already exist - I want to modify them but I don't want to write all fields in the table. I decided to create loops and use RecordRef and FieldRef. Unfortunately, this approach takes a long time (over 24 hours).
I am looking for another solution. I wrote something like that (but it doesn't work):
There are over 200 fields in my table and I don't want to write each field in the style:
it will work but I am looking for another solution.
Do you have any suggestions?
I am writing a script that is supposed to copy Item data from one company to another.
I have a problem with Items that already exist - I want to modify them but I don't want to write all fields in the table. I decided to create loops and use RecordRef and FieldRef. Unfortunately, this approach takes a long time (over 24 hours).
sourceTable.OPEN(objectId, FALSE, fromCompany); sourceFieldFilter := sourceTable.FIELD(54); sourceFieldFilter.SETFILTER('FALSE'); IF sourceTable.FINDSET THEN BEGIN targetTable.OPEN(objectId, FALSE, toCompany); REPEAT targetTable.INIT; FOR intCounter := 1 TO sourceTable.FIELDCOUNT() DO BEGIN sourceField := sourceTable.FIELDINDEX(intCounter); targetField := targetTable.FIELD(sourceField.NUMBER); targetField.VALUE(sourceField.VALUE); IF FORMAT(sourceField.CLASS) <> 'FlowField' THEN BEGIN targetTable.MODIFY(FALSE); COMMIT; END; END; UNTIL sourceTable.NEXT = 0; END; targetTable.CLOSE(); sourceTable.CLOSE();
I am looking for another solution. I wrote something like that (but it doesn't work):
fromItem.INIT; fromItem.CHANGECOMPANY(companyName); fromItem.SETFILTER(fromItem.Blocked, 'FALSE'); IF fromItem.FINDSET THEN BEGIN REPEAT IF toItem.GET(fromItem."No.") THEN BEGIN toItem := fromItem toItem.MODIFY; END; COMMIT; UNTIL fromItem.NEXT = 0; END;
There are over 200 fields in my table and I don't want to write each field in the style:
toItem.Fields1: = fromItem.Fields1; toItem.Fields2: = fromItem.Fields2; toItem.Fields3: = fromItem.Fields3; .... .... toItem.Fields200: = fromItem.Fields200; toItem.MODIFY;
it will work but I am looking for another solution.
Do you have any suggestions?
0
Best Answers
-
Then try to use
toItem.TRANSFERFIELDS(fromItem, FALSE);
instead oftoItem := fromItem;
1 -
As @AlexDen said, you should absolutely use the transferfields function for this kind of task.
If performance is still an issue (which it should'nt be with the transferfields approach) try committing the changes every 1000 records (assuming you sync that many).
/WisaAustrian NAV/BC Dev1
Answers
-
Hi,
Your "another" solution looks correct and should work.
Do you execute this code in the correct company?
Maybe, you have to add:toItem.ChangeCompany(toCompany);
0 -
Hi,
Your "another" solution looks correct and should work.
Do you execute this code in the correct company?
Maybe, you have to add:toItem.ChangeCompany(toCompany);
Thanks for the tip.
I am sure he execute in the correct company. System has a problem with MODIFY function, if the record already exists - as if it would also like to modify the key (??).
I get the following message:
0 -
Then try to use
toItem.TRANSFERFIELDS(fromItem, FALSE);
instead oftoItem := fromItem;
1 -
As @AlexDen said, you should absolutely use the transferfields function for this kind of task.
If performance is still an issue (which it should'nt be with the transferfields approach) try committing the changes every 1000 records (assuming you sync that many).
/WisaAustrian NAV/BC Dev1 -
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