📚 SAP Business One SDK Help

Adding user keys sample
See Also
Private Sub AddUserKey()

'//****************************************************************************
'// The UserKeysMD represents a meta-data object that allows you
'// to add\remove user defined keys.
'//****************************************************************************

    Dim oUserKeysMD As SAPbobsCOM.UserKeysMD

    '//flag
    Dim bFlagFirst As Boolean

    bFlagFirst = True

'//****************************************************************************
'// In any meta-data operation there should be no other object "alive"
'// but the meta-data object, otherwise the operation will fail.
'// This restriction is intended to prevent collisions.
'//****************************************************************************

    '// The meta-data object must be initialized with a
    '// regular UserKeys object
    Set oUserKeysMD = oCompany.GetBusinessObject(oUserKeys)

    '// Set the table name and the key name
    oUserKeysMD.TableName = "OCRD" '// BP table
    oUserKeysMD.KeyName = "BE_MyKey1"

'//*******************************************
'// Add a column to a key button:
'//-------------------------------------------
'// To add an additional column to
'// the key, an additional element must be
'// created in the Elements collection.
'// The Add method of the Elements collection
'// must be used only as of the second element.

    '// Do not use the Add method for the first element
    If bFlagFirst = True Then
        bFlagFirst = False
    Else
        '// Add an item to the Elements collection
        oUserKeysMD.Elements.Add
    End If

    '// Set the column's alias
    oUserKeysMD.Elements.ColumnAlias = "The required field name comes here"

'// Determine whether the key is unique or not
    oUserKeysMD.Unique = tYES

'// Add the key
    oUserKeysMD.Add

End Sub