Monday, December 19, 2011

HTML based notifications

PeopleSoft provides functions and procedures to send email notifications. Most of the times we send simple text messages and attach reports, generally in PDF format to these notifications. The following post, explores the possibility of generating a report via XMLP and then sending a notification to an email client like MS Outlook in HTML format. The distribution type of "email" that is natively available via XMLP just doesn't cut it. Its very tedious to change the subject/body of the notification and then the report is sent as an attachment and not the body the notification.

For this POC, I am using a HR 9.1 PeopleSoft system running on PT 8.51.10 and my email client is MS Outlook 2007. The exchange server is running MS Exchange 2003. I am executing the following code via an AE program. Some changes needed if using it via an online trigger like FieldChange.
I will be using the SendMail function instead of PeopleSoft MultiChannel Framework mail classes. To do the same via MCF mail classes is another post, some other day.

/* this section details the steps to create the report in HTM format and store it in a folder location */
SQLExec("select PRCSOUTPUTDIR from PSPRCSPARMS where PRCSINSTANCE = :1", N_RECORD_AET.PROCESS_INSTANCE, &dir);
&REPORTDEFN.OutDestination = &dir | "\"; /* This will create the output under the temp directory used by the process scheduler */
&REPORTDEFN.ProcessReport(&template, "", %Date, "HTM"); /* generating the report in HTM format */
&REPORTDEFN.Publish("", "", "", N_RECORD_AET.PROCESS_INSTANCE);

/* the following section, sets up the parameters of the SendMail function and sends the notification */
&EmailSQL = CreateSQL("select EMAIL_ADDR from PS_EMAIL_ADDRESSES where EMPLID = :1 and PREF_EMAIL_FLAG = 'Y'", &EmplID);
&EmailSQL.Fetch(&email_addr);
If All(&email_addr) Then
      &MAIL_FLAGS = 0;
      &MAIL_TO = &email_addr;
      &MAIL_CC = "";
      &MAIL_BCC = "";
      &MAIL_SUBJECT = "PeopleSoft Report";
/* here I am reading the htm report file in UTF-8 mode and then use that to populate the "body" variable */
      &fullfile = "";
      &reportfilepath = &dir | "\N_REPORT.htm";
      &html_file = GetFile(&reportfilepath, "R", "UTF-8", %FilePath_Absolute);
      While &html_file.ReadLine(&Text)
         &fullfile = &fullfile | &Text;
      End-While;
      &html_file.close();
      &MAIL_TEXT = &fullfile;

      /* this will takre care of only one image */
      &imgpath = &dir | "\*.png";
      &FileList = FindFiles(&imgpath, %FilePath_Absolute);
      &filepath = &FileList.Shift();
      &MAIL_FILES = &filepath;
     
      &pos = Find("xdo", &filepath);
      &MAIL_TITLES = Substring(&filepath, &pos, Len(&filepath) - &pos + 1);
     
      &CONTTYPE = "Content-type: text/html; charset=utf8";
      &MAIL_SENDER = "PeopleSoft@test.com";
     
      &RET = SendMail(&MAIL_FLAGS, &MAIL_TO, &MAIL_CC, &MAIL_BCC, &MAIL_SUBJECT, &MAIL_TEXT, &MAIL_FILES, &MAIL_TITLES, &MAIL_SENDER, ";", &CONTTYPE);
      If Not (&RET = 0) Then
         Warning ("Return status from mail = " | &RET);
      End-If;
Else
      Warning ("No Email Addresses found for " | &EmplID);
End-If;

Thats it. It produces a nice and chic looking HTML notification.