Friday, November 7, 2008

XMLP from Field Change and Email *.rtf

Here is a great sample that will invoke a XMLP and email the results to a email address within a page. We used this to send out Offer letters to faculty. My good colleague Roger and I worked on this one. Enjoy.

/*ASU_HRM0058 9/1/2008 rdavies3
Limited code to custom test page*/

/*** ASU_HRM0058 9/30/2008 J.McMahon ***/
/*** Added XMLP rowset logic, and email logic ***/


import PSXP_RPTDEFNMANAGER:*;
import PSXP_XMLGEN:*;
import PSXP_ENGINE:*;
import PT_MCF_MAIL:*;

Declare Function GetFilePathServer PeopleCode ASU_FILEPATH_WK.ECFILELISTPATH FieldFormula;


/*Create an email object by setting individual parameters*/
Local PT_MCF_MAIL:MCFOutboundEmail &eMail = create PT_MCF_MAIL:MCFOutboundEmail();
Local Rowset &ASU_NOA_Letter_Rowset, &ASU_NOA_Empl_Rowset;
Local Row &person_row;
Local Record &person_rec, &derived_pb;

/*** Fetch Value from page ***/
&Email_to_from_page = FetchValue(Record.ASU_NOA_EE_HIST, 1, ASU_NOA_EE_HIST.EMAILID);
&Fiscal_Year = FetchValue(Record.ASU_NOA_EE_HIST, 1, ASU_NOA_EE_HIST.FISCAL_YEAR);
&Emplid = FetchValue(Record.ASU_NOA_EE_HIST, 1, ASU_NOA_EE_HIST.EMPLID);
&Empl_Rcd = FetchValue(Record.ASU_NOA_EE_HIST, 1, ASU_NOA_EE_HIST.EMPL_RCD);


/*ASU_HRM0058 9/1/2008 rdavies3
NOA save and process code*/
&ASU_NOA_Empl_Rowset = GetLevel0()(1).GetRowset(Scroll.ASU_NOA_EE_HIST);
&derived_pb = GetRecord(Record.ASU_DERIVED_HR);
&person_row = &ASU_NOA_Empl_Rowset(1);
&person_rec = &person_row.GetRecord(Record.ASU_NOA_EE_HIST);
&ASU_NOA_Letter_Rowset = &person_row.GetRowset(Scroll.ASU_NOA_LTRHIST);
&person_rec.LASTUPDOPRID.Value = %UserId;
&person_rec.LAST_ACTIVITY_TIME.Value = %Datetime;

If &person_rec.EMPLID.Value = %EmployeeId Then
&person_rec.COMPLETED.Value = "Y";
&person_rec.APPROVER_OPRID.Value = %UserId;
&person_rec.APPROVEDTTM.Value = %Datetime;
&derived_pb.ASU_HR_PB_TEST_EMA.Label = "Reprint and Email";
DoSave();
CommitWork();
/* do reprint*/
Else
DoSave();
CommitWork();
End-If;



GetFilePathServer(&outFilePath, &inFilePath, %DbName);


&sRptDefn = "ASUHRNOA";
&sTemplateId = "ASUHRNOA_1";
&sLangCode = "";
&dtAsOfDate = %Date;
&sOutputFmt = "PDF";
&RptOutputDir = &outFilePath | "HR/";


/*Set-Up Report*/
&ReportDef = create PSXP_RPTDEFNMANAGER:ReportDefn(&sRptDefn);
&ReportDef.Get();
&ReportDef.SetRuntimeDataXMLFile(&sOutputFile);

&ReportDef.OutDestination = &RptOutputDir;

/*Create Rowset*/
&rs = &ASU_NOA_Empl_Rowset;

/*Fill Rowset*/
rem &rs.FILL("WHERE FILL.FISCAL_YEAR = :1 and FILL.EMPLID = :2 and FILL.EMPL_RCD = :3", &Fiscal_Year, &Emplid, &Empl_Rcd);

/*Create Schema*/
&rds = create PSXP_XMLGEN:RowSetDS(); /*package method*/
&mySchema = &rds.GetXSDSchema(&rs);
&f1 = GetFile(&RptOutputDir | "ASU_HR_NOA.xsd", "W", %FilePath_Absolute);
&f1.WriteLine(&mySchema);
&f1.Close();


/*Create Sample XML File*/
&myXMLFile = &rds.GetXMLData(&rs, &RptOutputDir | "ASU_HR_NOA.xsd");
&f2 = GetFile(&RptOutputDir | "ASU_HR_NOA.xml", "W", %FilePath_Absolute);
&f2.WriteLine(&myXMLFile);
&f2.Close();

/* output format */
&sOutputFormat = &sOutputFmt;

/*Provide a Data Source for the Report*/
&ReportDef.SetRuntimeDataRowset(&rs);


/*Generate the Report*/
&ReportDef.ProcessReport(&sTemplateId, %Language_User, %Date, &sOutputFormat);

/*Publish the Report*/
&ReportDef.Publish("", &RptOutputDir, "XMLP", 9999999);
rem &sFileExt = GetFileExtension(&sOutputFormat);


/*Send Mail*/
rem &ToList = "Jeromy.McMahon@asu.edu";
&ToList = &Email_to_from_page;
&FromList = "support@asu.edu";
&ReplyToList = "";
&Subject = "NOA (Notice of Appointment) Letter Attached";
&eMail.Recipients = &ToList; /*comma separeted list of email addresses*/
&eMail.From = &FromList; /*from email address*/
&eMail.ReplyTo = &ReplyToList; /*in case the reply is to be sent to a different email address*/
&eMail.Subject = &Subject;

/*Body for multiple parts*/
Local string &plain_text = "Attached you will find your copy of the Notice of Appointment letter.";
Local PT_MCF_MAIL:MCFBodyPart &text = create PT_MCF_MAIL:MCFBodyPart();
&text.Text = &plain_text;

Local PT_MCF_MAIL:MCFBodyPart &attach = create PT_MCF_MAIL:MCFBodyPart();

&attach.SetAttachmentContent(&RptOutputDir | "RptInst\" | "ASUHRNOA.pdf", %FilePath_Absolute, "ASUHRNOA.pdf", "ASUHRNOA", "", "");

Local PT_MCF_MAIL:MCFMultipart &mp = create PT_MCF_MAIL:MCFMultipart();
&mp.AddBodyPart(&text);
&mp.AddBodyPart(&attach);
&eMail.MultiPart = ∓

Local integer &resp = &eMail.Send();
/*now check &resp for the result*/
Local boolean &done;
Evaluate &resp
When %ObEmail_Delivered
/*every thing ok*/
&done = True;
Break;
When %ObEmail_NotDelivered
/*check &eMail.InvalidAddresses, &eMail.ValidSentAddresses and &eMail.ValidUnsentAddresses*/
&done = False;
Break;
When %ObEmail_PartiallyDelivered
/*check &eMail.InvalidAddresses, &eMail.ValidSentAddresses and &eMail.ValidUnsentAddresses*/
&done = True;
Break;
When %ObEmail_FailedBeforeSending
/*get the formatted messages from &eMail.ErrorDescription, &eMail.ErrorDetails*/
&done = False;
Break;
End-Evaluate;

CommitWork();