Best Practices - Updating an Existing Object

The purpose of this document is to provide best practices to update an existing object.

The recommended method is the following:

  1. Create a NewInstance.
  2. Read the instance to be updated.
    • If it can be read (i.e. if it already exists), set the new properties, and update the instance.
    • If not, free the instance to clear the read errors, create a new instance, set all the instance properties, and insert the new instance.

Example:

# In a given class, two properties (MYKEY, MYSTRING) have to be created in MYTABLE if they don't exist
# Otherwise, update MYSTRING property
Local File MYTABLE
Local Instance MY_TABLE Using C_MYTABLE
  MY_TABLE = NewInstance C_MYTABLE AllocGroup null
  [L]ASTATUS = fmet MY_TABLE.AREAD(this.MYKEY)
  If [L]ASTATUS = [V]CST_AOK
    MY_TABLE.MYSTRING = this.MYSTRING
    [L]ASTATUS = fmet MY_TABLE.AUPDATE()
    If [L]ASTATUS<>[V]CST_AOK
      [L]ASTATUS = fmet this.ASETERROR("MYSTRING","Error updating record"-this.MYKEY,[V]CST_AERROR)
    Endif
  Else
  # Initialize Instance
    FreeGroup MY_TABLE                                    : # Remove error on read
    MY_TABLE = NewInstance C_MYTABLE AllocGroup null     : # create new instance
    [L]ASTATUS = fmet MY_TABLE.AINIT()
    If [L]ASTATUS=[V]CST_AOK
      MY_TABLE.MYKEY = this.MYKEY
      MY_TABLE.MYSTRING = this.MYKEY
      [L]ASTATUS = fmet MY_TABLE.AINSERT()
      If [L]ASTATUS<>[V]CST_AOK
        [L]ASTATUS = fmet this.ASETERROR("MYKEY","Error creating record"-this.MYKEY,[V]CST_AERROR)
      Endif
    Else
      # Error upon table initialization
      [L]ASTATUS = fmet this.ASETERROR("MYKEY","Error upon initialization of"-this.MYKEY,[V]CST_AERROR)
    Endif
  Endif
  FreeGroup MY_TABLE