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!
- Core Blimey! – Sitecore Blog. (2017). Form Submit Actions in Sitecore 9. [online] Available at: http://www.coreblimeysitecore.com/blog/submit-actions-sitecore-9/ [Accessed 6 Mar. 2018].
- Doc.sitecore.net. (2018). Sitecore Documentation: Walkthrough: Creating a custom submit action that updates contact details. [online] Available at: https://doc.sitecore.net/sitecore_experience_platform/digital_marketing/sitecore_forms/setting_up_and_configuring/walkthrough_creating_a_custom_submit_action_that_updates_contact_details [Accessed 6 Mar. 2018].
- Doc.sitecore.net. (2018). Sitecore Documentation: Walkthrough: Creating a custom submit action. [online] Available at: https://doc.sitecore.net/sitecore_experience_platform/digital_marketing/sitecore_forms/setting_up_and_configuring/walkthrough_creating_a_custom_submit_action [Accessed 6 Mar. 2018].
- Gullentops, G. (2017). Sitecore 9 Forms: custom submit action. [online] Ggullentops.blogspot.nl. Available at: https://ggullentops.blogspot.nl/2017/10/sitecore-9-forms-custom-submit-action.html [Accessed 6 Mar. 2018].