How do I run a report from a Codeunit with certain options?

josephdeweyjosephdewey Posts: 87Member
I'm trying to call a report from within a Codeunit, and I want certain options to populate on the Options tab. Specifically, it's report 795, Adjust Cost - Item Entries, but I actually have this question for any report. And specifically, I want to have the "Post to G/L" checked when I run this report.

I know a good way of doing this is making a function in the report called Initialize with the variables as parameters, and then I can call this function from the Codeunit, before I run the report from the Codeunit.

The problem is that I can't modify 795, because I'm not a partner (Item Ledger Permissions error), so I'm wondering if there's another way to call 795 (or other reports) with specific options, without modifying the report.

Thanks in advance!
Joseph Dewey
Microsoft Dynamics NAV User

Comments

  • DenSterDenSter Posts: 8,043Member
    That report needs to be modified to either set default values automatically, or to have functions that accept them as parameters. If you don't have permission to do it in your license, you'll have to get your partner involved to do it for you.
  • josephdeweyjosephdewey Posts: 87Member
    So there is no other way?
    Joseph Dewey
    Microsoft Dynamics NAV User
  • Alex_ChowAlex_Chow Posts: 5,035Member
    Instead of a codeunit, you can use a process only report that runs without user prompts.
  • DenSterDenSter Posts: 8,043Member
    That report already IS processing only. He doesn't have the rights to modify it.
  • SogSog Posts: 1,049Member
    edited 2012-02-28
    To answer your question
    The problem is that I can't modify 795, because I'm not a partner (Item Ledger Permissions error), so I'm wondering if there's another way to call 795 (or other reports) with specific options, without modifying the report.

    The answer is no, without modifications a report's option can't be specified from another object (codeunit, report,...) unless a function is already available.
    |Pressing F1 is so much faster than opening your browser|
    |MCBMSS: 5.0 intro|MCTS:NAV09 839..841|JetReports© Certified Specialist|
    |Dynamics Anywhere: Mobile Business Solutions|
  • BernardJBernardJ Posts: 57Member
    And specifically, I want to have the "Post to G/L" checked when I run this report.
    If you are calling the report with use of request window then "Post to G/L" will be checked when you check "Automatic Cost Posting" in Inv. Setup
  • josephdeweyjosephdewey Posts: 87Member
    I was able to do it with the following code.

    The only problems are that you have to mark "Automatic Cost Posting" as False, and the second report prints out some output.
    OBJECT Codeunit 50047 Run Adjust Cost Reports
    {
      OBJECT-PROPERTIES
      {
        Date=03/28/12;
        Time=11:24:41 AM;
        Modified=Yes;
      }
      PROPERTIES
      {
        OnRun=BEGIN
                CostReports;
              END;
    
      }
      CODE
      {
    
        PROCEDURE CostReports@1240520003();
        VAR
          AdjustCostReport@1240520004 : Report 795;
          PostCostReport@1240520003 : Report 1002;
          InventorySetup@1240520005 : Record 313;
          TEXT01@1240520006 : TextConst 'ENU=Automatic Cost Posting must be FALSE to run this Codeunit';
        BEGIN
          InventorySetup.GET();
          IF InventorySetup."Automatic Cost Posting" = TRUE THEN
            ERROR(TEXT01);
    
          AdjustCostReport.InitializeRequest('','');
          AdjustCostReport.USEREQUESTFORM(FALSE);
          AdjustCostReport.RUNMODAL;
    
          PostCostReport.InitializeRequest(1, '', TRUE);
          PostCostReport.USEREQUESTFORM(FALSE);
          PostCostReport.RUNMODAL;
    
          MESSAGE('Cost Posting Finished');
        END;
    
      }
    }
    
    
    Joseph Dewey
    Microsoft Dynamics NAV User
Sign In or Register to comment.