SetPrinterSelection; REPORT.RUNMODAL(TheReportID, FALSE, FALSE, Records); RestorePrinterSelection;
SetPrinterSelection() // delete any records in the temp printer selections record TempPrinterSelections.RESET; TempPrinterSelections.DELETEALL(FALSE); // filter the printer selections by report id and user id PrinterSelections.RESET; PrinterSelections.SETRANGE(PrinterSelections."Report ID", TheReportID); PrinterSelections.SETRANGE(PrinterSelections."User ID", USERID); // if a record exists for this user and report we need to save it off and modify it // there cannot be more than one record with these filters IF PrinterSelections.FIND('-') THEN BEGIN // save the printer selection into the temporary table TempPrinterSelections := PrinterSelections; TempPrinterSelections.INSERT; // change the printer selections record so it will now print to the api printer PrinterSelections."Printer Name" := 'Zetadocs Automation Subsystem'; PrinterSelections.MODIFY; END ELSE BEGIN // if there are no records we need to make a new one for this user and report PrinterSelections.INIT; PrinterSelections."Report ID" := TheReportID; PrinterSelections."User ID" := USERID; PrinterSelections."Printer Name" := 'Zetadocs Automation Subsystem'; PrinterSelections.INSERT; END; // make sure the transaction is committed to the database COMMIT;
RestorePrinterSelection() // filter the printer selections table so we will see the selections for this report PrinterSelections.RESET; PrinterSelections.SETRANGE(PrinterSelections."Report ID", TheReportID); PrinterSelections.SETRANGE(PrinterSelections."User ID", USERID); // We should always find one record because we put it there IF PrinterSelections.FIND('-') THEN BEGIN TempPrinterSelections.RESET; IF TempPrinterSelections.FIND('-') THEN BEGIN PrinterSelections := TempPrinterSelections; PrinterSelections.MODIFY; END ELSE BEGIN PrinterSelections.DELETE; END; END; COMMIT;
Comments
The error just before it crashes is as follows:
Another user has modified the record for this Printer Selection after you retrieved it from the database.
Enter your changes again in the updated window, or start the interrupted activity again.
Identification fields and values:
User ID='<regular user ID>',Report ID='205'.
Also, this problem occurs on NAV 4.0.
2) You have one big hole in your process - COMMIT. If someone will use this code somewhere, you could have big mess in the database. E.g. when the report fail, the setting will not be restored, and thus next call will "backup" wrong setting and you will never get back the original setting in the table. And of course the commit could break other transactions already running before this code...
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.
Incidentally, that error is caused by modifying the primary key of a record and then not retrieving it again from the database. So do a GET or FIND later on in your code.
I believe I have seen several posts on this, specifically about printing to PDF so you could find some code there.
My Blog - nav.education