Removing Carriage Returns (\r) and Line Feeds (\n) in Power Automate

In Power Automate, working with text data often requires cleaning up special characters such as carriage returns (\r) and line feeds (\n). These characters can appear in data retrieved from external sources, especially when the data has been URL-encoded. URL-encoded characters for carriage returns and line feeds are represented as %0D and %0A respectively. Power Automate provides a function decodeUriComponent() that can decode these representations back to their original characters, which can then be effectively removed from strings.

How to Remove Encoded Carriage Returns and Line Feeds

The process involves two main functions: decodeUriComponent() to decode URL-encoded characters, and replace() to remove them. Here's a step-by-step guide on how to implement this in Power Automate:

Step 1: Decoding and Removing \r

To remove URL-encoded carriage returns (%0D):

  1. Decode and Split: Use decodeUriComponent('%0D') to decode the encoded carriage return.

  2. Replace the Character: Use the replace() function to replace the decoded carriage return with an empty string:

    replace( string(your_data_here), decodeUriComponent('%0D'), '' )

Step 2: Decoding and Removing \n

To remove URL-encoded line feeds (%0A):

  1. Decode and Split: Use decodeUriComponent('%0A') to decode the encoded line feed.

  2. Replace the Character: Following the replacement of \r, apply the replace() function again to remove \n:

    replace( previous_step_output, decodeUriComponent('%0A'), '' )

Implementing in Power Automate

Here is a practical example using Power Automate expressions to remove both characters from a string retrieved from a label field in Dynamics 365:

if( empty(string(body('Get_Province_Label')?['new_province@OData.Community.Display.V1.FormattedValue'])), '', replace( replace( string(body('Get_Province_Label')?['new_province@OData.Community.Display.V1.FormattedValue']), decodeUriComponent('%0D'), '' ), decodeUriComponent('%0A'), '' ) )

Conclusion

Using decodeUriComponent() in conjunction with replace() offers a robust solution for cleaning up URL-encoded text in Power Automate. This method ensures that data handling processes are streamlined and error-free, enhancing the reliability of automated workflows.

No comments:

Post a Comment