Upsert an item in a list
Function: Upsert an item in a list
This function helps you manage lists of items by either updating an existing item or adding a new one. It's perfect for scenarios where you want to ensure a specific item is present in a list and its details are up-to-date, without having to manually check if it already exists.
Input,
- List: The list of items you want to modify. This list should contain objects, where each object represents an item with various details (like a product, a user, or a task).
- Filters: These are the conditions you set to find a specific item within your list. You can define one or more filters based on the item's attributes.
- Attribute: Choose a specific detail (field) of the items in your list (e.g., "Product ID", "User Email").
- Operator: Select how you want to compare the attribute's value (e.g., "Equal", "Greater than", "Contains").
- Value: Provide the value to compare against the chosen attribute.
- Overwriting values: These are the new details you want to apply. If an item matching your filters is found, its existing details will be updated with these new values. If no item matches, these values will be used to create and add a brand new item to the list.
Output,
This function does not return a new list. Instead, it directly modifies the provided 'List' by either updating an existing item or adding a new one. The original 'List' variable will contain the updated data after the function runs.
Execution Flow,
Real-Life Examples,
Example 1: Update an existing product in a shopping cart
Imagine you have a shopping cart list, and a customer updates the quantity of an item they already added.
Inputs:
- List:
[
\{"productID": "P001", "name": "Laptop", "quantity": 1, "price": 1200\},
\{"productID": "P002", "name": "Mouse", "quantity": 2, "price": 25\}
] - Filters:
- Attribute:
productID - Operator:
Equal - Value:
P001
- Attribute:
- Overwriting values:
\{"quantity": 2\}
Result: The "Laptop" item in the shopping cart list will be updated to have a quantity of 2.
[
\{"productID": "P001", "name": "Laptop", "quantity": 2, "price": 1200\},
\{"productID": "P002", "name": "Mouse", "quantity": 2, "price": 25\}
]
Example 2: Add a new product to the shopping cart if it's not there
A customer adds a new item to their cart. If the item isn't already in the cart, it should be added.
Inputs:
- List:
[
\{"productID": "P001", "name": "Laptop", "quantity": 1, "price": 1200\},
\{"productID": "P002", "name": "Mouse", "quantity": 2, "price": 25\}
] - Filters:
- Attribute:
productID - Operator:
Equal - Value:
P003
- Attribute:
- Overwriting values:
\{"productID": "P003", "name": "Keyboard", "quantity": 1, "price": 75\}
Result: Since no item with productID "P003" is found, a new "Keyboard" item will be added to the shopping cart list.
[
\{"productID": "P001", "name": "Laptop", "quantity": 1, "price": 1200\},
\{"productID": "P002", "name": "Mouse", "quantity": 2, "price": 25\},
\{"productID": "P003", "name": "Keyboard", "quantity": 1, "price": 75\}
]
Example 3: Update a user's status based on multiple criteria
You have a list of users and want to update the status of a specific user based on their ID and department.
Inputs:
- List:
[
\{"userID": "U101", "name": "Alice", "department": "Sales", "status": "Active"\},
\{"userID": "U102", "name": "Bob", "department": "Marketing", "status": "Active"\},
\{"userID": "U103", "name": "Charlie", "department": "Sales", "status": "Inactive"\}
] - Filters:
- Filter 1:
- Attribute:
userID - Operator:
Equal - Value:
U101
- Attribute:
- Filter 2:
- Attribute:
department - Operator:
Equal - Value:
Sales
- Attribute:
- Filter 1:
- Overwriting values:
\{"status": "On Leave"\}
Result: The user "Alice" (matching both userID "U101" AND department "Sales") will have their status updated to "On Leave".
[
\{"userID": "U101", "name": "Alice", "department": "Sales", "status": "On Leave"\},
\{"userID": "U102", "name": "Bob", "department": "Marketing", "status": "Active"\},
\{"userID": "U103", "name": "Charlie", "department": "Sales", "status": "Inactive"\}
]