Email Notifier

Mike_HWGMike_HWG Member Posts: 104
Hey all,

I thought I had a brilliant idea today, but I think I ran into a brick wall.

My idea was to have a report (or codeunit, doesn't matter) that you could specify a report to run. Once that report completed, my code would send me an email notification.

Unfortunately, NAV doesn't transfer control back to my code until after the called report is closed... is there a way around this?
Michael Hollinger
Systems Analyst
NAV 2009 R2 (6.00.34463)

Answers

  • AndwianAndwian Member Posts: 627
    Is it a process-only report or preview report?

    How about print the report to PDF, XLS, and then freely close the report?
    Regards,
    Andwian
  • Mike_HWGMike_HWG Member Posts: 104
    edited 2011-11-28
    My current solution works great for processing only reports and dataports.
    Though I haven't tried it, it should work fine with reports that you send to a printer.
    The only problem is reports that you wish to preview... but you gave me an idea. Users can simply print the report to a PDF printer! =D> Thank you for the idea!

    The goal of the code was for it to work with any report WITHOUT having to modify the target report... I think I've done it well enough that I can release it to my users.

    Here's my code, hope others may benefit as well. Please respond here with any improvements!

    WARNING: This functionality exposes ALL reports and dataports to the user, INCLUDING objects that are not visible to the user on the Navigation Pane! At this time, only Super Users should have access to this object!!
    OBJECT Report 50094 Email Notifier
    {
      OBJECT-PROPERTIES
      {
        Date=11/25/11;
        Time=[ 2:41:36 PM];
        Modified=Yes;
        Version List=HW322x;
      }
      PROPERTIES
      {
        ProcessingOnly=Yes;
        OnPreReport=BEGIN
                      IF Object.Name = '' THEN
                        ERROR(txtNoObject);
                    END;
    
        OnPostReport=BEGIN
                       Subject := STRSUBSTNO(txtComplete,Object.ID,Object.Name);
                       Body := STRSUBSTNO(txtBody,Subject,SenderAddress);
                       Subject := txtSubject + Subject;
                       SMTPMail.CreateMessage('',SenderAddress,Recipients,Subject,Body,FALSE);
    
                       SMTPMail.CreateMessage('',SenderAddress,Recipients,Subject,Body,FALSE);
    
                       CASE ObjectType OF
                         ObjectType::Report:
                           REPORT.RUNMODAL(Object.ID,TRUE);
                         ObjectType::Dataport:
                           DATAPORT.RUNMODAL(Object.ID,TRUE);
                       END;
    
                       SMTPMail.Send;
                     END;
    
      }
      DATAITEMS
      {
      }
      REQUESTFORM
      {
        PROPERTIES
        {
          Width=9130;
          Height=3080;
        }
        CONTROLS
        {
          { 1000000004;TextBox;3410 ;0    ;2310 ;440  ;InPage=-1;
                                                       CaptionML=ENU=Object Type;
                                                       SourceExpr=ObjectType;
                                                       OnAfterValidate=BEGIN
                                                                         CLEAR(Object);
                                                                       END;
                                                                        }
          { 1000000005;Label  ;0    ;0    ;3300 ;440  ;ParentControl=1000000004;
                                                       InPage=-1 }
          { 1000000006;TextBox;3410 ;550  ;5720 ;440  ;Name=ctrl_ObjectID;
                                                       InPage=-1;
                                                       CaptionML=ENU=Object;
                                                       SourceExpr=Object.Name;
                                                       OnLookup=BEGIN
                                                                  CASE ObjectType OF
                                                                    ObjectType::Report:
                                                                      Object.SETRANGE(Type,Object.Type::Report);
                                                                    ObjectType::Dataport:
                                                                      Object.SETRANGE(Type,Object.Type::Dataport);
                                                                  END;
    
                                                                  IF FORM.RUNMODAL(358,Object) = ACTION::LookupOK THEN
                                                                    Text := Object.Name
                                                                  ELSE
                                                                    EXIT(FALSE);
    
                                                                  EXIT(TRUE);
                                                                END;
                                                                 }
          { 1000000007;Label  ;0    ;550  ;3300 ;440  ;ParentControl=1000000006;
                                                       InPage=-1;
                                                       CaptionML=ENU=Object }
          { 1000000008;TextBox;3410 ;1100 ;5720 ;440  ;InPage=-1;
                                                       CaptionML=ENU=Sender Email;
                                                       SourceExpr=SenderAddress }
          { 1000000009;Label  ;0    ;1100 ;3300 ;440  ;ParentControl=1000000008;
                                                       InPage=-1 }
          { 1000000012;TextBox;3410 ;1650 ;5720 ;440  ;InPage=-1;
                                                       CaptionML=ENU=Recipient Email(s);
                                                       ToolTipML=ENU=Separate addresses with a comma;
                                                       SourceExpr=Recipients }
          { 1000000013;Label  ;0    ;1650 ;3300 ;440  ;ParentControl=1000000012;
                                                       InPage=-1;
                                                       ToolTipML=[ENU="Separate addresses with a semicolon (;)"] }
          { 1000000000;Label  ;0    ;2200 ;9130 ;880  ;InPage=-1;
                                                       MultiLine=Yes;
                                                       LeaderDots=No;
                                                       CaptionML=ENU=For printable reports, please redirect your output to a virtual PDF printer }
        }
      }
      CODE
      {
        VAR
          ObjectType@1000000000 : 'Report,Dataport';
          Object@1000000010 : Record 2000000001;
          SenderAddress@1000000003 : Text[100];
          Recipients@1000000004 : Text[1024];
          txtSubject@1000000008 : TextConst 'ENU="NAV Automated Notification: "';
          txtComplete@1000000005 : TextConst 'ENU=%1: %2 Processing is Complete';
          txtBody@1000000007 : TextConst 'ENU=This is an automated email reminding you that %1.  Thank you,%2';
          Subject@1000000001 : Text[200];
          Body@1000000002 : Text[1024];
          SMTPMail@1000000009 : Codeunit 400;
          txtNoObject@1000000006 : TextConst 'ENU=You must choose an object to run!';
    
        BEGIN
        {
          HW322, 11/23/11, MJH - Initial Creation.  Note that it only works for processingonly reports or for dataports.  For printable
                                 reports, please redirect to a virtual PDF printer.  Note that CASE statements were used if there is a future
                                 need for other object type handling.
        }
        END.
      }
    }
    
    
    
    Michael Hollinger
    Systems Analyst
    NAV 2009 R2 (6.00.34463)
  • AndwianAndwian Member Posts: 627
    Mike_HWG wrote:
    Here's my code, hope others may benefit as well.
    Thank you for sharing the code :D
    Andwian wrote:
    How about print the report to PDF, XLS, and then freely close the report?
    Actually, what I was trying to say was: since NAV could not transfer control back to my code until after the called report is closed, I think that is because the report is in the preview mode, and you should close the report before take the control back. Hence, the solution :idea: maybe: print it to PDF or somewhat, instead of preview it, and let's see if NAV could take the control back, because it did not preview at all, hence no preview window must be closed. :mrgreen:
    Regards,
    Andwian
  • Mike_HWGMike_HWG Member Posts: 104
    Andwian wrote:
    Andwian wrote:
    How about print the report to PDF, XLS, and then freely close the report?
    Actually, what I was trying to say was: since NAV could not transfer control back to my code until after the called report is closed, I think that is because the report is in the preview mode, and you should close the report before take the control back. Hence, the solution :idea: maybe: print it to PDF or somewhat, instead of preview it, and let's see if NAV could take the control back, because it did not preview at all, hence no preview window must be closed. :mrgreen:
    Yep, that's what I was agreeing with; and yes, it does work!
    Michael Hollinger
    Systems Analyst
    NAV 2009 R2 (6.00.34463)
  • Mike_HWGMike_HWG Member Posts: 104
    Just discovered one issue #-o

    WARNING: This functionality exposes ALL reports and dataports to the user, INCLUDING objects that are not visible to the user on the Navigation Pane! At this time, only Super Users should have access to this object!!

    I currently don't have an easy solution for this. :(
    Michael Hollinger
    Systems Analyst
    NAV 2009 R2 (6.00.34463)
Sign In or Register to comment.