Skip to main content

Substrings in Power Automate

Substrings

Today we will look at an example, how to extract a substring from a given string. Our aim is to extract the file extension of a note (attachment in CDS) from the mime/type field.

Single vs. multiple records

First we fetch our underlying note using either the get record or the list records CDS action:


Depending on the action you choose, the formulas below differ, since "Get record" returns just one single record, while "List records" can return multiple records.

For a single record you will reference your fields like outputs('<Name of your action>')?['body/<'name of your field'>'].

While for multiple records you will have to put your actions into an apply to each "group" and will reference your fields like items('<name of your apply to each action>')?['<fieldname>'].

Extract substring

Once we have added an action to fetch our underlying record:

we are ready to define 4 compose actions, that will result in our substring.

Our starting point is the field "mimetype" of a note --> outputs('GetNote')?['body/mimetype'] and it contains the value "image/png". We want to extract the file extension "png" from this string.

First we have to define the starting point of our substring, by adding a compose action to our flow:


and adding the following formula as an expression:

add(int(indexOf(outputs('GetNote')?['body/mimetype'],'/')),1)


Since we will reference this action in the further steps, we give it a meaningful name and call it StartSubString:


Then we add a second compose action to find out the lenght of our mime/type string, using the following formula:

length(outputs('GetNote')?['body/mimetype'])


In our third compose action we calculate the difference between the length and our starting point, to find out the length of our substring, using following formula:

sub(outputs('LengthMimeType'),outputs('StartSubstring'))


Finally we add a last compose action to put our substring together:

concat('.',substring(outputs('GetNote')?['body/mimetype'],outputs('StartSubstring'),outputs('LengthSubString')))

Result

Lets perform a test run and have a look at our result:


Comments

Popular posts from this blog

Power Automate Get absolute Sharepoint URL from document location

Document Locations If you enable Sharepoint document integration in your model driven apps, you can define for which entities the possibility to upload documents to sharepoint shall be offered and you can define a folder structure. Possible folder structures can either be based on account or contact, which means if you create child items (e.g. opportunities, activities, ...) sub folders are created below. If an entity is enabled for document management a "Files" tab is added to the form of your model driven app and as soon as you click on that tab a folder is automatically generated on Sharepoint. Once the folder has been created you have options to create new or upload existing file or to open the document location (folder) on Sharepoint: What happens behind the scenes is, as soon as you click on the files tab your model driven app checks, weither a "document location" for this item already exists, or not. The document location is an own entity in C

Working with Optionset lables in Power Automate

Connector Types Power Automate currently offers 3 different connectors, to connect Power Automate with your Dynamics 365 instance: Dynamics 365 Common Data Service Common Data Service (current environment) Today we will look on ways how to retrieve option set labels via the different connectors, for our sample accounts: Dynamics 365 This connector should not be used any more , since in April 2019 it has been announced as being deprecated. https://flow.microsoft.com/en-us/blog/nine-new-connectors-in-april-and-improved-visibility-into-trigger-failures/ With the Dynamics 365 connector it was quite easy to work with optionsets, since it offered both the optionset value (int) and the optionset label (name) in the dynamic content: And here we go, these are our results: Common Data Service Since the Dynamics 365 connector has been deprecated, this is the connector you should use. If you perform a "Get record" or "List records&

Power Automate - Records modified during the last 30 minutes

Query operators Recently I have been working on a requirement to query accounts and contacts that have been modified during the last 30 minutes in Power Automate. Out of the box only the following time based filters are offered: https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/use-fetchxml-fiscal-date-older-datetime-query-operators So as you can see a filter "Older than X minutes", but no filter "Last X minutes" is offered. Also the "On or After" filter only allows as to provide a date and not the time part into the filter: So we have to be a bit creative, to solve this challenge. Compose - Date Time in Power Automate As a first step we use the Compose action and provide the following expression to calculate the current date/time minus 30 minutes: And now it is time to define our filter expression to query all accounts, modified during the last 30 minutes: Lets prepare some test data and modify one a