Skip to main content

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.
info

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:

  1. Receives an array through the Array input
  2. Validates that the array is not null
  3. Removes the first element (at index 0)
  4. Returns the removed element through the Element output
  5. Returns the modified array (with first element removed) through the Array output
  6. 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:

  1. Array: ["Task A", "Task B", "Task C", "Task D"]
  2. Shift → Element: "Task A", Array: ["Task B", "Task C", "Task D"]
  3. Process the element "Task A"
  4. Shift again → Element: "Task B", Array: ["Task C", "Task D"]
  5. Continue until array is empty

Example 2: Sequential Customer Processing

Process customers in the order they arrived:

  1. Array: {{flow.customerQueue}} = ["John", "Mary", "David", "Sarah"]
  2. Shift → Element: "John", Array: ["Mary", "David", "Sarah"]
  3. Process customer "John"
  4. Update {{flow.customerQueue}} with remaining customers
  5. Repeat for next customer

Example 3: Message Queue Processing

Process messages from a queue:

  1. Array: {{flow.messageQueue}}
  2. 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:

  1. Array: {{flow.batchQueue}} = [batch1, batch2, batch3]
  2. Shift → Element: batch1, Array: [batch2, batch3]
  3. Process batch1
  4. Continue with remaining batches in order

Example 5: Priority Queue Implementation

Process items by priority (assuming array is pre-sorted by priority):

  1. Sort items by priority (highest priority first)
  2. Store in {{flow.priorityQueue}}
  3. Shift → get highest priority item
  4. Process the item
  5. Repeat for next highest priority

Example 6: Remove and Archive First Entry

Remove the oldest entry and archive it:

  1. Array: {{flow.entries}} = [entry1, entry2, entry3]
  2. Shift → Element: entry1, Array: [entry2, entry3]
  3. Archive entry1 to database or file
  4. 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

OperationShiftPop
Removes fromBeginning of arrayEnd of array
Processing orderFIFO (First-In-First-Out)LIFO (Last-In-First-Out)
Use caseQueue, sequential processingStack, reverse processing
ExampleProcess oldest request firstUndo last action
Real worldTicket queue at storeBrowser 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:

  1. Check length before shifting to avoid errors
  2. Store updated array back to the variable
  3. Use Push to add new items to the queue
  4. Monitor queue size to prevent unbounded growth
  5. Implement error handling for empty queue scenarios
  6. Consider capacity limits for production queues
  7. Log queue operations for debugging and monitoring