Shift
Removes and returns the first element from an array.
Common Properties
- Name - The custom name of the node.
- Color - The custom color of the node.
- Delay Before (sec) - Waits in seconds before executing the node.
- Delay After (sec) - Waits in seconds after executing node.
- Continue On Error - Automation will continue regardless of any error. The default value is false.
If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.
Inputs
- Array - The array to remove the first element from.
Outputs
- Element - The removed first element from the array.
- Array - The modified array with the first element removed.
How It Works
The Shift node removes the first element from an array and returns both the removed element and the modified array. When executed, the node:
- Receives an array through the Array input
- Validates that the array is not null
- Removes the first element (at index 0)
- Returns the removed element through the Element output
- Returns the modified array (with first element removed) through the Array output
- Handles thread-safe operations when working with Global or Flow scope variables
Requirements
- Valid array as input (not null)
- Array must have at least one element to shift
Error Handling
The node will return specific errors in the following cases:
- Array is null: "Array cannot be null"
Usage Examples
Example 1: Process Tasks in Order (Queue/FIFO)
Implement First-In-First-Out (FIFO) processing:
- Array:
["Task A", "Task B", "Task C", "Task D"] - Shift → Element:
"Task A", Array:["Task B", "Task C", "Task D"] - Process the element "Task A"
- Shift again → Element:
"Task B", Array:["Task C", "Task D"] - Continue until array is empty
Example 2: Sequential Customer Processing
Process customers in the order they arrived:
- Array:
{{flow.customerQueue}}=["John", "Mary", "David", "Sarah"] - Shift → Element:
"John", Array:["Mary", "David", "Sarah"] - Process customer "John"
- Update
{{flow.customerQueue}}with remaining customers - Repeat for next customer
Example 3: Message Queue Processing
Process messages from a queue:
- Array:
{{flow.messageQueue}} - Loop while array is not empty:
a. Shift → get first message and reduced array
b. Process the message
c. Update
{{flow.messageQueue}}with the reduced array d. Continue until all messages processed
Example 4: Batch Processing with Order
Process data batches in sequence:
- Array:
{{flow.batchQueue}}=[batch1, batch2, batch3] - Shift → Element: batch1, Array:
[batch2, batch3] - Process batch1
- Continue with remaining batches in order
Example 5: Priority Queue Implementation
Process items by priority (assuming array is pre-sorted by priority):
- Sort items by priority (highest priority first)
- Store in
{{flow.priorityQueue}} - Shift → get highest priority item
- Process the item
- Repeat for next highest priority
Example 6: Remove and Archive First Entry
Remove the oldest entry and archive it:
- Array:
{{flow.entries}}=[entry1, entry2, entry3] - Shift → Element: entry1, Array:
[entry2, entry3] - Archive entry1 to database or file
- Update
{{flow.entries}}with remaining entries
Tips for Effective Use
- Queue behavior: Shift implements FIFO (First-In-First-Out) - opposite of Pop which is LIFO
- Original array is modified: The returned array has one less element
- Use in loops to process all elements from beginning to end
- Combine with Push for queue data structure implementation
- Store the returned array back to the original variable to maintain state
- Use Array.GetLength first to check if array has elements before shifting
- For large arrays, consider the performance of repeated shift operations
- Thread-safe when using Global or Flow scopes
- The removed element can be any data type
- Perfect for sequential, ordered processing
Common Use Cases
- Implementing FIFO (queue) data structures
- Processing items in the order they were added
- Customer queue management
- Message queue processing
- Task scheduling (first come, first served)
- Sequential batch processing
- Order fulfillment systems
- Request processing in order of receipt
- Playlist or media queue management
- Event processing in chronological order
Difference Between Shift and Pop
| Operation | Shift | Pop |
|---|---|---|
| Removes from | Beginning of array | End of array |
| Processing order | FIFO (First-In-First-Out) | LIFO (Last-In-First-Out) |
| Use case | Queue, sequential processing | Stack, reverse processing |
| Example | Process oldest request first | Undo last action |
| Real world | Ticket queue at store | Browser back button |
Common Errors and Solutions
Error: "Array cannot be null"
Cause: The array input is null or undefined.
Solutions:
- Verify that the array variable exists and is initialized
- Check that previous nodes successfully created the array
- Use Array.Assign to initialize arrays before shifting
- Add null checking before calling Shift
Runtime Issue: Empty Array
Behavior: Shifting from an empty array may cause issues.
Solutions:
- Use Array.GetLength to check array size before shifting
- Add condition: If length > 0, then shift
- Implement empty queue handling logic
- Use Continue On Error if processing should continue when queue is empty
Integration Examples
Example: Queue-Based Processing
1. Initialize queue: ["Request1", "Request2", "Request3"]
2. While queue is not empty:
a. Shift → get first element and reduced queue
b. Process element
c. Update queue variable
3. All requests processed in FIFO order
Example: Safe Shift with Validation
1. Get array: {{flow.queue}}
2. Use Array.GetLength to check size
3. If length > 0:
a. Shift → get first element
b. Process element
c. Update array
4. If length = 0:
a. Skip shift operation
b. Log "Queue is empty"
Example: Combine with Push for Queue Operations
Enqueue (add to queue):
1. Get current queue: {{flow.queue}}
2. Use Push to add new item at end
3. Update {{flow.queue}}
Dequeue (remove from queue):
1. Get current queue: {{flow.queue}}
2. Use Shift to remove and get first item
3. Process the item
4. Update {{flow.queue}}
Result: First-In-First-Out behavior
Example: Customer Service Queue
1. Customers arrive and are added via Push:
{{flow.customerQueue}} = ["John", "Mary", "David"]
2. Service representative becomes available:
a. Check queue length
b. If customers waiting:
- Shift → get next customer "John"
- Serve customer "John"
- Update queue: ["Mary", "David"]
c. Repeat for next customer
3. Result: Customers served in order of arrival
Example: Message Processing Pipeline
1. Messages arrive in queue: {{flow.messages}}
2. Message processor:
a. While messages exist:
- Shift → get oldest message
- Validate message
- Process message
- Log result
- Update queue
b. When queue empty:
- Wait for new messages
3. Result: Messages processed in order received
Example: Batch Job Queue
1. Jobs submitted and added to queue: {{flow.jobQueue}}
2. Job processor:
a. Get job queue length
b. If jobs available:
- Shift → get next job
- Execute job
- Check for errors
- Update status
- Update job queue
c. If no jobs:
- Idle or shutdown
3. Result: Jobs executed in submission order
Performance Considerations
- Shift operation requires shifting all remaining elements
- Time complexity: O(n) where n is array length
- For very large arrays, consider alternative data structures
- Multiple shifts in sequence can be inefficient
- Consider processing in batches for better performance
- Use appropriate scope (Message/Flow/Global) based on needs
Queue Implementation Best Practices
When using Shift for queue implementation:
- Check length before shifting to avoid errors
- Store updated array back to the variable
- Use Push to add new items to the queue
- Monitor queue size to prevent unbounded growth
- Implement error handling for empty queue scenarios
- Consider capacity limits for production queues
- Log queue operations for debugging and monitoring