StringBuilder
Sometimes, the messages that you need to create can be lengthy and complicated. Creating message content dynamically based on information collected from different sources often requires complex logic. One option for building messages is to use StringBuilder.
To learn more about StringBuilder, please refer to the Microsoft StringBuilder documentation.
StringBuilder Example
This StringBuilder example demonstrates how to formulate an XML message to be sent to a demo REST endpoint.
If you have a Frends Environment and SQL database in use, it is recommended that you create a similar Process and test it.
Learning by doing is often the best way to learn!
Overview of the Example Process
The Process is straightforward. The first Task queries product data from the SQL database. The next Element sets the variable for the StringBuilder. The Foreach Element loops over the product data queried from SQL and creates an XML message for each product. The Process flow branches from the Exclusive Decision Element based on the product's color. If the color value is null, the message will contain different content from products with a color value set. The XML message is sent to the REST endpoint using the POST method. Finally, the last Element in the Foreach loop clears the StringBuilder variable before moving on to the next product.
Element Configuration
Below are screenshots of each of the Element's configurations.
Microsoft ExecuteQuery:
In the example Process, the Microsoft ExecuteQuery queries only two products from the table. The connection string is stored in an Environment Variable and is hidden. Next to the configuration illustration, you can find the query result with product details.
Microsoft ExecuteQuery Frends Task GitHub page.
Set targetMessage
In this Expression Element, the variable targetMessage is initialized.
Foreach product
The Foreach Element loops over the two queried products.
Append to targetMessage
This Code Element creates the first part of the XML message. Note the top-level XML Element and the rest of the XML message structure.
No product color?
Some products have a Color value of null, and some have a Color value of Red, Green, Blue, etc. This Exclusive Decision Element branches the execution based on the Color attribute value.
Append no color detail
If the Color value is null, this part of the message is appended to the targetMessage.
This Code Element also closes the message's element.
Append color detail
In case the Color value is other than null, this part of the message is appended to the targetMessage.
This Code Element also closes the message's element.
Send XML message
Web RestRequest Task Frends GitHub page.
Clear targetMessage
This Expression Element clears the targetMessage variable for the next iteration of the Foreach.
Execution of Example Process
Below are screenshots of the Process Instance.
You can see in the lower left-hand corner of the Foreach Element that the loop has run two times, as there were two product details queried from the database. Check the XML message content and compare the result with the Send XML Message Task to see the difference in the message.
Results when Color value is null
Result of Send XML Message Task
Results when Color value is other than null
Result of Send XML Message Task
The next article is Introduction to Environment Variables.
β