Conditional Set Filters
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:
Add a Database Request: Start by adding a
Get Recordrequest from the same table (products). Set theField Valueto the product ID, and give the variable a meaningful name (e.g.,product).Apply the First Not Empty Filter: In the
Edit Recordrequest, apply theFirst Not Emptyfilter to each input field you want to conditionally update. For instance, for thenamefield, you'd select theFirst Not Emptyfilter and set the value toproduct.name. This way, the field will only be updated if the input value isn't empty.Use First Not Null for Null Values: If you're dealing with null values instead of empty strings, you can use the
First Not Nullfilter instead ofFirst 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:
Update the Object Variable: Use the
Update Variablerequest to update theproductobject variable.Apply the Set If Not Empty Filter: Search for the
Set If Not Emptyfilter and apply it to each property you want to conditionally update. For example, to update thenameproperty, set thePathtonameand theValueto thenameinput field.Chain Multiple Filters: You can chain multiple
Set If Not Empty(orSet If Not Null) filters to update multiple properties in one go.Use the Updated Object: Finally, use the updated
productobject variable in yourEdit Recordrequest 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!
Sign up for Xano.
100,000+ people build on Xano.
Start today and scale to millions.