Printer Selection table permissions for regular user

pfedorypfedory Member Posts: 3
Hello,

I have a codeunit which modifies the printer selection for a report. Before you print the report, it is changed to our own printer and after, the printer is restored to the original printer. This works for a super user, but not for a regular user. I have added rights for the printer selection table and table data for the regular user, however when I run the report, NAV crashes sometimes when I'm setting the printer selection and sometimes when I'm restoring the printer selection.

Does anyone know why this would happen for a regular user but not a super user? Are there any other permissions needed aside from the printer selection table and table data which has already been added?

Here is a snippet from the codeunit which runs the report:
SetPrinterSelection;
REPORT.RUNMODAL(TheReportID, FALSE, FALSE, Records);
RestorePrinterSelection;

Here is the definition for SetPrinterSelection:
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;

Here is the definition for Restore Printer Selection:
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;

Any help would be greatly appreciated.
Regards,
Paul Fedory

Comments

  • pfedorypfedory Member Posts: 3
    Sorry, I should have added the error I'm seeing just before NAV crashes.
    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.
  • kinekine Member Posts: 12,562
    1) Try to assign only the needed fields instead assigning whole record when restoring the record.
    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...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • matttraxmatttrax Member Posts: 2,309
    Yep, avoid commits whenever possible.

    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.
Sign In or Register to comment.