Data Management

Conditional Set Filters

Summary

Hey there! In this post, we'll dive into Xano's latest feature - four new filters designed to streamline your business logic and remove the need for complex conditional statements. These filters are especially handy when you're editing records or objects and want to update specific fields only if they're not empty or null. Let's get started!

Updating Records with First Not Empty and First Not Null

Imagine you're working with a `products` table, and you want to update a record's fields like `name`, `description`, and `price`. Previously, you'd need conditional statements for each field to only update if the input value wasn't empty or null. But with Xano's new filters, you can kiss those conditionals goodbye and keep your function stack clean and tidy.

Here's how it works:

  1. Add a Database Request: Start by adding a `Get Record` request from the same table (`products`). Set the `Field Value` to the product ID, and give the variable a meaningful name (e.g., `product`).
  2. Apply the First Not Empty Filter: In the `Edit Record` request, apply the `First Not Empty` filter to each input field you want to conditionally update. For instance, for the `name` field, you'd select the `First Not Empty` filter and set the value to `product.name`. This way, the field will only be updated if the input value isn't empty.
  3. Use First Not Null for Null Values: If you're dealing with null values instead of empty strings, you can use the `First Not Null` filter instead of `First Not Empty`.

That's it! Now, when you run the endpoint, Xano will intelligently update only the fields that have non-empty (or non-null) input values, leaving the rest untouched.

Updating Objects with Set If Not Empty and Set If Not Null

These filters shine when updating objects too. Let's say you have a `product` object, and you want to update its properties (`name`, `description`, `price`) without overwriting existing values with empty or null inputs.

Here's how you can leverage the `Set If Not Empty` and `Set If Not Null` filters:

  1. Update the Object Variable: Use the `Update Variable` request to update the `product` object variable.
  2. Apply the Set If Not Empty Filter: Search for the `Set If Not Empty` filter and apply it to each property you want to conditionally update. For example, to update the `name` property, set the `Path` to `name` and the `Value` to the `name` input field.
  3. Chain Multiple Filters: You can chain multiple `Set If Not Empty` (or `Set If Not Null`) filters to update multiple properties in one go.
  4. Use the Updated Object: Finally, use the updated `product` object variable in your `Edit Record` request to apply the changes.

By following these steps, you'll ensure that only the object properties with non-empty (or non-null) input values are updated, preserving the existing values for the rest.

These new filters are game-changers for streamlining your business logic, making your function stacks more readable, and simplifying the process of updating records and objects conditionally. Give them a try and see how they can supercharge your Xano workflows!

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free