docs: public void doAddAutosequence(Record record) throws DBException
      {
          record = record.getBaseRecord();    // Must operate on the raw table
          
          String strRecordset = null;
          m_LastModifiedBookmark = null;
          CounterField fldID = (CounterField)record.getCounterField();
          // First step - get the largest current key
      // Save current order and ascending/descending and selection
          int iOrigOrder = record.getDefaultOrder();
          boolean bOrigDirection = record.getKeyArea(DBConstants.MAIN_KEY_AREA).getKeyField(DBConstants.MAIN_KEY_FIELD).getKeyOrder();
          boolean bCounterSelected = fldID.isSelected();
          boolean brgSelected[] = new boolean[record.getFieldCount()];
          for (int iFieldSeq = DBConstants.MAIN_FIELD; iFieldSeq <= record.getFieldCount() + DBConstants.MAIN_FIELD - 1; iFieldSeq++)
          {
              brgSelected[iFieldSeq] = record.getField(iFieldSeq).isSelected();
              record.getField(iFieldSeq).setSelected(false);  // De-select all fields
          }
          fldID.setSelected(true);    // Only select the counter field
      // Set reverse order, descending
          record.setKeyArea(DBConstants.MAIN_KEY_AREA);
          record.getKeyArea(DBConstants.MAIN_KEY_AREA).getKeyField(DBConstants.MAIN_KEY_FIELD).setKeyOrder(DBConstants.DESCENDING);
          boolean[] rgbEnabled = record.setEnableListeners(false);    // Temporarily disable behaviors
  
          strRecordset = record.getSQLQuery(false, null);
          ResultSet resultSet = this.executeQuery(strRecordset, DBConstants.SQL_AUTOSEQUENCE_TYPE, null);
          this.setResultSet(resultSet, DBConstants.SQL_AUTOSEQUENCE_TYPE);
      // Set back, before I forget.
          record.setKeyArea(iOrigOrder);
          record.getKeyArea(DBConstants.MAIN_KEY_AREA).getKeyField(DBConstants.MAIN_KEY_FIELD).setKeyOrder(bOrigDirection);
  
          try   {
              m_iColumn = 1;
              boolean bMore = resultSet.next();
              if (!bMore)
              {
                  int iStartingID = record.getStartingID();
                  fldID.setValue(iStartingID - 1, DBConstants.DONT_DISPLAY, DBConstants.READ_MOVE);
              }
              else
                  this.dataToField(fldID);    // Move the high value to the ID
              fldID.setModified(true);        // Just to be sure
          } catch (SQLException ex) {
              record.setEnableListeners(rgbEnabled); // Re-enable behaviors
              throw this.getDatabase().convertError(ex);
          } finally {
              for (int iFieldSeq = DBConstants.MAIN_FIELD; iFieldSeq <= record.getFieldCount() + DBConstants.MAIN_FIELD - 1; iFieldSeq++)
              {
                  record.getField(iFieldSeq).setSelected(brgSelected[iFieldSeq]);
              }
              fldID.setSelected(bCounterSelected);
              this.setResultSet(null, DBConstants.SQL_AUTOSEQUENCE_TYPE);    // Close resultset. If I share statements with an active query, reset the active query (Ouch-serious performance hit)
          }
          if (!bCounterSelected)
              fldID.setSelected(true);    // Counter must be selected to write this record
  
          // Second step - bump the key and write until successful.
          int iCount = 0;
          while (iCount++ < MAX_DUPLICATE_ATTEMPTS)
          {
              fldID.setValue(fldID.getValue() + 1, DBConstants.DONT_DISPLAY, DBConstants.READ_MOVE);  // Bump counter
              strRecordset = record.getSQLInsert(SQLParams.USE_INSERT_UPDATE_LITERALS);
              int iType = DBConstants.SQL_INSERT_TABLE_TYPE;
              int iRowsUpdated = 0;
              try   {
                  iRowsUpdated = this.executeUpdate(strRecordset, iType);
              } catch (DBException ex)    {
                  if (ex.getErrorCode() == DBConstants.DUPLICATE_COUNTER)   // Duplicate key
                  {
                      Utility.getLogger().info("Duplicate key, bumping value");
                      iRowsUpdated = 0;
                  }
                  else
                  {
                      if (!bCounterSelected)      // Set back to orig value
                          fldID.setSelected(bCounterSelected);
                      record.setEnableListeners(rgbEnabled); // Re-enable behaviors
                      throw ex;
                  }
              }
              if (iRowsUpdated == 1)
                  break;      // Success!!!
          }
  
          if (!bCounterSelected)      // Set back to orig value
              fldID.setSelected(bCounterSelected);
          record.setEnableListeners(rgbEnabled); // Re-enable behaviors
  
          if (iCount > MAX_DUPLICATE_ATTEMPTS)
              throw new DBException(DBConstants.DUPLICATE_KEY);  // Highly unlikely
          // Third step - Save the counter for possible getLastModified call
          m_LastModifiedBookmark = record.getHandle(DBConstants.BOOKMARK_HANDLE);
      }