eConnect - PM Transaction with Analytical Accounting

I've been coding eConnect for a while, and this one was hard. I wanted to be sure to save this for future use.

Because of its 'bolted on' nature, Analytical Accounting is often not straight forward.

This is a complete working example of a PM Transaction that uses AA.

Notes:

Not every line in the distribution needs AA; the AA lines need their own counter for that reason.

The DocType here for the AA is 0, not 1. That's in the documentation

Some errors that I got along the way:

An item with the same key has already been added

There are no distribution accounts linked to an Accounting Class.

The example below fixes those issues

 

An item with the same key has already been added > I got this when the specific AA code being used was not set up in Dynamics. I don't know how this error indicates that... but that's what fixed it. Swear.

Sub IntegrateAPDocument(ByVal oAPHeader As APHeader)
    'Create a PM Voucher
    'all the data needed for the voucher is passed in in the oAPHeader class
    'this class is not documented here... it's not important to the example
    Try
        'line counter used for the distribution lines
        Dim intNumberOfLines As Int16 = 0
        'line counter used for the AA lines
        Dim intAALine As Int16 = 0
        'initialize variables
        Dim strSQLServer As String = System.Configuration.ConfigurationManager.AppSettings("SQLServer")
        Dim strDabase As String = System.Configuration.ConfigurationManager.AppSettings("Database")
        'declare our eConnect classes
        'this is our eConnect class, documented here: http://dyndeveloper.com/thread.aspx?Threadid=1117
        Dim oeConnectType As New Microsoft.Dynamics.GP.eConnect.Serialization.eConnectType
        Dim oPMTransactionType As New Microsoft.Dynamics.GP.eConnect.Serialization.PMTransactionType
        Dim oeConnectFunctions As New GP11.eConnectFunctions(strSQLServer, strDabase)
        'get a voucher number
        'here we call a method in our eConnect class, documented here: http://dyndeveloper.com/thread.aspx?Threadid=1117
        Dim strVoucherNumber As String = oeConnectFunctions.GetPMNextVoucherNumber(IncrementDecrement.Increment)
        'create the document header
        Dim otaPMTransactionInsert As New Microsoft.Dynamics.GP.eConnect.Serialization.taPMTransactionInsert
        'populate the header
        With otaPMTransactionInsert
            .DOCTYPE = 1
            .VCHNUMWK = strVoucherNumber
            .VENDORID = oAPHeader.VendorID
            .DOCAMNT = oAPHeader.Purchases
            .PRCHAMNT = oAPHeader.Purchases
            .CHRGAMNT = oAPHeader.Purchases
            .DOCNUMBR = oAPHeader.DocumentNumber
            .BACHNUMB = oAPHeader.BatchID
            .DOCDATE = oAPHeader.DocDate
            .VADCDTRO = oAPHeader.VendorRemitToID
            .PYMTRMID = oAPHeader.PaymentTerms
            'this is for Binary Stream
            .USRDEFND1 = oAPHeader.Facility
            .USRDEFND2 = ""
        End With
        'assign the header to the master
        oPMTransactionType.taPMTransactionInsert = otaPMTransactionInsert
        'declare an object for the detail line
        Dim oTaPMDistribution As Microsoft.Dynamics.GP.eConnect.Serialization.taPMDistribution_ItemsTaPMDistribution
        Dim intLineCount As Int16 = 0
        For Each oAPLine As APLines In oAPHeader.APLines
            'loop through the line collection and 
            'populate the detail line
            oTaPMDistribution = New Microsoft.Dynamics.GP.eConnect.Serialization.taPMDistribution_ItemsTaPMDistribution
            With oTaPMDistribution
                .VCHRNMBR = strVoucherNumber
                .DOCTYPE = 1
                .DSTSQNUM = intLineCount
                .VENDORID = oAPHeader.VendorID
                .ACTNUMST = oAPLine.DistributionAccount
                .DISTTYPE = oAPLine.DistributionType
                .DEBITAMT = oAPLine.DebitAmount
                .CRDTAMNT = oAPLine.CreditAmount
                .DistRef = oAPLine.DistRef
                ReDim Preserve oPMTransactionType.taPMDistribution_Items(intLineCount)
                oPMTransactionType.taPMDistribution_Items(intLineCount) = oTaPMDistribution
            End With
            'not all lines need AA. Check for the presence of the AA data
            If oAPLine.AADimension > "" And oAPLine.AACode > "" Then
                Dim otaAnalyticsDistribution As New Microsoft.Dynamics.GP.eConnect.Serialization.taAnalyticsDistribution_ItemsTaAnalyticsDistribution
                'populate the aa line
                With otaAnalyticsDistribution
                    .DOCNMBR = strVoucherNumber
                    .DOCTYPE = 0
                    .DistSequence = intLineCount
                    .DistSequenceSpecified = 1
                    .aaTrxDim = oAPLine.AADimension
                    .aaTrxDimCode = oAPLine.AACode
                    .ACTNUMST = oAPLine.DistributionAccount
                    ReDim Preserve oPMTransactionType.taAnalyticsDistribution_Items(intAALine)
                    oPMTransactionType.taAnalyticsDistribution_Items(intAALine) = otaAnalyticsDistribution
                    intAALine += 1
                End With
            End If
            intLineCount += 1
        Next
        'here we call a method in our eConnect class, documented here: http://dyndeveloper.com/thread.aspx?Threadid=1117
        ReDim Preserve oeConnectType.PMTransactionType(0)
        oeConnectType.PMTransactionType(0) = oPMTransactionType
        oeConnectFunctions.CreateTransactionEntity(oeConnectType)
    Catch ex As Exception
        Throw ex
    End Try
End Sub

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences