Creating a new record when creating a new item

MichaelleeMichaellee Member Posts: 65
Hi there,

I am trying to create a new record in another table when a new record is created in the item table. The table in question needs to use the newly created item number on insert and change as the primary key.

So, the code that I used was:


Form - OnInsertRecord(BelowxRec : Boolean) : Boolean
Blocked:= TRUE;
"Reordering Policy":= "Reordering Policy"::"Maximum Qty.";
LCHeader.INIT;
LCHeader."No.":="No.";
LCHeader."Item No." := "No.";
LCHeader.INSERT;

Unfortunately, I am guessing that as the item no has not been created, it is picking up a blank item number. Could you please tell me where the code should really sit to enable me to pick up the item number once the item no. has been created?
Many thanks,

Mike

Comments

  • SavatageSavatage Member Posts: 7,142
    First I would put the code on the item table not the form on the oninsert & onmodify (if you need on modify) but Insert code will not work beacuse it already exists (but that's another issue)

    Secondly why would you use two field to hold the same value?
    LCHeader."No.":="No.";
    LCHeader."Item No." := "No.";

    What's the key of the LCHeader table?
    You could on insert of the item table check to see if the item already exists on whatever your LCtable is. If it exists then MODIFY, If it doesn't then INSERT
  • MichaelleeMichaellee Member Posts: 65
    Many thanks Savatage for the help. I am new to the code designer in tables ans forms as I have only really dealt with reports.

    I have placed my code on the OnInsert part of the Item Card but now I am stuck on 2 issues:

    1) I know for certain that the LCTable will not be manually filled in so the issue of there already being one is not a problem, BUT a usr may wish to change the Item no. from A0001 to A001A etc. How am I able to change the No. field on the LCTable? I would have guessed to use the:

    No. - OnValidate()

    part?

    2) I tried to add this code to copy the description to the LCTable

    Description - OnValidate()
    IF ("Search Description" = UPPERCASE(xRec.Description)) OR ("Search Description" = '') THEN
    "Search Description" := Description;

    LCHeader.SETRANGE("No.", "No.");
    IF LCHeader.FIND('-') THEN
    LCHeader.Description := Description;

    But the Description does not copy over? I checked that the LCTable record was there but no joy.

    Many thanks for your help.

    Mike
  • DenSterDenSter Member Posts: 8,305
    you forgot to write it to the database
    LCHeader.SETRANGE("No.", "No."); 
    IF LCHeader.FIND('-') THEN BEGIN
      LCHeader.Description := Description; 
      LCHeader.MODIFY; //that's the one you forgot
    END;
    
    When a user changes the primary key of a table, the OnRename trigger fires, so that's where you'd program that. Mind you though that you will need to do a RENAME instead of MODIFY, and that you will get a warning message.
  • DenSterDenSter Member Posts: 8,305
    By the way, it would be less work to program this in the OnModify trigger, and only include the fields you want to copy.
  • MichaelleeMichaellee Member Posts: 65
    haha! Oops! Thanks Denster for the heads up. I'll check out the OnRename part as well.

    I know that this is bread and butter for the majority of you but please be patient with me while I try to work and understand NAV more in the coding sense.

    Thanks,

    Mike
  • SavatageSavatage Member Posts: 7,142
    OnInsert of item table (something Like)
    IF LCHeader.FIND('-')
    THEN BEGIN
    LCHeader."Item No." := "No.";
    LCHEADER."No." := "No.";
    LCHeader.INSERT;
    End;

    If you wish for the LCheade table to change along with an item no change then you would add code to the OnRename trigger of the item table.
    (if your talking about that?) when you said change.

    As far as the description thing goes if you don't add a yourtable.MODIFY;
    code to you code then nothing will change.

    the c/side reference guid can help you with MODIFY.
    Since your table seems to be based off the item table then you can see how many things happen from the item table by looking at it's triggers and seeing the code already there. Perhaps you can think about the ondelete trigger too incase the item is deleted.

    I suggest doing all these test on a seperate database & testing before ever making it a live function.
  • SavatageSavatage Member Posts: 7,142
    denster types faster than I :(
  • DenSterDenSter Member Posts: 8,305
    Michaellee wrote:
    please be patient with me while I try to work and understand NAV more in the coding sense.
    No problem Mike, we all started somewhere, we're all here to help :mrgreen:
  • MichaelleeMichaellee Member Posts: 65
    Thanks for all the help guys!

    Well, I have been able to create a LCHeader record OnInsert, but am stuck on the OnRename. I have read the help but still no joy. Basically, the scenario is this.

    Item A0001 is created and a record is auto created in the LCHeader table called A0001. A user changed the item No. to A0001A and the LCHeader table's record renames to A0001A as well.

    My issues is this, if the user has changed the Item No.(A0001A), where is the previous item No (A0001) stored? I was looking at xRec but that didn't really help me in the coding front.

    Thanks,

    Mike
  • MichaelleeMichaellee Member Posts: 65
    Just to provide you with the code that I used:

    OnRename()

    LCHeader.SETRANGE("No.", xRec."No.");
    IF LCHeader.FIND('-') THEN
    BEGIN
    LCHeader."No.":= "No.";
    LCHeader.RENAME("No.");
    END;

    I have checked the debugger and it seems that I the recird is found using xRec."No." but it doesn't seem to allow me to rename... I have checked and the primary key is "No."

    Thanks for you rhelp,

    Mike
  • SavatageSavatage Member Posts: 7,142
    1)Item A0001 is created and a record is auto created in the LCHeader table called A0001.

    2)A user changed the item No. to A0001A and the LCHeader table's record renames to A0001A as well.

    My issues is this, if the user has changed the Item No.(A0001A), where is the previous item No (A0001) stored? I was looking at xRec but that didn't really help me in the coding front.

    Wht do you mean previous Item #? It was changed, isn't that what you wanted?
  • DenSterDenSter Member Posts: 8,305
    It is literally a name change of the record. The record is still there, it only has different unique identifying attributes. It's the same as though you would go to city hall to get your legal name changed. You are still the same person, the "old one" doesn't get stored anywhere.
Sign In or Register to comment.