Problem in inserting a copy of an item

poppinspoppins Member Posts: 647
Hi everyone,

I need to change some properties of my items.The problem is that I've already worked with them so I can not change the properties directly.

I decided to make a copy of the items, change the properties in the copied item and then block the original one...

The code I used in the OnAfterGetRecord of my report is the following:
CurrentItem.GET("No.");
IF CurrentItem.FINDFIRST THEN
BEGIN

CurrentItem.RENAME('C_'+CurrentItem.Description);

CopyItem.COPY(CurrentItem);
CopyItem.Description:= COPYSTR(CopyItem.Description, 2,STRLEN(CopyItem.Description)-2) ;
CopyItem."Costing Method":=3;

CurrentItem.Blocked := TRUE;
CopyItem.INSERT;

END;

The code generates the following error:
The Item No. already exists.

What shall I do???

Comments

  • bbrownbbrown Member Posts: 3,268
    Use the Debugger to step thru your code to see what it is doing. To start with, it's not updatign the item you think it is.
    There are no bugs - only undocumented features.
  • ChinmoyChinmoy Member Posts: 359
    The primary key of the item table is No. So, I guess after you copy the existing item to a new record, generate a new no. and put it on the new record and then insert.

    Chn
  • poppinspoppins Member Posts: 647
    bbrown wrote:
    Use the Debugger to step thru your code to see what it is doing.

    The instruction generating error is:
    CopyItem.INSERT;
    
    bbrown wrote:
    To start with, it's not updatign the item you think it is.

    How is that???
  • poppinspoppins Member Posts: 647
    Chinmoy wrote:
    The primary key of the item table is No. So, I guess after you copy the existing item to a new record, generate a new no. and put it on the new record and then insert.

    Chn
    How can I generate a new no. ???
  • bbrownbbrown Member Posts: 3,268
    poppins wrote:
    bbrown wrote:
    bbrown wrote:
    To start with, it's not updatign the item you think it is.

    How is that???

    CurrentItem.GET("No.");
    IF CurrentItem.FINDFIRST THEN
    

    The first line retrieves the Item you want. The second line moves to the first item in your item table. Unless you are trying to update the first item in your table, it is pointing to the wrong record. If you are, then the two lines are redundant.
    There are no bugs - only undocumented features.
  • bbrownbbrown Member Posts: 3,268
    poppins wrote:
    bbrown wrote:
    Use the Debugger to step thru your code to see what it is doing.

    The instruction generating error is:
    CopyItem.INSERT;
    

    Step thru "line by line" to see what it is doing,
    There are no bugs - only undocumented features.
  • poppinspoppins Member Posts: 647
    I think I am not using the RENAME function correctly...Infact, I want to modify the item description while the instruction I wrote modifies the Item No.
    What shall I do to modify the description not the No???
  • SavatageSavatage Member Posts: 7,142
    so you just want to change the description?

    and not copy an item as per you post name?

    description := mynewdescription;
    modify;

    why would you want to add a "C_" to the beginning of each description?
  • poppinspoppins Member Posts: 647
    I want to rename the description of the item so that I can insert a copy of the item in the database...
    By the way, i tried to change the code with modify...It didn't work and the description of the item didn't change...
  • SavatageSavatage Member Posts: 7,142
    is this a report you are using?
    is item the main dataitem?

    changing the description alone will not create a copy of the item.

    the key field is "no."

    you need new item no for each item copy
    what's your thinking behind "FINDFIRST"?
    If item is your dataitem and currentitem is a variable type record for the item table then it will run thru the records anyway.
  • poppinspoppins Member Posts: 647
    I modified the code I wrote above:
    CurrentItem.RESET;
    CopyItem.RESET;
    
    CurrentItem.SETRANGE(CurrentItem."No.","No.");
    IF CurrentItem.FIND('-') THEN
    BEGIN
    desc:= CurrentItem.Description;
    CurrentItem.Description:='C_'+desc;
    CurrentItem.Blocked := TRUE;
    CurrentItem.MODIFY;
    
    CopyItem.COPY(CurrentItem);
    
    CopyItem."No.":='';
    CopyItem.Description:= desc ;
    CopyItem."Costing Method":=3;
    CopyItem.Blocked := FALSE;
    
    CopyItem.INSERT;
    END;
    
    You're right...The problem is in the field "No."...I need to generate a new No. and assign it to the copy item before onsertion...but How to do that???
  • MBergerMBerger Member Posts: 413
    poppins wrote:
    You're right...The problem is in the field "No."...I need to generate a new No. and assign it to the copy item before onsertion...but How to do that???
    INSERT(TRUE) ;
    
    This will trigger the OnInsert code of T27, which will make a new number ( providing the associated number series is set to automatically generate them ).
  • bbrownbbrown Member Posts: 3,268
    MBerger wrote:
    poppins wrote:
    You're right...The problem is in the field "No."...I need to generate a new No. and assign it to the copy item before onsertion...but How to do that???
    INSERT(TRUE) ;
    
    This will trigger the OnInsert code of T27, which will make a new number ( providing the associated number series is set to automatically generate them ).

    No it won't because the "No." field already has a value.
    There are no bugs - only undocumented features.
  • ChinmoyChinmoy Member Posts: 359
    Why don't you read the Default No. series to generate Item No. from the Inventory Setup table and then call InitSeries from the NoSeriesManagement codeunit to generate the next number. Check how this function is called from the Item (27) table INSERT trigger.

    //IF "No." = '' THEN BEGIN -- You do not need to check this though
    GetInvtSetup;
    InvtSetup.TESTFIELD("Item Nos.");
    NoSeriesMgt.InitSeries(InvtSetup."Item Nos.",<<can be the same as the last argument>>,0D,<<your variable where you want the next number to come>>,<<pass the default number series code here>>);
    //END;

    Chn
  • MBergerMBerger Member Posts: 413
    bbrown wrote:
    No it won't because the "No." field already has a value.
    In his revised code he actually empties the "No." field.
  • bbrownbbrown Member Posts: 3,268
    MBerger wrote:
    bbrown wrote:
    No it won't because the "No." field already has a value.
    In his revised code he actually empties the "No." field.


    OK. Missed that. But not sure I'd assume he's using a number series to generate new item numbers.
    There are no bugs - only undocumented features.
  • poppinspoppins Member Posts: 647
    I corrected my code to remane the item No. instead of the description...I setup two request filter fields in my report: No. and Gen. Prod. Posting Group.
    Here is the code:
    CurrentItem.RESET;
    CopyItem.RESET;
    
    CurrentItem.SETRANGE(CurrentItem."No.","No.");
    IF CurrentItem.FIND('-') THEN
    
    BEGIN
    
    CopyItem.COPY(CurrentItem);
    num:=CurrentItem."No.";
    CurrentItem.RENAME('C_'+num);
    CurrentItem.Blocked := TRUE;
    CurrentItem.MODIFY;
    
    CopyItem."Costing Method":=3;
    CopyItem.INSERT;
    
    END;
    
    

    I tested it for one item or many items, it works just fine...
    The problem is when I try to run it for a Gen. Prod. Posting Group...I get the following error message:
    Overflow under type conversion of Text to Code:
    Value C_C_C_C_C_C_C_C_7000.
    

    I tried to debug the code...It seems like it is running forever...
    Why is that???What shall I do???
Sign In or Register to comment.