I have a form where I loadAllRecords() and then invertRecords() to get an empty foundset. From here on I create x records to be printed.
When I show the form normally it does show the correct amount of records. But when invoking printpreview (even manually) it shows the whole table.
Servoy Developer
Version R2 2.2.1rc-build 330
Java version 1.5.0_02-36 (Mac OS X)
I found the solution to this problem with help of Jan Blok (thanks Jan!).
I thought I’d share this with you so you don’t have to pull the hair out of your head like I did.
This behavior is not a bug but by design.
Foundsets are defined by queries. This can be the initial query (select all) or a query by a user or relationship (or in a method).
When you add records to it then these new records usually aren’t part of the query done to get the foundset (unless it was a select all).
When Servoy prints it needs this query to do the group by and such defined in the form parts.
So the original query is fired again and caused this odd behavior I described before.
How did I make it work?
What I did was adding a column to my table (varchar(30)) to hold a unique ID. A UID in this case.
So my method looks something like this:
var UID = new java.rmi.server.UID(); // This will give me a unique ID
controller.find();
myUIDfield = UID;
controller.search();
// Now I have an empty foundset because no records exist yet with this unique ID.
//
// Here I add my new records including adding the the UID to each record. (!)
//
controller.newRecord();
field1 = 'some value';
field2 = 'some other value';
myUIDfield = UID;
// Show the print preview
controller.showPrintPreview();
// Servoy will fire the query I did in the first part of the method
// and lo and behold...my newly added record(s) appear.
I use this technique for making mailings.
I hope this explanation and technique will save you some headaches.
// Now I have an empty foundset because no records exist yet with this unique ID.
//
// Here I add my new records including adding the the UID to each record. (!)
//
controller.newRecord();
field1 = ‘some value’;
field2 = ‘some other value’;
myUIDfield = UID;
// Show the print preview
controller.showPrintPreview();
// Servoy will fire the query I did in the first part of the method
// and lo and behold…my newly added record(s) appear.
I use this technique for making mailings.
I hope this explanation and technique will save you some headaches.
Do you think it can also work by setting an empty foundset with an “never true” query (like setting pkfield < 0)?
We have just run into the same problem, so Robert’s “tip” was very helpful.
We have, however, also come across a problem described here:
The problem is, that code execution doesn’t really allow you to do something after the print preview. Without having played with this for hours, we couldn’t really find a solution. We have tried several approaches:
Attach a method to the onPrintPreviewCmd: here you have the same problem, code runs right to the end
Attach a method to the onHide event of the print form: this event is fired BEFORE the preview shows, not after you return to the original form
So the question is: has anyone managed to execute code directly after a print preview?
Riccardino:
Do you think it can also work by setting an empty foundset with an “never true” query (like setting pkfield < 0)?
The issue is not how to get an empty foundset. You can use several approaches for that (clearFoundset, loadAllRecords/InvertRecords, etc.).
The point is that that same query is fired when you do the showPrintPreview and will result (in this case) in no records.
In my example I do use a ‘never true’ query but make it a true query by adding records that will match that specific query.
So when using showPrintPreview that query is fired and it shows my newly added records.
The problem is, that code execution doesn’t really allow you to do something after the print preview. Without having played with this for hours, we couldn’t really find a solution. We have tried several approaches:
Attach a method to the onPrintPreviewCmd: here you have the same problem, code runs right to the end
Attach a method to the onHide event of the print form: this event is fired BEFORE the preview shows, not after you return to the original form
So the question is: has anyone managed to execute code directly after a print preview?
Now we have the same problem as well as Patrick already described. Same question here: Is there a way to execute code directly after a print preview?