Using UniqueID's

UniqueID's are used to maintain uniqueness of fields within Bubble "things."

Use Case

You as the developer, create a UniqueID field, and then insert values into this index. The system prevents duplicates from being created, giving you a reliable check for ID's even under concurrent load.

For example, when creating an Order ID, a check is first performed against the index to see if this value has already been inserted. If it has, you would abandon your data insert. If not, you would continue.

Sample Implementation

Installation of the plugin must be completed first -- Instructions found HERE

Creating Unique ID's

You may create as many Unique ID fields as needed. A Unique ID field can be an order id, an invoice id, or anything else you want to prevent duplicates for. Fields can be either text or number only fields. You create the Unique ID only once, and then Insert values as needed and covered in the next section.

  • Add the Create UniqueID action to a workflow.

  • Set the name of the UniqueID you'd like to use ("order_id", "invoice_id", etc.)

  • Set the type of UniqueID you are creating ("text" or "number")

  • When the workflow is executed, a new UniqueID will be created. The status of this is provided by the action. A response code of 200 indicates it was successfully created. A response code of 400 indicates and error and further information is provided in the response message. The actual JSON provided is shown below, and it can be evaluated using the typical Result of Step x syntax.

JSON response

{
  "response_code": 200,
  "response_message": "Successfully created test_order_id for your_user_name",
  "unique_id_name": "test_order_id",
  "unique_id_type": "char(50)",
  "user_name": "your_user_name"
}

Inserting a single value into a UniqueID

  • Inserting into the index is simply a matter of providing the UniqueID name along with the value to be indexed.

  • A result code of 400 indicates an error.

    The results provided are, which again can be evaluated with the Result of Step x syntax. If response_code is 200, the value was added to the index. For typical use cases, this would indicate it was safe to use the provided value when inserting your data record.

JSON responses

{
  "unique_id_value": "testing value",
  "response_code": 200,
  "response_message": "Successfully inserted into index",
  "unique_id_name": "testing_field",
  "user_name": "your_user_name"
}
  • A result code of 499 indicates the value was already found within the index. For typical use cases, this would indicate it was NOT safe to use the provided value when inserting your data record.

  "response_code": 499,
  "response_message": "1062 (23000): Duplicate entry 'testing value' for key 'usr_IDs_your_user_name_testing_field_index'",
  "unique_id_name": "testing_field",
  "unique_id_value": "testing value",
  "user_name": "your_user_name"
}

Remove a value from a UniqueID

  • Use the "Delete from UniqueID" action to remove a value from a UniqueID by providing the name of the ID and the value to be removed.

  • Status code 200 indicates the value was removed, and 400 indicates some other error.

JSON response

{
  "response_code": 200,
  "response_message": "Successfully deleted value",
  "unique_id_name": "testing_field",
  "unique_id_value": "testing value",
  "user_name": "your_user_name"
}

Insert a list of values into a UniqueID

The ID Generator plug-in is expecting a comma separated list of double quoted values when inserting in bulk. The free Toolbox plugin (https://bubble.is/plugin/toolbox-1488796042609x768734193128308700) is a great way to prepare a Bubble list for submission. A sample is shown below, which does a search for a list of things, and formats properly. Your item type, data source and field names will of course be different.

  • Use the "Bulk Insert into UniqueID" action, similarly to a single value add, but point to the List Item Expression element you created.

  • The response for a bulk insert provides the status for each individual value. Status codes are the same, 200 for successfully inserted, 499 for duplicate, and 400 for other errors.

JSON response

{
  "insert_results": [
    {
      "insert_result_code": 200,
      "insert_result_message": "Added - test 1a",
      "inserted_value": "test 1a"
    },
    {
      "insert_result_code": 499,
      "insert_result_message": "Failed - 1062 (23000): Duplicate entry 'test 2' for key 'usr_IDs_your_user_name_testing_field_index'",
      "inserted_value": "test 2"
    }
  ],
  "response_code": 200,
  "response_message": "Successfully processed - Individual insert results found in insert_results element",
  "unique_id_name": "testing_field",
  "user_name": "your_user_name"
}

Last updated