Best Practices for Iterating Through Items in Workflow

Efficient iteration is one of the keys to building powerful, maintainable Laserfiche workflows. For Each activities let you repeat actions across collections of entries, users, rows, or values, but the real value comes when you combine iteration with smart data handling. By pairing For Each activities with Assign Token Values and indexing, you can design workflows that adapt dynamically to your data and produce meaningful, reusable results.

This page presents best practices for designing iterative workflows that are clear, efficient, and scalable. Rather than focusing on the For Each activity in isolation, we’ll explore how to use it together with other activities to collect, shape, and persist data effectively.

The Iteration Pattern

At the heart of effective iteration is a repeatable pattern that combines For Each activities, token assignment, and indexing. Understanding this pattern makes it easier to build complex automation from simple, consistent components.

Gather

Gather a collection of data (for example, entries, database rows, or users). Many activities can gather information such as the Search Repository activity, Find Entries activity, Query Data activity, Custom Query activity, Retrieve Shortcuts activity, Find Group activity, and Get Document Signatures activity.

Iterate

Iterate, or loop, through each item gathered using a For Each activity. The For Each activities are detailed in the How Each Component Works section below.

Aggregate or Process

Aggregate or process information inside the loop. For Each activities perform a set of actions for each item in the gathered collection. For example, you could rename each entry found in a search or email each user found in a group. You can also use a combination of Assign Token Values activities to aggregate information gathered from each item being processed. See the Leveraging the Assign Token Values Activity section below.

Extract or Format

Extract or format specific results using indexing or token joining to make information processed in the loop available to other activities or processes. See the Indexing and Joining Multi-Value Tokens section below.

Used together, these steps let you build workflows that:

  • Compile summaries or lists dynamically during iteration
  • Make loop-generated data available outside the loop for later use
  • Reference exactly the value you need, such as the first, last, or nth item
  • Reduce complexity by managing data through tokens instead of nested loops

Example: Generating and Distributing Status Reports

You need to generate an updated summary of all contract vendor prospects for each manager and email it to them. This scenario shows how the For Each activity, Assign Token Values activity, and indexing pattern works in practice. ClosedSee a screenshot of this workflow.

Example workflow with details of how each activity is configured

1. Gather Data

Begin with a Query Data activity to retrieve information about current vendor prospects from the company database. The results include the vendor, contact name, contact phone number, and industry.

2. Prepare to Compile Data

Use the Assign Token Values activity to create a multi-value token called Vendor but leave it blank.

3. Iterate Through Each Prospective Vendor

Use a For Each Row activity to loop through, or process, each vendor returned by the query. Inside this loop, tokens such as %(ForEachRow_Vendor) and %(ForEachRow_Contact Name) represent the vendor and contact name currently being processed.

4. Compile a List of Vendors

Inside the For Each activity, use an Assign Token Values activity to append the current vendor name (%(ForEachRow_Vendor)) to the Vendor token. This allows the workflow to build a collection of completed vendors dynamically as it iterates. Repeat this step to create tokens for all the information you want to collect.

5. Persist Data After the Loop

When the For Each Row loop completes, the Vendor token contains a multi-value list of all prospective vendors.

6. Use Indexing to Reference Specific Values

To highlight the most recent vendor prospect, index into the Vendor token with:

%(Vendor_All#[-1]#)

This retrieves the last value added to the list. You might include it in an email or in the first line of a report body.

7. Send Summary Emails

Use an E-mail activity to send a list of all prospective vendors:

%(Vendor_All#[]#)

This syntax joins all values in the Vendor token with line breaks, producing a cleanly formatted list of prospective.

8. Provide a Table of All Prospective Vendor Information

Use an Assign Field Values activity to put all the prospective vendor information into a table as metadata on a Vendor Guidelines document.

Vendor information is shown as metadata next to a document

Summary

By combining For Each activities with Assign Token Values and indexing, you can efficiently loop through collections, build complex data structures, and extract exactly the information you need. This approach ensures your workflows are modular, maintainable, and capable of turning raw data into actionable results.

How Each Component Works

For Each Entry

Runs the contained activities once for each entry in a collection.

Example: On the weekend, you want to rename and file all the receipts from the previous week. These receipts are saved in different departmental folders throughout the repository, but they all have the Receipt tag. Use a Search Repository activity to find the documents with the Receipt tag that were created in the last week. Place a Rename activity and Move Entry activity inside a For Each Entry activity. Point the For Each Entry activity to the collection returned by the Search Repository activity, and configure the Rename and Move Entry activities based on your desired naming and filing conventions. When the For Each Entry activity runs, each entry the search found will be renamed and moved.

Example For Each Entry configuration

For Each Row

Runs the contained activities once for each row returned by a Custom Query or Query Data activity, or for each result returned by activities that return collections of information.

Example: Each billing cycle, a workflow retrieves all the customers from the company’s database with a Query Data activity. Then a For Each Row activity with a Fill Out PDF Form activity inside uses the information from each row in the database to fill out individualized forms for each customer.

Example For Each Row configuration

For Each Value

Runs the contained activities once for each value in a multi-value token.

Example: An e-mail needs to be sent to each person who added their name to the Approver field on a project proposal. Workflow retrieves all the names in the field using the Retrieve Field Values activity. A For Each Value activity (configured to use the Approver token) has a Find User activity and E-mail activity inside it. The Find User activity looks up the user’s information based on For Each Value’s Current Value token and the E-mail activity sends an email using the FindUser_Email token as the email address.

Example For Each Value configuration

For Each File

Runs the contained activities once for each file attached to a Laserfiche Form or for each file at a specified location on the Workflow Server.

Example: When a Laserfiche Form is saved in the repository, its attachments also need to be saved. The Retrieve Laserfiche Form Content activity gets the attachments from the Laserfiche Forms Server and a For Each File activity with a Create Entry and an Attach Electronic Document activity inside can save the attachments to your repository. Configure the Attach Electronic Document activity to attach the file currently being processed by the For Each File activity to the entry created by the Create Entry activity.

Example For Each File configuration

For Each User

Runs the contained activities once for each user in a selected group.

Example: Every time an IT training occurs, each employee on the IT team needs to fill out a confirmation form. A workflow uses a For Each User activity (set to the IT group) with an E-mail activity inside to email each member of the IT team a link to the form to fill out.

Example For Each User configuration

Tokens Produced by For Each Activities

Each For Each activity produces tokens that resolve to information about the item currently being processed. Often, you’ll want to use the tokens created by the For Each activity to customize how the contained activities run.

Example: An email needs to be sent to each member of a group. An E-mail activity is placed inside a For Each User activity. In the To: field of the e-mail, put the %(ForEachUser_Current User_E-mail) token. Each time the For Each User activity runs, an email will be sent to the user currently being processed by the For Each User activity.

Leveraging the Assign Token Values Activity

The Assign Token Values Activity is especially powerful when combined with For Each activities as it allows you to create a single token that contains information from each iteration of a For Each Values activity.

Counting or Compiling Values Across Iterations

When counting items or compiling lists (for example, page counts or aggregating field values), first create a token with a blank value and then update it inside a For Each activity by appending values.

Example: For an upcoming meeting you want to compile a list of all proposed projects. First, you use a Search Repository activity to gather all the proposals. Then, you create a multi-value “Proposals” token with the Assign Token Values activity, but you do not assign it a value. In a For Each Entry activity, you use another Assign Token Values activity to append each entry’s name (using the %(ForEachEntry_CurrentEntry_Name) token) to the multi-value “Proposals” token. That token can be inserted into an e-mail announcing the meeting’s agenda.

Example Assign Token Values configuration

Making Loop-Generated Tokens Available Outside the Loop

Tokens produced by activities inside a loop (Repeat or For Each) are normally only available within the loop. Use an Assign Token Values activity inside the loop to copy a value to a new token whose last assigned value remains available after the loop completes.

Example workflow

Tips:

  • When you use an Assign Token Values activity to make a token available outside of a loop, the token will only contain the values assigned in the last iteration of the loop.
  • You can easily modify a token created by an Assign Token Values activity within the same workflow. Copy the Assign Token Values activity and paste it at the point you want to modify the created token. Right-click the token in the Tokens property box for the pasted activity, and select Change action to 'Modify'. The pasted activity will now modify the original token instead of creating a duplicate.
  • Within a single Assign Token Values activity, you can reference tokens created earlier in the same activity. Ensure the referenced token appears before the token that refers to it in the Tokens list.

Indexing and Joining Multi-Value Tokens

Multi-value tokens can be configured to return a single value by index or to return all values joined with a delimiter. You can do this either through the Token Editor (Apply Index and All values separated by options) or by editing the token syntax directly.

Selecting a Single Value by Index (Token Editor)

To select one value from a multi-value token, enable Apply Index in the Token Editor or Token Dialog and specify the index. Values are indexed in the order they were added (1-based).

Example: Three people must sign a document for it to be approved and for the workflow to continue. The multi-value token %(GetDocumentSignatures_Signer) lists the three people who signed a document: Bob, Sue, and Paul. Each value/person is given an index number (Bob=1, Sue=2, and Paul=3) based on the order in which they signed the document. To have the token be replaced by only the last person who signed the document (Paul), specify the index 3.

  • By default, indices count from the start of the list (1 = first value, 2 = second, etc.).
  • Switch the index basis from start to end to count from the end (1 = last value, 2 = second-to-last, etc.).
  • If counting from the start and the index is greater than the number of values, the last value is used. If counting from the end and the index is greater than the number of values, the first value is used.
  • If you enter characters other than an integer or token for the index, the setting changes to All values separated by and the characters become the delimiter.

Returning All Values with a Delimiter (Token Editor)

Select All values separated by and provide one or more characters to separate the values. Integers cannot be used as delimiters because they are reserved for indexes.