Plant An App has a lot of built in functionality to enable you to do exactly what you want in its Low-Code environment. Using both tokens and actions you can often get the results you want, but what if you are having issues producing just what you want to happen or maybe Plant An App can do it, but you want to do it more concisely? Let me introduce you to the Execute Razor Action.

The Execute Razor Action lets you build a standard razor template using C#, which means you can do about anything you want in it. If you are a developer at your core (like me), then you will feel right at home with this action, but be careful, you may develop the habit of always using the Execute Razor Action when other more structured actions would be more appropriate to use. (As well as easier for a low-code engineer or business logic member of your team to understand). When needed though, the Execute Razor Action is very powerful and handy to have in your toolbox.

In this example I’m going to create a very basic Action Form that consists of a text box field (Name) where the user will enter their name, a static text field (Greeting) where the user will see a greeting, and a button that will run the code to populate the greeting message.

This is a very simple example which could easily be implemented without the Execute Razor Action and instead using a condition on the Execute Token Action to override a default token with a name, but hopefully this simple example will help to illustrate the basics of the Execute Razor Action.

When the form is displayed initially to the user it looks this. The name field is not required. If the user enters their name and clicks Run they will be greeted with a customized message.

If the user chooses to leave the name field blank they well be greeted with the message “Hello World!”

There are just two actions behind the Run button, the Execute Razor Action and an Update Form Data Action that will refresh the Static Text field on our form to display the greeting.

Let’s drill down into what the Execute Razor Action is doing. It has the standard fields that you would expect, Description, Error Message and Condition which function just like they do for the other actions in the Plant An App ecosystem. The first field specific to the Execute Razor Action is the script name. I’m not 100% sure why Plant An App just doesn’t generate this itself, but in any case, it is needed by the system to compile the razor template so I tend to just name it the same as my description without the spaces.

The next section, Local Variables, allows you to define variables that you want available to you in your code. I rarely use this, because all your tokens (excluding entity lists) are available to you automatically so I tend to have everything setup that I want access to before I even get to this point.

After that is the Additional Assemblies section. This allows you to provide access to other libraries of code that aren’t already available to Plant An App. The usage of this field is beyond the scope of this post, but it can come in handy when you need some extra functionality or already have some business logic in a library from another application.

I’ll skip the code section for now, which means the last field down at the bottom is the Store Result field which is just the name of the token you want to store your output from the razor into. In my case, once the action has completed, I will have my greeting in the token [Greeting].

Finally, let’s tackle the razor code section. Razor is a great templating engine and it was originally built to generate html output for Microsoft’s MVC web development platform. Since its creation it has been repurposed for lots of different templating needs. I myself have used it may times for creating email templating systems. Code execution, C# in this case, happens after the @ sign. It can either be a line or it can be a block of code inside curly braces.

@{
    String message = "";
  
    if(String.IsNullOrWhiteSpace(Name)){
        message = "Hello World!";
    }
  else{
     message = String.Format("Hello {0}!", Name);
  }
}
@message

In this example code, we start out by creating a variable called message and initializing it to an empty string; Now keep in mind, I said we have access to all the tokens as variables so in the next line we have the variable Name which corresponds to the token [Name] that Plant An App created from our Name field, which contains whatever the user entered. We use the Name variable as an input to the C# function String.IsNullOrWhiteSpace which returns true if Name is an empty string or just contains spaces/tabs (i.e. the user entered no data or blanks). If the user actually entered their name, then we get false. That true or false is passed to the if statement to decide which branch of the if we take.

If we received no user input and the result was true, we drop into the first section and message is set to “Hello World!”.

 message = "Hello World!";

If, on the other hand, the user did enter their name, then the result is false, and we drop into the second code block which sets message using a string format function, inserting the Name variable (remember this has our value from the Name field in it) after Hello, producing, in the case of my example at the start of this post, “Hello David!”

message = String.Format("Hello {0}!", Name);

The last piece is @message. This makes the last thing the code does is “output” the contents of the variable message which is then stored in our defined result token name [Greeting].

That’s it and if you already know C# then you are probably already thinking about all the ways you can now use the Execute Razor Action. I will caution you once more, there are lots of different actions available to you in Plant An App that can help you to do things in a way that makes the logic readable to other low-code engineers that aren’t hardcore developers. That doesn’t mean don’t use the Execute Razor Action, that just means use it when it is the best solution for the task at hand and it will be a powerful tool for you.

Hopefully you were able to learn a bit about the Execute Razor Action that you didn’t already know. As always, I only know what I know, and don’t know what I don’t know, so if you have something to add on the topic feel free to reach out via email or in the comments below. Let me know if you’d like me to dive deeper into more functionality of the Execute Razor Action, Plant An App topics in general, or other things related to software development and technology.

Need help with a software solution for you business? Contact Us

Comments are closed