Previous Topic

Next Topic

MB Tutorial: Script to Record Document Changes

You can create a MaxBasic script to record any changes to documents and save these to a table. This can be used for version control, so you can see what changes have been made to documents, and who made the changes and when.

Note: You can also use Tracking to record changes to fields in Accredo Tables.

In this example we show you how to track changes made to OE Orders. You can modify the script to track changes to other document types.

  1. First, create a memory table to store the modification details. Go to Navigator > Setup > Memory Table Builder. Alternatively, if you have TD installed, you can create a custom table to store the details.
  2. In the Fields tab, add the following fields. These will make up the document Modifications table.

    No.

    Name

    Type

    Domain

    Size

    1

    Module

    String

    Module Code

    2

    2

    ID

    String

    Document No

    12

    3

    PropertyName

    String

    Custom

    30

    4

    OldValue

    String

    Custom

    30

    5

    NewValue

    String

    Custom

    30

    6

    ModifiedDate

    Date

     

     

    7

    ModifiedTime

    Time

     

     

    8

    ModifiedUser

    String

    CO User Code

    30

  3. Click Generate (Alt+G). The code to create the memory table will be generated and shown in the Script Editor.
  4. Add code to add to the table when a change is made to the document. Enter or cut and paste the following script into the Script Editor after the existing code.

    Dim Form as Object
    Form = GetTriggerObject 'This sets Form to the OE Order Entry form that will call this script
    If Form.WasInserting = False Then 'Check that this is not a new Order being entered
      Dim PrevOrder as Object
      Dim ChangeList as Object
      PrevOrder = CreateObject("Accredo.OEOrderData")
      If PrevOrder.FindExact(Form.DocumentID) Then 'Find the previously saved order in the OEOrderData table
        ChangeList = ComparePropertyValues(Form, PrevOrder) 'Compare this order and the previously saved order
        For I = 0 to ChangeList.Count - 1
          tblMem1.Insert
          tblMem1.Module = Form.SourceModule
          tblMem1.ID = Form.DocumentID
          tblMem1.PropertyName = ChangeList.Item(I)
          tblMem1.OldValue = Eval("PrevOrder." & ChangeList.Item(I)) 'The property value for the PrevOrder
          tblMem1.NewValue = Eval("Form." & ChangeList.Item(I)) 'The property value on the form
          tblMem1.ModifiedDate = SystemDate
          tblMem1.ModifiedTime = Time
          tblMem1.ModifiedUser = User.LoginUserName
          tblMem1.Next
        Next I
        BrowseDataset(tblMem1)
      End If
    End If

    Note: You can later comment out the BrowseDataset(tblMem1) line if you don't want to see the dataset after each save.

  5. Click Check and Save (Alt+K) to check and save the code. Give the script a name like OEOrderCompareBeforeSave. The script will be saved as OEOrderCompareBeforeSave.pfs. Close the Script Editor.
  6. Open Script Events. Go to Accredo > Script > Script Events. Enter the following line. Note: If you already have a script linked to OESalesOrderEntryForm.BeforeSave, you will need to modify the existing script.

    Field

    Value

    Module

    OE

    Class

    OESalesOrderEntryForm

    Type

    BeforeSave

    Script Name

    Select the file name saved, such as OEOrderCompareBeforeSave.pfs

  7. Click Save (F9).
  8. The script will now be linked to the OE Order Entry Form. When you save changes to an Order, the script will be run before the order is saved.

See Also

Scripts & Scripting

Book Contents

Book Index