In this article, we will learn and explore the IndexOf function in Power Automate flow with 10 real-time examples using SharePoint Online as a data source. Power Automate, formerly known as Microsoft Flow, is a powerful automation tool that allows users to create workflows to automate various business processes. One of the essential functions within Power Automate is the IndexOf function, which helps in searching for a specified value within a string or an array and returns the position of the first occurrence.
In this article, we will dive into the IndexOf function and provide 10 real-time examples of how to utilize this function using SharePoint Online as the data source.
IndexOf Function Examples in Power Automate: 10 Real-time Examples with SharePoint Online
Before getting into the examples, let’s understand the syntax of the IndexOf function in Power Automate:
Syntax and how to use the IndexOf function in Power Automate
Basically it follows a simple pattern.
- Text: This is a text input.
- String to search: This is a character or string you want to search in the text input.
Create a sample manually triggered flow with two input boxes for text input and search.
The IndexOf function will look like below:
Enter the text “This is an index of a function demo” in the first box and “an” in the second box, then run the flow, and we will see the below output.
The output will be 8.
Real-Time Examples with SharePoint Online
We will demonstrate various scenarios where the IndexOf function can be used with SharePoint Online as the data source.
Example: Finding the Position of a Substring in a Text Field
Suppose we have a SharePoint List or Document Library with a ‘Remark‘ field, and we want to find the position of the word ‘important‘ within this field.
Then, the IndexOf formula will be as below:
This will give output as “11“.
Example: Checking for Specific Text in a SharePoint List Item
We can utilize the IndexOf function to check if a SharePoint list item’s ‘Title‘ field contains a specific keyword.
This will give -1 as output if it is not found.
Example: Checking if a String Contains a Substring
To verify if a string contains a specific substring, we can use the IndexOf function.
strSampleString=’This is a sample string’
Example: Locating the First Occurrence of a Character
To find the position of the first occurrence of a character in a string:
Text: ‘Hello, World!’
Search text: ‘,’
Index Output: 5
Example: Searching for a Value in an Array – IndexOf Array
If we have an array of strings and we want to find the position of a specific value:
Index: 2 (Assuming ‘searchValue’ is at index 2 in the array)
[ "Apple", "Banana", "Cherry", "Date", "Apple" ]
Expression: indexOf(variables('stringArray'), 'Banana')
If ‘Banana’ is not found in the array, it will give output as -1.
Example: Identifying the Index of a SharePoint List Item
To get the index of the current SharePoint list item being processed in a loop:
Expression: indexof(outputs('Compose'),items('Apply_to_each')['ID']) Index: 0 (Assuming 'ID' matches the first element in the 'Compose' output)
Example: Checking for a Specific Email Domain
If we have an email address and want to check if it belongs to a specific domain:
indexOf('firstname.lastname@example.org','@gmail.com') Output: 7
Example: Checking for Leading Zeroes in a Number
To determine if a number has leading zeroes:
Best Practices for Utilizing the IndexOf Function in Power Automate Expressions
Working with the IndexOf function in Power Automate requires attention to best practices to ensure efficient and effective workflow development. Here’s a list of best practices:
- Clear Understanding: Gain a clear understanding of the input expression and value before using the IndexOf function.
- Use of Variables: Do not pass the string value directly inside the IndexOf function; rather, create a variable, assign the value there, and use it in your indexof function. For example, if you want to directly pass the string value inside the index of the function, you need to single quote your string, like ‘My test string’, but if you use it using a variable, then it is not needed.
- Keep the string short: Try to keep the string value as short as possible. In your complex logic, try to make the string as short as possible; otherwise, it will break or fail.
- Error Handling: Implement proper error handling to manage cases where the value is not found (e.g., -1 for not found).
- Input Validation: Ensure the input expression is of the expected data type (string or array) and validate it accordingly.
- Parameterization: Parameterize the value being searched to allow for flexibility and reusability of the workflow.
- Testing and Debugging: Thoroughly test the IndexOf function with various scenarios to ensure it behaves as expected and debug if needed.
- Use Expressions Wisely: Leverage expressions appropriately within the function to build dynamic searches based on your requirements.
- Commenting: Add comments to explain the purpose and usage of the IndexOf function within your workflow for future reference and collaboration.
- Consistent Naming Conventions: Adhere to a consistent naming convention for variables, actions, and outputs related to the IndexOf function for better code readability.
- Documentation: Maintain comprehensive documentation outlining the purpose, usage, and examples of the IndexOf function in your workflow.
- Optimize Performance: Consider the performance implications of using the IndexOf function, especially when working with large arrays or strings.
- Resilience: Ensure that your workflow is resilient to changes in the data structure or source by handling possible errors or exceptions gracefully.
- Version Control: Utilize version control practices to keep track of changes made to the workflow involving the IndexOf function.
- Reuse and Modularity: Encourage reuse of the IndexOf function in other parts of the workflow or in different workflows for better modularity and efficiency.
- Security and Privacy: Be mindful of any sensitive data involved in the IndexOf function and follow security and privacy best practices.
- Performance Optimization: Optimize the usage of the IndexOf function to minimize unnecessary calls and improve the overall performance of the workflow.
- Knowledge Sharing: Foster knowledge sharing within your team or community regarding best practices and use cases of the IndexOf function in Power Automate.
These best practices will help you effectively utilize the IndexOf function in Power Automate while ensuring maintainability, performance, and reliability of your Power Automate flows.
The indexOf function in Power Automate handles strings in case–insensitive ways, which means ‘MY TEST STRING’ and “my test string” are both valid.
Summary: IndexOf Function in Power Automate
Thus, in this article, we have learned about the IndexOf function in Power Automate along with some real-time examples with SharePoint Online data, and we have also learned about the best practices we should follow while working with the IndexOf function in Power Automate.
See Also: Power Platform Articles
You may also visit the Power Platform article hub, where you will see a bunch of articles focusing on Power Platform, like Power Automate, Power Apps, etc. All the articles are written with real-time project scenarios and troubleshooting techniques.