Professional > Table scripting > Working with Change Tracker > Change Tracker: Functions
 
Change Tracker: Functions
Sample Script File: ChangeTrackerFunctions.mrs
This example script includes functions that assist in modifying Table Object Model metadata. See Running the sample table scripts for information on running the example scripts.
For example:
Enable the change tracker
Function ChangeTracker_Enable(objTableDoc)
Const Context_ChangeTracker = "__MetadataServices_ChangeTracker"
Dim objMDMDoc, objChangeTracker, objContext, bHasChangeTracker

Set objMDMDoc = objTableDoc.DataSet.MdmDocument

bHasChangeTracker = False
For Each objContext in objMDMDoc.RoutingContexts
If objContext.Name = Context_ChangeTracker Then
bHasChangeTracker = True
Exit For
End If
Next
If Not bHasChangeTracker Then
objMDMDoc.RoutingContexts.AddEx(Context_ChangeTracker, True)
End If
objMDMDoc.RoutingContexts.Current = Context_ChangeTracker

' Get the ChangeTracker
Set objChangeTracker = ChangeTracker_GetChangeTrackerObject(objTableDoc)
objChangeTracker.RoutingContextNames = Context_ChangeTracker

' Add all fields to Rounting
Dim oField
For Each oField In objTableDoc.DataSet.MdmDocument.Fields
objTableDoc.DataSet.MdmDocument.Routing.AddNewItem(oField, Context_ChangeTracker)
Next

' Return
Set ChangeTracker_Enable = objChangeTracker
End Function
Obtain the change tracker object
Function ChangeTracker_GetChangeTrackerObject(objTableDoc)
  Set ChangeTracker_GetChangeTrackerObject =   objTableDoc.DataSet.MdmChangeTracker
End Function
Record the specific field in the added tracker
Sub ChangeTracker_AddField(objTableDoc, objField)
  objField.Properties.Item["IsUserDefined"] = True

  Dim objChangeTracker
  Set objChangeTracker =   ChangeTracker_GetChangeTrackerObject(objTableDoc)
  objChangeTracker.AddField(objField)
End Sub
Create a folder
Function ChangeTracker_CreateFolder(ObjMDMDoc, strFolderName, lngIndex)
  Dim objFolder

  Set objFolder = ObjMDMDoc.CreateFolder(strFolderName)
  ObjMDMDoc.Routing.AddNewItem(objFolder,   "__MetadataServices_ChangeTracker", lngIndex)
  ' Return
  Set ChangeTracker_CreateFolder = objFolder
End Function
Add a field to a folder
Sub ChangeTracker_AddFieldToFolder(objMdmDoc, objFolder, objField)
  objFolder.AddNewItem(objField, "__MetadataServices_ChangeTracker")
End Sub

'Return the index of the specific field's routing item
Function Routing_GetFieldIndexOf(objRouting, objField)
  Dim lngIndex, objRoutingItem

  For lngIndex = 0 To _
      objRouting.Count("__MetadataServices_ChangeTracker") - 1
    Set objRoutingItem = objRouting.Item[lngIndex]
    If objRoutingItem.Item = objField Then
      Routing_GetFieldIndexOf = lngIndex
      Exit Function
    End If
  Next

  ' Return
  Routing_GetFieldIndexOf = -1
End Function
See also
Working with Change Tracker