How to use a regex pattern in the Mapper action’s 'replace' function
Mapper provides a number of in-built functions that lets you create custom outputs with minimum time and efforts.
There are times when you may need to use the same custom outputs repeatedly with slight text modifications. In such cases, you can use regex pattern in the Mapper action’s 'replace' function.
Let's say you wish to create a workflow that triggers if the body of the new mail contains the word 'San Francisco' and replaces it with 'Sacramento' and send it to Cisco Webex Teams space. To do this, follow the instructions given below:
Add and configure 'Gmail - New Mail' Trigger
Add 'Gmail - New Mail' trigger and configure it as given below:
Click on 'ADD' link to define filters for this trigger.
Input: Add 'bodyText' field given on the right hand side of the trigger configuration window as an input for this field.
Condition: Select 'Contains' option from the drop down field.
Expected: Enter 'San Francisco' as an input for this field.
Once you have entered these details, click on 'Save'.
This will trigger the workflow if the body of the incoming new email contains the word 'San Francisco'.
Step 2. Add Actions
Add 'Mapper' and 'Cisco Webex Teams - Post New Message' actions to canvas and connect them as shown below:
Step 3. Configure 'Mapper' Action
Configure 'Mapper' action as given below:
Click on the '+' icon to add inputs in Mapper. A new key-value field will appear.
Key: You can change the name of the 'key' field, as per your requirements.
Value: Since we will be using the trigger data as an input for Mapper, you need not enter any value for the key.
Now, we need to replace the word 'San Francisco' with 'Sacramento' in the body of the email. To do this, first click on the 'Console' icon (</>) given beside 'Value' field to add custom logic on the input.
A new console window will appear. Click on the 'Collapse Window' icon to see the trigger data.
Click on the drop down icon given beside 'Functions' tab and select '_.replace' function listed under 'String'. The syntax for 'replace' will appear on the console window.
_.replace([string=''], pattern, replacement)
'string' refers to the source input string
'pattern' refers to the simple/regex pattern to be checked against the source input string
'replacement' refers to the keyword to be replaced with the text that matches the specified pattern.
In our example, since we need to check the body of the email for the word 'San Francisco', add 'bodyText' key given on the right hand side of the console window, as the 'String' and enter the regular expression '/san francisco/gi' as the pattern, where 'g' denotes global matching and 'i' denotes case-insensitive matching.
Next, we want to replace the word 'San Francisco' with 'Sacramento'. So enter the word 'Sacramento' as the 'replacement'. Right now your replace function code will look like this:
_.replace($trigger.bodyText, /san francisco/gi, "Sacramento");
Since we need to return the result of this function as the output of Mapper action, add 'return' before the given function. So your final function code block will be,
return _.replace($trigger.bodyText, /san francisco/gi, "Sacramento");
Once you have written the custom code, click on 'Done'. This will take you back to 'Mapper' configuration window where you can see the 'Custom Function' tag in the 'Value' field.
Click on 'Next', test the action and then click on 'Done'. This will take you back to canvas.
Step 4. Configure 'Cisco Webex Teams - Post New Message' Action
Configure the 'Cisco Webex Teams - Post New Message' action as given below:
1. Authorize Cisco Webex Teams: Select the Cisco Webex Teams authorization.
2. Space ID: Select/specify the ID of the space on which you wish to post the message.
3. Message: Add the 'Key' listed under Mapper output given on the right hand side of the configuration window.
Click on 'Next', test the action, and click on 'Done'. This will take you back to the canvas. Then click on the 'Run' button located at top right hand-side corner of the canvas to run the workflow.
This will replace the word 'San Francisco' with 'Sacramento' from the body of an email and will then send it to the specified space on Cisco Webex Teams.
You can learn more about Mapper functions here.