Final working code for generating a pdf with your own filename… and emailing it with Outlook
// You will need the following Custom Assembly References
// Erp.Contracts.Rpt.SalesOrderAck
// Erp.UIRpt.SalesOrderAck
// Ice.Contracts.BO.ReportMonitor
// Microsoft.Office.Inerop.Outlook <<<<this can be tricky to locate, when you find it, deploy it you epicor client folder, I found it here:- C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Outlook
// Microsft.ReportViewer.Common
// Microsft.ReportViewer.WebForms
// Microsft.ReportViewer.WinForms
//Add these using's
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution;
using System.Windows.Forms;
using System.IO;
//Add this to the script
public class Script
{
private Erp.UI.Controls.Combos.cboCustCnt soldToName;
private Erp.UI.Controls.Combos.cboCustCnt shipToName;
}
//Add this to InitializeCustomCode
public void InitializeCustomCode()
{
soldToName = (Erp.UI.Controls.Combos.cboCustCnt)csm.GetNativeControlReference("0bd70f62-56fa-4a05-b15f-ea3cbdd18f43");
shipToName = (Erp.UI.Controls.Combos.cboCustCnt)csm.GetNativeControlReference("50a5e4e4-46cd-4397-a600-5fb8ac57e16f");
}
//Add this to DestroyCustomCode
public void DestroyCustomCode()
{
soldToName = null;
shipToName = null;
}
////Prepare SOA/////////////////////////////////////////////\\\\\\\\\\\//////
private void prepareSOAPDF()
{
EpiDataView orderDtl = ((EpiDataView)(this.oTrans.EpiDataViews["OrderDtl"]));
int lineCount = orderDtl.dataView.Count;
EpiDataView edvOrderHed1 = ((EpiDataView)(this.oTrans.EpiDataViews["OrderHed"]));
DataRow editRow = edvOrderHed1.CurrentDataRow;
int soNum = Convert.ToInt32(editRow["OrderNum"]);
//Get the customers info - code could be cleaned up
String name1 = "";
String name2 = "";
String toName = "";
String email1 = "";
if(editRow["SoldToContactEMailAddress"].ToString() != "")
{
email1 = editRow["SoldToContactEMailAddress"].ToString() + "; ";
}
String email2 = "";
if(editRow["ShipToContactEMailAddress"].ToString() != editRow["SoldToContactEMailAddress"].ToString())
{
if(editRow["ShipToContactEMailAddress"].ToString() != "")
{
email1 = editRow["ShipToContactEMailAddress"].ToString() + "; ";
}
}
if(Convert.ToInt32(soldToName.Value.ToString()) >0)
{
name1 = soldToName.Text.ToString().Split(' ')[0];
}
if(Convert.ToInt32(shipToName.Value.ToString()) >0)
{
name2 = shipToName.Text.ToString().Split(' ')[0];
}
if(name1 == name2)
{
toName = name1;
}
else if(name2 != "" && name1 != "" )
{
toName = name1 + " and " + name2;
}
else if (name1 == "")
{
toName = name2;
}
salesOrderAckAdapter soa = new salesOrderAckAdapter(oTrans);
soa.BOConnect();
soa.GetNewParameters();
soa.ReportData.SalesOrderAckParam[0].OrderNum = soNum;
soa.ReportData.SalesOrderAckParam[0].AgentID = "SystemTaskAgent";
soa.ReportData.SalesOrderAckParam[0].ReportStyleNum = 1001; //Change to suit your requirements
Guid workstationid = Guid.NewGuid();
soa.ReportData.SalesOrderAckParam[0].WorkstationID = String.Format("{0}",workstationid);
soa.ReportData.SalesOrderAckParam[0].ArchiveCode = 1;
soa.ReportData.SalesOrderAckParam[0].AutoAction = "SSRSGenerate"; //generate
soa.SubmitToAgent("SystemTaskAgent",0,0);
MessageBox.Show("Print Job Has been submitted - This will take approx. 10 seconds.");
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(1000 * lineCount);
timer = timer + 1;
if (timer > 100 * lineCount)
{
MessageBox.Show("Attempts to generate a SOA 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\PrintReports\Sales_Order_Acknowledgement_{0}.pdf", soNum); // use this path to save to SERVER
string FileName = String.Format(@"C:\EpicorData\PrintReports\Sales_Order_Acknowledgement_{0}.pdf", soNum); // use this path to save to LOCAL
GenerateReportPDF(tableguid,FileName);
//The Email part - Comment out below to not do the email part
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 = "TWG Sales Order Acknowledgement " + soNum ;
oMsg.HTMLBody = "<p>Hello " + toName + "<br><br>Please see attached Sales Order Acknowledgement for your approval<br><br>Please check that the Bill To and Ship To details are correct</p>" + Signature;
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(email1 + email2);
oRecip.Resolve();
oMsg.Attachments.Add(FileName,Outlook.OlAttachmentType.olByValue, Type.Missing,Type.Missing);
MsgInspector.Activate(); // shows the new email message as topmost window.
}
catch (Exception Ex)
{
MessageBox.Show("SOA Email was attempted but failed. Please try again. If that doesn't work, please contact ERP Support");
}
//End of Email part
}
private void GenerateReportPDF(String tableguid, String PDFfilename)
{
// Create a Report Execution object
Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.ReportExecutionService rsExec = new ReportExecutionService();
rsExec.Url = "http://SERVER:80/ReportServer/ReportExecution2005.asmx"; //ServerURL Change to suit
rsExec.PreAuthenticate = true;
rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Render arguments
byte[] result = null;
string report = "/reports/CustomReports/SalesOrderAcknowledgement/SOForm_LIVE"; // Change to suit
string format = "PDF";
string historyId = null;
string deviceInfo = null;
// Prepare report parametes
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = "TableGuid";
parameters[0].Value = tableguid;
string extension;
string mimeType;
string encoding;
Warning[] warnings = null;
string[] streamIDs = null;
// Load the report
rsExec.LoadReport(report, historyId);
// pass parameters
rsExec.SetExecutionParameters(parameters, null);
// get pdf of report
result = rsExec.Render(format, deviceInfo, out extension, out mimeType, out encoding, out warnings, out streamIDs);
File.WriteAllBytes(PDFfilename, result);
}
////END Prepare SOA/////////////////////\\\\\\\\\\\\\\\\////\\\\\\\\//////