Adding email attachment from Documents  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Adding email attachment from Documents

Postby DannyC » Tue Apr 30, 2019 1:25 pm

Specifically talking about the Purchase Order email here but I guess can be equally applied to Sales Order emails.

Looking at the standard plugin Purchase Order Custom Email, I want to tweak it to attach anything which has been added to the Documents tab.
I am assuming I would need to attach it prior to the line
Code: Select all
PurchaseOrderForm.POEmail(reportDefinition.Report,  MyEmailOptionsForm.EmailFrom, MyEmailOptionsForm.EmailTo, MyEmailOptionsForm.Subject, MyEmailOptionsForm.CC, MyEmailOptionsForm.BCC, MyEmailOptionsForm.Message, If(MyEmailOptionsForm.NameToGiveAttachment.Trim.Length = 0, PurchaseOrderForm.PurchaseOrder.OrderNo, MyEmailOptionsForm.NameToGiveAttachment), lEM_Main_RecID, MyEmailOptionsForm.RequestReadReceipt, MyEmailOptionsForm.ExportReportFormat)

but I can't see any property or method used to add an additional attachment other than the actual purchase order.

What would be the line(s) used to attach something else?
Seeing as the original plugin is in VB, I guess that language is preferable but I don't mind if it's in C#.

This client is on version 7.0.175.
User avatar
DannyC
Senpai
Senpai
 
Posts: 636
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 30

Re: Adding email attachment from Documents

Postby DannyC » Thu May 09, 2019 4:07 pm

Gentle bump
User avatar
DannyC
Senpai
Senpai
 
Posts: 636
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 30

Re: Adding email attachment from Documents  Topic is solved

Postby Mike.Sheen » Thu May 09, 2019 5:22 pm

The POEmail method of the Purchase Order form can only deal with a single report attachment.

The method looks like this:

Code: Select all
Public Sub POEmail(ByVal Report As JiwaApplication.Report.Configuration.Report, ByVal From As String, ByVal EmailTo As String, ByVal Subject As String, ByVal CC As String, ByVal BCC As String, ByVal Message As String, ByVal NameToGiveAttachment As String, ByRef EM_Main_RecID As String, Optional ByRef RequestReadReceipt As Boolean = False, Optional ByRef ExportReportFormat As Integer = 31)
        If PurchaseOrder.AbstractPermissionCollection("Email Order").AccessLevel <> JiwaApplication.Security.UserGroup.AccessLevels.Allow Then
            Throw New JiwaApplication.Exceptions.PermissionDeniedException("You do not have sufficient priveleges to email a purchase order.")
        End If
        Dim SelectionFormula As String = "{PO_Main.OrderID}=" & Manager.Database.FormatChar(_PurchaseOrder.OrderID)
        Manager.EmailReportDirect(Report, From, EmailTo, Subject, CC, BCC, Message, NameToGiveAttachment, EM_Main_RecID, SelectionFormula, RequestReadReceipt, _PurchaseOrder.OrderID, "Purchase Order", _PurchaseOrder.OrderNo, JiwaSearch.clsSearch.SearchModes.jswPurchaseOrder, ExportReportFormat, "JiwaFinancials.Jiwa.JiwaPurchaseOrdersUI.PurchaseOrders")
    End Sub


So what you want to do is not call the PurchaseOrderForm.POEmail as you are, but create your own method which looks like the above but doesn't call Manager.EmailReportDirect - but instead your own method based on the code we have in EmailReportDirect:

Code: Select all
Public Sub EmailReportDirect(ByVal Report As JiwaApplication.Report.Configuration.Report, ByVal From As String, ByVal EmailTo As String, ByVal Subject As String, ByVal CC As String, ByVal BCC As String, ByVal Message As String, ByVal NameToGiveAttachment As String, ByRef EM_Main_RecID As String, Optional ByVal RecordSelectionFormula As String = "", Optional ByVal RequestReadReceipt As Boolean = False, Optional ByVal SourceID As String = "", Optional ByVal SourceType As String = "", Optional ByVal SourceDisplayNo As String = "", Optional ByVal SourceDrillDownIndex As JiwaSearch.clsSearch.SearchModes = JiwaSearch.clsSearch.SearchModes.jswUnknown, Optional ByVal ExportFormatType As CrystalDecisions.Shared.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Optional ByVal SY_Forms_ClassName As String = "")
        EM_Main_RecID = ""

        PrintReportObject.LoadReport(Report)
        PrintReportObject.OverrideRecordSelectionCriteria = RecordSelectionFormula

        If _ReportSelectionFormulaTokens.GetUpperBound(0) > 0 Then
            For MyLoop As Integer = 1 To _ReportSelectionFormulaTokens.GetUpperBound(0)
                For Each m_JiwaReportRange As JiwaPrintReport.JiwaReportRange In PrintReportObject.JiwaReportRangeCollection
                    If PrintReportObject.JiwaPrintFormulaCollection(m_JiwaReportRange.FormulaKey).Name.Replace("{", "").Replace("}", "").Replace("@", "").Trim().ToUpper() = _ReportSelectionFormulaTokens(MyLoop).TokenName.Replace("{", "").Replace("}", "").Replace("@", "").Trim().ToUpper() Then
                        m_JiwaReportRange.Value = CStr(_ReportSelectionFormulaTokens(MyLoop).TokenValue)
                        Exit For
                    End If
                Next m_JiwaReportRange
            Next MyLoop
        End If

        PrintReportObject.UpdateReport()
        PrintReportObject.EmailReport(From, EmailTo, Subject, CC, BCC, Message, NameToGiveAttachment, EM_Main_RecID, RequestReadReceipt, SourceID, SourceType, SourceDisplayNo, SY_Forms_ClassName, ExportFormatType)
    End Sub


And in that you don't want to call PrintReportObject.EmailReport as we are - but your own method based on the code we have in EmailReport:

Code: Select all
 Public Sub EmailReport(ByVal From As String, ByVal EmailTo As String, ByVal Subject As String, ByVal CC As String, ByVal BCC As String, ByVal Body As String, ByVal NameToGiveAttachment As String, ByRef EM_Main_RecID As Object, Optional ByVal RequestReadReceipt As Boolean = False, Optional ByVal SourceID As String = "", Optional ByVal SourceType As String = "", Optional ByVal SourceDisplayNo As String = "", Optional ByVal SourceForm As String = Nothing, Optional ByVal ExportFormatType As CrystalDecisions.Shared.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
            Dim exOption As CrystalDecisions.Shared.ExportOptions
            Dim lTempFolderPath As String = ""
            Dim EmailObject As JiwaApplication.JiwaEmail.EmailMessage = Nothing
            Dim lAttachmentKey As String = ""

            UpdateReport()
            exOption = _CrystalReportObject.ExportOptions


            exOption.ExportFormatType = ExportFormatType
            exOption.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile
            lTempFolderPath = My.Computer.FileSystem.SpecialDirectories.Temp
            If lTempFolderPath.Trim.Length = 0 Then
                Throw New Exception("Unable to get temporary folder")
            End If

            'Make filename safe
            For Each c As Char In System.IO.Path.GetInvalidFileNameChars
                NameToGiveAttachment = NameToGiveAttachment.Replace(c, "")
            Next

            If NameToGiveAttachment.Trim.Length = 0 Then
                Throw New Exception("Attachment name is invalid.")
            End If

            Select Case ExportFormatType
                Case CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
                    If Not NameToGiveAttachment.EndsWith(".pdf") Then
                        NameToGiveAttachment = NameToGiveAttachment & ".pdf"
                    End If

                Case CrystalDecisions.Shared.ExportFormatType.CrystalReport
                    If Not NameToGiveAttachment.EndsWith(".rpt") Then
                        NameToGiveAttachment = NameToGiveAttachment & ".rpt"
                    End If

                Case CrystalDecisions.Shared.ExportFormatType.HTML32, CrystalDecisions.Shared.ExportFormatType.HTML40
                    If Not NameToGiveAttachment.EndsWith(".html") Then
                        NameToGiveAttachment = NameToGiveAttachment & ".html"
                    End If

                Case CrystalDecisions.Shared.ExportFormatType.Excel, CrystalDecisions.Shared.ExportFormatType.ExcelRecord
                    If Strings.Right(NameToGiveAttachment, 5) <> ".xls" Then
                        NameToGiveAttachment = NameToGiveAttachment & ".xls"
                    End If

                Case CrystalDecisions.Shared.ExportFormatType.WordForWindows, CrystalDecisions.Shared.ExportFormatType.RichText
                    If Strings.Right(NameToGiveAttachment, 5) <> ".rtf" Then
                        NameToGiveAttachment = NameToGiveAttachment & ".rtf"
                    End If
            End Select

            _CrystalReportObject.ExportToDisk(ExportFormatType, My.Computer.FileSystem.CombinePath(lTempFolderPath, NameToGiveAttachment))

            EmailObject = Manager.BusinessLogicFactory.CreateBusinessLogic(Of JiwaApplication.JiwaEmail.EmailMessage)(Nothing)
            EmailObject.CreateNew()

            EmailObject.EmailFrom = From
            EmailObject.EmailTo = EmailTo
            EmailObject.EmailCC = CC
            EmailObject.EmailBCC = BCC
            EmailObject.EmailSubject = Subject
            EmailObject.EmailBody = Body
            EmailObject.SourceID = SourceID
            EmailObject.SourceType = SourceType
            EmailObject.SourceDisplayNo = SourceDisplayNo
            EmailObject.SourceForm = SourceForm

            Dim attachment As JiwaApplication.Documents.Document = Manager.CollectionItemFactory.CreateCollectionItem(Of Documents.Document)
            attachment.Description = ""
            attachment.PhysicalFileName = My.Computer.FileSystem.GetName(My.Computer.FileSystem.CombinePath(lTempFolderPath, NameToGiveAttachment))
            EmailObject.Attachments.Add(attachment)
            attachment.FileBinary = My.Computer.FileSystem.ReadAllBytes(My.Computer.FileSystem.CombinePath(lTempFolderPath, NameToGiveAttachment))

            If RequestReadReceipt Then
                EmailObject.RequestReadReceipt = True
            End If

            EmailObject.Status = JiwaEmail.EmailMessage.EmailStatuses.Sent
            EmailObject.Save()
            EM_Main_RecID = EmailObject.RecID

            exOption = Nothing
        End Sub


And it's in that method you add whatever documents (attachments) to the email.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: Adding email attachment from Documents

Postby DannyC » Mon May 13, 2019 9:43 am

That's awesome Mike, understood.
Thanks for you help.
User avatar
DannyC
Senpai
Senpai
 
Posts: 636
Joined: Fri Mar 22, 2013 12:23 pm
Topics Solved: 30


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron