This example shows how a script can be broken into various steps.
Typically, the process for a standing invoice run will include some or all of the following steps:
Lets take a simple case first. The following script will:
Dim InvList as Object
Dim InvForm as Object
Dim Invoice as Object
InvList = CreateObject("Accredo.INInvoiceListForm")
InvList.Selection = "Standing Invoice"
InvList.ToggleShowAll
Invoice = InvList.Invoice
Count = 0
Invoice.First
Do Until Invoice.EOF
If Upper(Invoice.InternalReference) = "MONTHLY" Then
Invoice.Open
InvForm = GetActiveObject
If IsNull(InvForm) or InvForm.ClassName <> "INInvoiceEntryForm" Then
Error "Bad Karma"
End If
Count = Count + 1
InvForm.DuplicateInvoice
InvForm.Save
InvForm.Close
Invoice.Open
InvForm = GetActiveObject
If IsNull(InvForm) or InvForm.ClassName <> "INInvoiceEntryForm" Then
Error "Bad Karma"
End If
InvForm.Edit
InvForm.OrderNo = Val(InvForm.OrderNo) + 1
InvForm.DocumentDate = AddMonths(InvForm.DocumentDate,1 ,31)
InvForm.PeriodID = PeriodName(PeriodForDate(InvForm.DocumentDate))
InvForm.Save
InvForm.Close
End If
Invoice.Next
Loop
InvList.Close
Print Count & " Invoices Generated"
This process will be quite slow if you have a lot of invoices to create and are working at the form level. Modified to work at the data level the script looks like this:
Dim InvData as Object
Dim Invoice as Object
InvData = CreateObject("Accredo.INInvoiceData")
InvData.IndexName = "TypeCustomerPeriodDocument"
InvData.SetRange("N")
Invdata.FilterSort.Filter = "(DocumentClass=""S"") And (InternalReference=""Monthly"")"
Invoice = CreateObject("Accredo.INInvoiceData")
Invoice.IndexName = "Document"
Count = 0
InvData.First
Do Until InvData.EOF
Invoice.SetRange(InvData.DocumentID)
Invoice.DuplicateInvoice
Invoice.DocumentDate = InvData.DocumentDate
Invoice.PeriodID = (PeriodForDate(Invoice.DocumentDate))
Invoice.Save
Count = Count + 1
InvData.Edit
InvData.OrderNo = Val(InvData.OrderNo) + 1
InvData.DocumentDate = AddMonths(InvData.DocumentDate,1 ,31)
InvData.PeriodID = PeriodForDate(InvData.DocumentDate)
InvData.Save
InvData.Next
Loop
Invoice = Nothing
InvData = Nothing
Print Count & " Invoices Generated"
Some other enhancements you may want to make are: