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:
In most cases you will end up with some code like this:
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:
This trick works with Pages, Reports and Forms!
Enjoy.
Wow! This is amazing and very usefull. Thanks AJ!
And codeunits!
I have a blog about this coming up (some day .. ;-)).
Great trick, thank you very much!
Amazing – finally something to use the Variant datatype for 🙂
Amazing!
And it also works with FORM.RUN in NAV2009R2 and 50SP1!
Amazing!
And it also works with FORM.RUN in NAV2009R2 and 50SP1!
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.
🙁
Pingback: Run a Page or Report with RecordRef variable | Pardaan.com
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;
Pingback: The power of Variants » waldo's blog
Fantastic! This is something I search for for years.
Hopefully it works like I expect it to work… need to play around… 🙂
Pingback: Run a Page or Report with RecordRef variable | NAV Italian People Blog
Does not work in RTC-Client 🙁
Sorry, all is well …
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
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
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.
Beautiful. I was looking for something like this. It works in NAV2015 too.
Thank you very much!
Pingback: How To: Execute a Page or a Report using a RecordRef variable - Code4NAV.com
Pingback: How To: Execute a Page or a Report using a RecordRef variable - Microsoft Dynamics NAV Community
Superb.. Thanks!
It works in business central 14 version too.
Thanks a lot !