How to know if a Record is a temporary record

stevedivimaststevedivimast Member Posts: 39
Hi all,
There is a method to know if a Record variable is a Temporary Record?
My problem is I'am using a Sales Header Temporary record variable and i need different behavior in Record trigger.

eg.

OnInsert() (Trigger of Sales Header Table)
IF ISTEMPORARY THEN
do something
ELSE
do something else

Thanks
Steve

Answers

  • SavatageSavatage Member Posts: 7,142
    i need different behavior in Record trigger.

    Perhaps you vcan explain a bit more of what you are trying to accomplish?
  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    Add a new boolean field to the Sales Header table, and name it "IsTemporary".
    In your code that fills the temporary table, set this new field to True.

    Then you can query this field in the OnInsert-trigger:
    IF IsTemporary THEN
    do something
    ELSE
    do something else
    
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    There are at least two methods:

    1. You can add extra field to the table, Integer type, with Autoincrement=Yes. Autoincrement will work only in real table, so if your autoincrement field is 0 after insertion then you are in temporary record. Problem is that you do not know this in OnInsert trigger, as this trigger is launched before record is physically inserted into database, and physical insertion of record increments AutoIncement field..

    2. You can open another table variable, pointing to real table, and try to read newly inserted record. If you succeed you are in real table, if not - temporary. Again - not in OnInsert I'm afraid...

    Perhaps in OnInsert trigger you could try INSERT, then make your test, then DELETE current record. After finishing OnInsert trigger the record will be re-inserted. It should work however I've never tried this. And of course it is far from optimal as instead of one database insert you will have three physical insert-delete-insert operations...

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • kinekine Member Posts: 12,562
    Best is to NEVER use triggers when working with TEMPORARY table... and this is one example of possible problems when you are using the triggers... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • garakgarak Member Posts: 3,263
    use the triggers like modify(true) or insert(true) or validate of an field only if you know what for source is in this trigger.

    for example u have a Temp.Record "A" and a real record "A". In the onmodify or validate(of some Fields) are other tables modified based on the record "A".
    Now you modify(true) or Validate(somefield) your temp.record "A". what do you thin would be done ..... ;-)

    Regards
    Do you make it right, it works too!
  • DenSterDenSter Member Posts: 8,304
    I think Steve knows about this, and that is the entire purpose of his question. If you could distinguish between regular and temporary records, you could program behavior accordingly.
  • reijermolenaarreijermolenaar Member Posts: 256
    Hi Steve,

    It depends on what version of NAV you use.
    In 2009 you can use the property IsTemporary of the variabletype RecRef:
    RecRef.GETTABLE(TmpCustomer);
    MESSAGE(FORMAT(RecRef.ISTEMPORARY));
    

    Is earlier versions you can test the tablenumber of the variabletype RecRef.
    If you have a temporary record the number is higher than 2000100000:
    RecRef.GETTABLE(TmpCustomer);
    MESSAGE(FORMAT(RecRef.NUMBER >= 2000100000)); 
    

    Regards,
    Reijer Molenaar
    Object Manager
  • David_SingletonDavid_Singleton Member Posts: 5,479
    kine wrote:
    Best is to NEVER use triggers when working with TEMPORARY table... and this is one example of possible problems when you are using the triggers... ;-)


    I think this is the best advise. If you get in the habit of calling triggers on temp tables, one day you will do it on a trigger you have not modified and things will go bad. Try to rethink what you are doing and do it differently.
    David Singleton
  • stevedivimaststevedivimast Member Posts: 39
    Reijer Molenaar's solution is exactly what I was looking for!!! =D>
    I searched in Nav documentation (Nav 2009 Sp1) for a function like ISTEMPORARY, but I didn't see it.
    This seem to be an undocumented RecordRef function.
    Thanks to everybody.
    Every time i post a questions in this forum I get back always very high quality answer in few hours.
    I love this forum!!
Sign In or Register to comment.