Workflow 8.3. I have created a multi-valued token containing "document creation date/document name" values. I need this token sorted before I send it in an email.
Question
Question
c# script to sort a multi-valued token
Answer
Hi Joyce! If you are stuck with the token-function-less 8.3 (which doesn't have the awesome token functions) you should be able to use this simple C# script to sort your token (just replace the name "MyToken" with your own token's name, keeping the quotes):
protected override void Execute() { // Write your code here. List<object> tokenValues = this.GetTokenValue("MyToken") as List<object>; tokenValues.Sort(); this.SetTokenValue("MyToken", tokenValues); }
Here we are just using the standard Sort method that .NET provides. From your description, your tokens are just simple strings and this should sort them as such. Keep in mind though, that you might want to also format the Dates and Times in such a way that they sort nicely (for example, use 02 for month rather than 1, since otherwise month 2 will sort before month 12). You can use token formatting for this.
By the way, this assumes that "MyToken" is a Multi-Value token that exists at that time, and it is stored as a List<object> (which multi-value tokens are). If you want to be extra meticulous you can add a null check in case the token might not exist at that time. This also makes the code do nothing if the token is single-value (which would likely be the behavior you want in that case).
protected override void Execute() { // Write your code here. List<object> tokenValues = this.GetTokenValue("MyToken") as List<object>; if (tokenValues != null) { tokenValues.Sort(); this.SetTokenValue("MyToken", tokenValues); } }
By the way, this is basically the code which the token function itself uses ;).
Replies
Hi Joyce,
In Workflow 9.0 functions were added to the token editor so that these sort of tasks could be accomplished, code free! Would you be able to implement an upgrade?