The Timer event script creates
MS as a mailsession object. MS then has all the
properties, events, and methods (PB calls them functions) needed
to describe and handle interaction with a Windows, or Mac,
computer's default MAPI client. Msg is declared as
a Message type Object, and Recipient as a MailRecipient.
The instance variable Attempts
is incremented at each pass through the Timer event's script
so the number of attempts can be displayed on w_getpsx12.
MailLogon, MailDownLoad!,
mailReturnSuccess!, MailGetMessages, and the array MessageID[]
are used to logon to the MAPI session and get InBox messages'
ids into the array MessageID[]. The UpperBound
function applied to MessageID[] gives the count of items in the
InBox and loads InBoxCount after the Download request.
Of these InBoxCount messages,
some may be formatted as psX12 transactions with a leading *!*!
in the subject line. A for/next loop goes through the
InBox InBoxCount times looking for *!*!, increments psX12Count
when it finds one, and drops the InBoxLoc-ation of each
potential psX12 transaction into an array at psX12Loc[psX12Count].
If psX12Count is zero after this
check the script does a goto (not of the spaghetti
variety) to MSLogoff, which tidies up after the session and
returns control back to PB, which keeps the Timer running.
If psX12Count isn't zero the
script continues to CONNECT to the database and write any
of the psX12 transactions that have been pigeon-holed into a
file for easy handling later in the script.
A for/next loop checks MS.mailReadMessage
for each of the locations 1 through psX12Count & loads
the MS object with each email message. The psX12FileName
is constructed using the current date, time, and position in the
batch of email received. Each email message has its Subject
concatenated with CrLf and its NoteText as FileWrite streams
it to a file on disk. FileClose closes each
file. As each file is written, it's psX12FileName is put
into psX12FileNames[psX12Count] for easy reference in the
next part of the script. MailReadMessage called
with a FALSE in the last parameter leaves the message marked
unread, and a TRUE there marks it read.
The next for/next loop gets each
of the psX12FileNames, checks for a valid file name (not
"") and starts the edit of the email item by doing a FileOpen
and FileRead of the item into psX12Line. Note that
while the file was written in stream mode it is read one line at
a time using the CrLf as a record delimiter.
The mid string function
gets a potential EnterpriseId from the 5th postion in the string
SubjectLine for a length of 4 into EmailEnterpriseId. An
SQL SELECT checks the table enterprises for a valid
EnterpriseId and returns the current value for EnterpriseName
and EmailAddress.
If there is an email address the
edit continues. If there is no email address there is no
reason to edit anymore and the only thing that happens is that
the list box lb_psx12inbound.AddItem function displays it
in the window.
The instance variable EditErrors
is used to accumulate the count of errors that are detected
through the script. Another instance variable, EditMessage,
is used to echo each line of the transaction as it is
encountered and any edit messages that may apply. At
several points in the script EditErrors being greater than zero
will terminate processing. If the transaction is
completely parsed with no EditErrors the database is updated, or
the REQUEST is fulfilled.
After the script leaves the
subject line, where the position in a string is all that is used
to find the EnterpriseId, the other lines in the psX12
transaction all have their fields delimited with an asterisk so
the data needs to be 'parsed' out of the string and put into
variables. The user function ParseLine is called
for each subsequent psX12Line and parses out each field it finds
into an array, defined as an instance variable Fields[].
It returns an integer into FieldCount so that the first
edit consideration, number of fields, is an easy one to find.
If there are no edit errors from
SubjectLine and the expected FieldCount is found, choose case
Fields[1] calls the function appropriate for a case
of JV, CAT, or REQUEST*CAT in Fields[1]. JV calls handleJV,
CAT calls handleCAT, and REQUEST*CAT gets handleREQUEST.
Anything Else gets an EditMessage complaining about the
data there. Each function call passes psX12File (the
integer with the file's 'handle' from the FileOpen) with it so
that the function can ReadFile(psX12File) and get each
subsequent line.
As each of the functions returns
control to the Timer event's script EditErrors is checked.
If it is not zero a message is concatenated to the end of
EditMessage advising that the transaction wasn't processed.
Email is sent in each case,
EditErrors or not. The property Recipient.Name is
set to the string EmailAddress, Msg.Recipient[1] is set
to Recipient, and Msg.Subject & Msg.NoteText get
"!*!*Reponse from the Ledger Engine" and EditMessage,
respectively. MS.mailSend engages the mailsession
function that interacts with the MAPI client to send the
message.