When creating a custom submit action in Sitecore Forms, all you need to do is to inherit the SubmitActionBase base class and override the Execute method, right? Wrong!
In this article, I want to share some gotchas when creating a custom submit action in the Sitecore Experience Forms.
Sitecore Forms, announced in the Sitecore Symposium 2017 and available at the Sitecore Experience Platform 9.0 Initial Release, comes with a completely redesigned UX that enables marketers and content authors to create forms from templates, create new forms by dragging and dropping Form elements such as textboxes, labels, lists, and etc. It promisses to completely replace the WFFM soon.
One of the features present in both WFFM and Experience Forms is the submit action (in WFFM they are called save actions) which are additional actions performed when a form is submitted. Altough, the Sitecore 9 – Update 1 brought a new submit action – Send Email Campaign Mesage – Experience Forms still lacks some very important submit actions that are present in the WFFM.
In the same manner WFFM enabled you to create custom save actions, Experience Forms also enables you to create custom submit actions. If you want to get started with custom submit actions please read the following walkthroughts:
- Walkthrough: Creating a custom submit action
- Walkthrough: Creating a custom submit action that updates contact details
- Sitecore 9 Forms: custom submit action
- Form Submit Actions in Sitecore 9
When creating a custom submit action in the Sitecore Forms, you inherit the Sitecore.ExperienceForms.Processing.Actions.SubmitActionBase class.
Below is the definition of the SubmitActionBase class:
All you need to do (theoretically) is to override the Execute method, right? Wrong!
Recently, I was struggling with the code below. I couldn’t figure out what was wrong with it.
Whenever I clicked the submit button in my website, I would get the error message that you set in the Error Message field of the custom submit action.
Investigating it a bit further, I found the reason why my code wasn’t working. It was because I was using the string type parameter in the SubmitActionBase, but wasn’t overriding the TryParse method.
It doesn’t happen if you use strongly typed parameter in the SubmitActionBase class.
Why does it happen?
If you decompile the Sitecore.ExperienceForms.Processing.Actions.SubmitActionBase class you’ll notice some interesting things.
Where does the “parameters” in the ExecuteAction come from? It comes from the submit action item that was created throught the Experience Forms Editor when you add a submit button.
How to fix it?
Below I list three alternatives to work this around.
1. Use a strongly typed parameter in the SubmitActionBase (recommended)
2. Override the TryParse method (recommended)
3. Add parameters to your submit action
The problem with this approach is that you have to remember to fill the parameter field whenever you create a new form and add the submit action to a submit button.
That’s all for today!