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:
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
Post a Comment