We have a customisation on PO entry that creates and an email, inserts the suppliers email address, attaches the PO report, attaches our terms and conditions, and fills in default text in the email and leaves the email open for the user to edit as required before hitting send. It was done a long time ago so it may not be the best code… I also had some permissions issues to sort out before this worked. It is triggered from a button on the PO entry screen.
Here is the code. Note the additional dll’s that need to be distributed to the clients.
private void epiButtonC1_Click(object sender, System.EventArgs args)
{
// ** Place Event Handling Code Here **
// This customisation requires 2 .dll files for the email automation to work. These files must exsist in the client
// folder on the client machine before the customistaion can load.
// The files are:
// - Microsoft.Office.Interop.Outlook.dll
// - Microsoft.ReportViewer.WebForms.dll
EpiDataView edvPOHeader = (EpiDataView)oTrans.EpiDataViews["POHeader"];
int PONUM;
string Approved;
string Requestor;
string Supplier;
string CurrentCompany;
// check if there is data loaded into the form. if not return from method.
if ( (bool)edvPOHeader.HasRow == false )
{
MessageBox.Show("There is no data present to email");
return;
}
// Get data from the form
Approved = (string)edvPOHeader.dataView[edvPOHeader.Row]["ApprovalStatus"];
PONUM = (int)edvPOHeader.dataView[edvPOHeader.Row]["PONum"];
Requestor = (string)edvPOHeader.dataView[edvPOHeader.Row]["Character01"];
Supplier = (string)edvPOHeader.dataView[edvPOHeader.Row]["VendorVendorID"];
CurrentCompany = (string)edvPOHeader.dataView[edvPOHeader.Row]["Company"];
if (Approved == "A")
{
// check the supplier has an email address in Purchase Point.
// grab the email address textbox (this textbox is a customisation) so we can check and get the address if it is there.....
string PPemailAddress;
PPemailAddress = epiTextBoxC3.Text; //Supplier email address is in this Textbox.
if (PPemailAddress == "")
{
MessageBox.Show("No Email address. Add address to Supplier Purchase Point");
return; // bailout - no email address...
}
Erp.Proxy.Rpt.POFormImpl Form = WCFServiceSupport.CreateImpl<Erp.Proxy.Rpt.POFormImpl>((Ice.Core.Session)oTrans.Session, Epicor.ServiceModel.Channels.ImplBase<Erp.Contracts.POFormSvcContract>.UriPath);
// Make the report from the PO data
Erp.Rpt.POFormDataSet POFormDS;
POFormDS = Form.GetNewParameters();
// set the PO number and report style and submit the form to the system agent.
POFormDS.POFormParam[0].PONum = PONUM;
switch (CurrentCompany)
{
case "A":
POFormDS.POFormParam[0].ReportStyleNum = 1002;
break;
case "B":
POFormDS.POFormParam[0].ReportStyleNum = 1003;
break;
default:
POFormDS.POFormParam[0].ReportStyleNum = 1002;
break;
}
Guid workstationid = Guid.NewGuid(); // Put a GUID in the workstationId to allow searching for the report later.
POFormDS.POFormParam[0].WorkstationID = String.Format("{0}",workstationid); // Using a GUID as the WorkstationID so it is easy to reteive this report
POFormDS.POFormParam[0].DateFormat = "dd/MM/yy";
POFormDS.POFormParam[0].ArchiveCode = 1;
POFormDS.POFormParam[0].AutoAction = "SSRSGenerate";
//MessageBox.Show("Submit to System Agent ...");
Form.SubmitToAgent(POFormDS, "SystemAgent", 0, 0, "Erp.UIRtp.POForm"); //Submit the report to be generate by the system agent.
// get report data. Make a ReportMonitor, use it to get a reportmonitor dataset. The reportmonitor dataset will have the sysrowID of the report
// we want, we can then use the sysrowID to get the report data to put into the SSRS report.
Ice.Proxy.BO.ReportMonitorImpl RM = WCFServiceSupport.CreateImpl<Ice.Proxy.BO.ReportMonitorImpl>((Ice.Core.Session)oTrans.Session, Epicor.ServiceModel.Channels.ImplBase<Ice.Contracts.ReportMonitorSvcContract>.UriPath);
int timer=0;
bool morepages;
SysRptLstListDataSet RptList;
RptList = RM.GetList(@"WorkStationID ='"+workstationid+"'", 0, 0, out morepages);
while (RptList.SysRptLstList.Count == 0 ) // setup a loop to look for when the report has been generated.
{
System.Threading.Thread.Sleep(500);
timer = timer + 1;
if (timer > 120)
{
MessageBox.Show("Attempts to generate a PO pdf has timed-out. Please try again, and if that does not work, contact ERP Support");
return;
}
RptList = RM.GetList(@"WorkStationID ='"+workstationid+"'", 0, 0, out morepages);
}
string tableguid;
tableguid = RptList.SysRptLstList[0].FileName.Replace("REPORT DATABASE: ","");
// make a pdf of the PO from the report
string FileName = String.Format(@"\\<SERVER>\EpicorData\PO Reports\{0}.pdf", PONUM);
string reportStyle;
switch (CurrentCompany)
{
case "A":
reportStyle = "/Reports/CustomReports/POForm - With Line Notes";
break;
case "B":
reportStyle = "/Reports/CustomReports/POFormB - With Line Notes";
break;
default:
reportStyle = "/Reports/CustomReports/POForm - With Line Notes";
break;
}
GenerateReportPDF("http://<SSRS_SERVER>/reportserver_Epicor/ReportExecution2005.asmx",reportStyle,tableguid,FileName);
// create email message
try
{
//MessageBox.Show("Attaching to email...");
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector MsgInspector = oMsg.GetInspector;
string Signature = oMsg.HTMLBody; //capture signature for appending to custom message later.
oMsg.Subject = PONUM.ToString() + " Purchase Order" ;
switch (CurrentCompany)
{
case "A":
oMsg.HTMLBody = "Put your default text here in html format" + Signature;
break;
case "B":
oMsg.HTMLBody = "Put your default text here in html format" + Signature;
break;
default:
oMsg.HTMLBody = "Put your default text here in html format" + Signature;
break;
}
// Set up the recipients for the email
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(PPemailAddress);
oRecip.Resolve();
// Find the requestors email and add that
if (Requestor != "")
{
Ice.Proxy.BO.UserFileImpl userBO = WCFServiceSupport.CreateImpl<Ice.Proxy.BO.UserFileImpl>((Ice.Core.Session)oTrans.Session, Epicor.ServiceModel.Channels.ImplBase<Ice.Contracts.UserFileSvcContract>.UriPath);
try
{
UserFileDataSet user = userBO.GetByID(Requestor);
oRecip = oRecips.Add(user.UserFile[0].EMailAddress);
oRecip.Resolve();
}
catch
{
MessageBox.Show("Could not add requestors email address to email, user " + Requestor + " not found in Epicor.");
}
}
// Attach pdf of PO, terms, and any other attachments to PO.
oMsg.Attachments.Add(FileName,Outlook.OlAttachmentType.olByValue, Type.Missing,Type.Missing);
oMsg.Attachments.Add(@"\\<SERVER>\Shared\Purchasing\Purchase Order Terms and Conditions.pdf",Outlook.OlAttachmentType.olByValue, Type.Missing,Type.Missing);
var FolderId = Guid.NewGuid().ToString("N").ToUpper();
var TempPath = Path.Combine(Path.GetTempPath(), FolderId);
if (!Directory.Exists(TempPath))
{
Directory.CreateDirectory(TempPath);
}
foreach (DataRowView row in edvAutoAttachPOHeader.dataView)
{
var AttachPath = row["FileName"].ToString();
var AttachFileName = Path.GetFileName(AttachPath);
var AttachNewPath = Path.Combine(TempPath, AttachFileName);
var AttachDesc = row["DrawDesc"].ToString();
File.Copy(AttachPath, AttachNewPath);
if (File.Exists(AttachNewPath))
{
oMsg.Attachments.Add(AttachNewPath, Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
}
}
MsgInspector.Activate(); // shows the new email message as topmost window.
//Clean up
oRecips = null;
oRecip = null;
oMsg = null;
oApp = null;
}
catch (Exception Ex)
{
MessageBox.Show("PO Email was attempted but failed. Please try again. If that doesn't work, please contact ERP Support");
}
}
else
MessageBox.Show("PO must be approved before it can be sent out");
}
private void GenerateReportPDF(String ServerURL, String report, String tableguid, String PDFfilename)
{
// setup report variables
string deviceInfo = null;
string extension = String.Empty;
string mimeType = String.Empty;
string encoding = String.Empty;
Warning[] warnings = null;
string[] streamIDs = null;
string historyId = null;
// Create a Report Execution object
Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.ReportExecutionService rsExec = new ReportExecutionService();
rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
rsExec.Url = ServerURL;
// Load the report
rsExec.LoadReport(report, historyId);
// pass parameters
rsExec.SetExecutionParameters(
new ParameterValue[] { new ParameterValue() { Name = "TableGuid", Value = tableguid } }, null);
// get pdf of report
Byte[] results = rsExec.Render("PDF", deviceInfo,
out extension, out encoding,
out mimeType, out warnings, out streamIDs);
File.WriteAllBytes(PDFfilename, results);
}
Hope this is useful.
Brett