Emailing Invoice Sometimes Adds More Than One Invoice PDF Attachment to an Email

Hello,

Hello,

I am looking for some help with a customization (ARInvoiceForm) that has to be created for in regards to email invoice customers.

We have a customer that will accept one pdf per email sent. Now our customization seems to add multiple PDF invoices to the email and then it’s being sent to this customer.

Is it possible to get this code to email one attachment at a time? If so, how do I tell the code to do this?

// ** Wizard Insert Location - Do not delete ‘Begin/End Wizard Added Variable Initialization’ lines **
// Begin Wizard Added Variable Initialization

	edvInvoiceGrp = ((EpiDataView)(oTrans.EpiDataViews["InvcGrp"]));
	edvHeadList = ((EpiDataView)(oTrans.EpiDataViews["InvcHeadList"]));

	// End Wizard Added Variable Initialization

	// Begin Wizard Added Custom Method Calls
	
	this.btnPrintAutoEmailInvoice.Click += new System.EventHandler(this.btnPrintAutoEmailInvoice_Click);
	epiSession = (Epicor.Mfg.Core.Session)ARInvoiceForm.Session;

	this.btnPostInvGrp.Click += new System.EventHandler(this.btnPostInvGrp_Click);
	
	DynamicQuery _dq = new DynamicQuery(((Session)oTrans.Session).ConnectionPool);
	DataSet ds = _dq.ExecuteByID("MSIUK-baq-btnAutoEmailFinance");
	bool found =false;
	foreach(DataRow dr in ds.Tables[0].Rows)
	{
		if(dr["UserFile.DcdUserID"].ToString().ToUpper().Equals(((Session)oTrans.Session).UserID.ToUpper()))
			{
				found = true;
				
			}
	}
	btnPrintAutoEmailInvoice.Enabled = found;
	btnPostInvGrp.Enabled = found;
	// End Wizard Added Custom Method Calls
}

public void DestroyCustomCode()
{
	// ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
	// Begin Wizard Added Object Disposal


	this.btnPrintAutoEmailInvoice.Click -= new System.EventHandler(this.btnPrintAutoEmailInvoice_Click);
	edvInvoiceGrp.Dispose();
	edvHeadList.Dispose();
	edvInvoiceGrp=null;
	edvHeadList = null;
	this.btnPostInvGrp.Click -= new System.EventHandler(this.btnPostInvGrp_Click);
	// End Wizard Added Object Disposal

	// Begin Custom Code Disposal

	// End Custom Code Disposal
}

private void btnPrintAutoEmailInvoice_Click(object sender, System.EventArgs args)
{
	if(txtCustID.Text.Length>0)
	{

	string LockStatus = edvInvoiceGrp.dataView[edvInvoiceGrp.Row]["LockStatus"].ToString(); // only invoice group with lock status having "Not Posted can be processesd. 
	       																				// other status such as "Reviewing" cannot processed.

	

		if(LockStatus == "Not Posted")
		{
		
		// ** Place Event Handling Code Here **
		 try
          {
				 Cursor.Current = Cursors.WaitCursor;
				btnPrintAutoEmailInvoice.Text = "Processing..";
				btnPrintAutoEmailInvoice.Enabled=false;
				subLoopAllInvoiceAndEmail();
				Cursor.Current = Cursors.Default;
				
	
          }	
          catch(System.Exception ex)
		  {
				Cursor.Current = Cursors.Default;
				ExceptionBox.Show(ex,"btnPrintAutoEmailInvoice");
		  }
	
		}
		else
		{
	
		MessageBox.Show(String.Format("Cannot Process! Invoice Group Status = {0}",LockStatus),"Invoice Group Posting Exception");
		}
			
	}

	else
	{
	 MessageBox.Show("Cannot Process! Invoice Group Null!","Invoice Group Posting Exception");
	}
}

private void subLoopAllInvoiceAndEmail()
{
	try
	{  
		
		
	// EpiDataView edvInvoiceGrp = ((EpiDataView)(oTrans.EpiDataViews["InvcGrp"])); // invoice grp detail

	
		string custID;
		string InvoiceNum;
		string CustInvoiceEmailId;
		
		if(edvHeadList.dataView.Count>0) // check any invoice avilable in the invoice group
		{
			for(int RowCount = 0; RowCount < edvHeadList.dataView.Count;RowCount++) // loop through each invoice 
			{
				
				// custID = edvHeadList.dataView[RowCount]["CustomerCustID"].ToString(); //get customer ID of each invoice

				//CallCustomerAdapterGetDataMethod(custID); //validate email id for customer  // <---- VALIDATION DONE IN REPORT. DOING VALIDATION CLIENT SIDE, SLOW THE SYSTEM

				InvoiceNum = edvHeadList.dataView[RowCount]["InvoiceNum"].ToString(); //get invoice number of each invoice
				subEmailInvoice(Convert.ToInt32(InvoiceNum)); //send Invoice Email
		    	PauseForMilliSeconds(3000); 


			}
			
			btnPrintAutoEmailInvoice.Text = "EMail";
			btnPrintAutoEmailInvoice.Enabled=true;
			EpiMessageBox.Show("Invoice E-Mail, sent to all Customers" + "\n" + "Please your check your Outlook OutBox/Sent Items!","AutoEmail");
			

		}
		else 
		{
		
		MessageBox.Show("No Invoices in the Invoice Group","Invoice Group Posting Exception");	
		btnPrintAutoEmailInvoice.Text = "Print";
		btnPrintAutoEmailInvoice.Enabled=true;
		}
		
	} 
	catch (System.Exception ex)
	{
		ExceptionBox.Show(ex);
	}
}



private void CallCustomerAdapterGetDataMethod(string id) // check invoice email id avilable for the customer. if not set angie email id.
{
	try
	{
		// Declare and Initialize EpiDataView Variables
		// Declare and create an instance of the Adapter.
		CustomerAdapter adapterCustomer = new CustomerAdapter(this.oTrans);
		adapterCustomer.BOConnect();
		adapterCustomer.clearData();

		// Declare and Initialize Variables

		// Call Adapter method
		System.Data.DataSet dsCustomer = adapterCustomer.GetData(id);
		
	    EpiDataView edvCust = new EpiDataView();
        edvCust.dataView = new DataView(adapterCustomer.CustomerData.Customer);
		string Email = edvCust.dataView[edvCust.Row]["ShortChar02"].ToString(); // customer invoiceEmail Id (i.e. Customer.Shortchar02)
	       if (Email.Length <= 0)
            { 

            edvCust.dataView[edvCust.Row].BeginEdit() ;
            edvCust.dataView[edvCust.Row]["ShortChar02"] = "Angie.Dean@msi-forks.com";
            edvCust.dataView[edvCust.Row].EndEdit(); 
			adapterCustomer.Update();
						
            }
			else
			{
				//MessageBox.Show( edvCust.dataView[edvCust.Row]["ShortChar02"].ToString());
			}
		
		
		// Cleanup Adapter Reference

		dsCustomer.Dispose();
		dsCustomer = null;
		edvCust.Dispose();
		edvCust=null;
		adapterCustomer.Dispose();

	} 
	catch (System.Exception ex)
	{
		ExceptionBox.Show(ex, "Customer Get Data ");
	}
}

public void subEmailInvoice(int invoiceNum) // get invoice number and send email using edocprinter
{
Epicor.Mfg.Rpt.ARInvForm ARInv = new Epicor.Mfg.Rpt.ARInvForm(epiSession.ConnectionPool);
Epicor.Mfg.Rpt.ARInvFormDataSet ARInvDs = ARInv.GetNewParameters();

     try
      {
					
            if (invoiceNum > 0) 
           	{
               ARInvDs.Tables["ARInvFormParam"].Rows[0]["InvoiceNum"] = invoiceNum; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["Vouchering"] ="False"; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["PrintNPost"] = "False"; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["AssignLegalNumber"] = "False"; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["DocTypeOption"] = "All"; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["EnableAssignLegalNumber"] = "False"; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["PrintStatusOption"] = "All"; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["TransOptions"] = "ARC"; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["TH-IsService"] ="False"; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["AutoAction"] = "Print"; //use "Preview" to preview the report
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["PrinterName"] = "eDocPrinter PDF Pro";
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["AgentSchedNum"] = 0;
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["AgentID"] = "SystemTaskAgent";
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["AgentTaskNum"] = 0; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["RecurringTask"] = "False"; 
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["RptPageSettings"] = "Color=True,Landscape=False,Margins=[Left=100 Right=100 Top=100 Bottom=100],PaperSize=[Kind=\"A4\" Height=1169 Width=827],PaperSource=[SourceName=\"Auto\" Kind=\"AutomaticFeed\"],PrinterResolution=[X=600 Y=600]";
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["RptPrinterSettings"] = "PrinterName=\"eDocPrinter PDF Pro\",Copies=1,Collate=False,Duplex=Simplex,Frompage=0,ToPage=0";
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["ReportStyleNum"] = 1012;
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["WorkstationID"] = Environment.MachineName + " 1";
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["DateFormat"] = "dd/mm/yyyy";
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["NumericFormat"] = ",.";
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["ProcessTaskNum"] = "0";
			   ARInvDs.Tables["ARInvFormParam"].Rows[0]["ReportCultureCode"] = "en-GB";
               ARInvDs.Tables["ARInvFormParam"].Rows[0]["ReportCurrencyCode"] = "GBP";
               ARInvDs.Tables["ARInvFormParam"].Rows[0]["ArchiveCode"] = "0";
               ARInvDs.Tables["ARInvFormParam"].Rows[0]["DecimalsGeneral"] = "2";
               ARInvDs.Tables["ARInvFormParam"].Rows[0]["DecimalsCost"] = "5";
               ARInvDs.Tables["ARInvFormParam"].Rows[0]["DecimalsPrice"] = "5";
               ARInvDs.Tables["ARInvFormParam"].Rows[0]["GlbDecimalsGeneral"] = "2";
               ARInvDs.Tables["ARInvFormParam"].Rows[0]["GlbDecimalsCost"] = "5";
               ARInvDs.Tables["ARInvFormParam"].Rows[0]["GlbDecimalsPrice"] = "5";
               ARInv.SubmitToAgent(ARInvDs, "SystemTaskAgent", 0, 0, "Epicor.Mfg.UIRpt.ARInvForm");
		
	         ARInvDs.Dispose();
	         ARInv = null;

found the problem - document sender setting need changing,.