Why my custom submit action doesn’t work?

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.

WFFM Sitecore Forms Comparison Updated
Figure 1: Submit Actions available on Sitecore Forms (left) and WFFM (right)

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:

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.

Error message field.PNG
Figure 2: Error Message field

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.

where is the overrided
Figure 3: When using string type you need to override the TryParse

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.

SubmitActionBase.png
Figure 4: SubmitActionBase class decompiled

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.

parameters
Figure 5: Parameters field

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.

parameters filled in.PNG
Figure 6: Parameters field with value

That’s all for today!

References:

  1. 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].
  2. 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].
  3. 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].
  4. 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].

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s