Run a Page or Report with RecordRef variable

9 Apr

The RecordRef variable in Microsoft Dynamics NAV is a very useful data type. Especially when writing generic code where you can’t predict the record type. Using the RecordRef variable can drastically reduce your code.

But sometimes you need to run a certain Page (or Report), based on the record type. To run the default lookup Page of a table, you can use 0 as the Page ID.

When you try to run a Page with a RecordRef variable instead of a Record type, you get an error message:

image

image

 

In most cases you will end up with some code like this:

image

 

There is a solution to avoid this kind of code! And it works with Windows Client (RTC) and Classic Client. The trick is to first put the RecordRef variable in a Variant variable, and then run the Page with the Variant:

SNAGHTML14df7877

image

 

This trick works with Pages, Reports and Forms!

Enjoy.

21 thoughts on “Run a Page or Report with RecordRef variable

  1. Great trick, thank you very much!
    Amazing – finally something to use the Variant datatype for 🙂

  2. sadly you don’t get back the selected record in the variant…

    For example:
    IF FORM.RUNMODAL(0,VarRecRef) = ACTION::LookupOK THEN

    In this case the VarRecRef does not point to the selected Record.
    🙁

  3. Pingback: Run a Page or Report with RecordRef variable | Pardaan.com

  4. Hi Chris,

    It appears that in Classic Client the the VarRecRef does not pick up filters or selected records. So the usage is limited here.
    But in NAV 2013 it works. I didn’t test in NAV 2009 RTC, but I assume that it works the same as with NAV 2013.

    This code in NAV 2013 returns the selected record:

    RecRef.OPEN(DATABASE::Customer);
    VarRecRef := RecRef;
    IF PAGE.RUNMODAL(0,VarRecRef) = ACTION::LookupOK THEN BEGIN
    RecRef := VarRecRef;
    FldRef := RecRef.FIELD(1);
    MESSAGE(‘%1’,FldRef.VALUE);
    END;

  5. Pingback: The power of Variants » waldo's blog

  6. Fantastic! This is something I search for for years.
    Hopefully it works like I expect it to work… need to play around… 🙂

  7. Pingback: Run a Page or Report with RecordRef variable | NAV Italian People Blog

  8. Absolutely fantastic! Finally we can build true generic output solutions.

    Isn’t this piece of code nice? 🙂

    RecRef.GET(RecID);
    RecRef.SETRECFILTER;
    VarRecRef := RecRef;
    REPORT.SAVEASPDF(ReportID, PathAndFilename, VarRecRef);

    Thanks a lot for this information!

    /Lars

  9. Thank you so much! That’s exactly the piece of code I needed! It works perfectly (NAV 2013 R2)! Even with a SETVIEW:

    RecRef.OPEN(TableID);
    RecRef.SETVIEW(TableFilterString);
    VarRecRef := RecRef;
    REPORT.SAVEASPDF(ReportID, FileName, VarRecRef);

    This is so amazing! Thanks a lot again 🙂

    Markus

  10. This code in NAV 2009 (Classic Client) don’t return the selected record:

    RecRef.OPEN(DATABASE::Customer);
    VarRecRef := RecRef;
    IF FORM.RUNMODAL(0,VarRecRef) = ACTION::LookupOK THEN BEGIN
    RecRef := VarRecRef;
    FldRef := RecRef.FIELD(1);
    MESSAGE(‘%1′,FldRef.VALUE);
    END;

    In NAV5.0 this code causes a crash.

  11. Pingback: How To: Execute a Page or a Report using a RecordRef variable - Code4NAV.com

  12. Pingback: How To: Execute a Page or a Report using a RecordRef variable - Microsoft Dynamics NAV Community

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.