Walkthrough: Creating a custom submit action

Last updated Thursday, November 2, 2017 in Sitecore Experience Platform for Developer
Keywords: Configuration, Forms

To submit a form, a contact must click the Submit button. You can add different types of actions to perform when a user clicks Submit. By default, you can add the Trigger Goal, Trigger Campaign Activity, Trigger Outcome, Redirect to Page, and Save Data submit items.

This walkthrough describes how to create a custom submit action that adds an entry to the log files about the form's submit status.

This walkthrough describes how to:

Create a submit action class

When you create a custom submit action, you must create a class that inherits from the SubmitActionBase<TParametersData> class.

To create the submit action class:

  1. Create the LogSubmit class and inherit from SubmitActionBase<TParametersData>.
  2. Specify the TParametersData type.
  3. Override the TryParse method to enable parsing empty string parameter and return true.
  4. Override the Execute method and log a message that contains the form submit status and the form id. For example:
    using System.Linq;
    using Sitecore.Diagnostics;
    using Sitecore.ExperienceForms.Models;
    using Sitecore.ExperienceForms.Processing;
    using Sitecore.ExperienceForms.Processing.Actions;
    using static System.FormattableString;
    namespace Sitecore.ExperienceForms.Samples.SubmitActions
    {
        /// <summary>
        /// Executes a submit action for logging the form submit status.
        /// </summary>
        /// <seealso cref="Sitecore.ExperienceForms.Processing.Actions.SubmitActionBase{TParametersData}" />
        public class LogSubmit : SubmitActionBase<string>
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="LogSubmit"/> class.
            /// </summary>
            /// <param name="submitActionData">The submit action data.</param>
            public LogSubmit(ISubmitActionData submitActionData) : base(submitActionData)
            {
            }
            /// <summary>
            /// Tries to convert the specified <paramref name="value" /> to an instance of the specified target type.
            /// </summary>
            /// <param name="value">The value.</param>
            /// <param name="target">The target object.</param>
            /// <returns>
            /// true if <paramref name="value" /> was converted successfully; otherwise, false.
            /// </returns>
            protected override bool TryParse(string value, out string target)
            {
                target = string.Empty;
                return true;
            }
            /// <summary>
            /// Executes the action with the specified <paramref name="data" />.
            /// </summary>
            /// <param name="data">The data.</param>
            /// <param name="formSubmitContext">The form submit context.</param>
            /// <returns>
            ///   <c>true</c> if the action is executed correctly; otherwise <c>false</c>
            /// </returns>
            protected override bool Execute(string data, FormSubmitContext formSubmitContext)
            {
                Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));
                if (!formSubmitContext.HasErrors)
                {
                    Logger.Info(Invariant($"Form {formSubmitContext.FormId} submitted successfully."), this);
                }
                else
                {
                    Logger.Warn(Invariant($"Form {formSubmitContext.FormId} submitted with errors: {string.Join(", ", formSubmitContext.Errors.Select(t => t.ErrorMessage))}."), this);
                }
                return true;
            }
        }
    }
  5. Build and deploy.

Create a submit action item

To create the submit action item:

  1. Navigate to /sitecore/system/Settings/Forms/Submit Actions
  2. Right-click Submit Actions, click Insert, and click Insert from template.
  3. Select the /System/Forms/Submit Action template, in the Item Name field, enter the name Log Submit and click Insert.

    Picture 1

  4. Navigate to the item you just created and in the Settings section, in the Model Type field, set the value to the class type name.

    Picture 6

  5. In the Error Message field, enter an error message, for example, Log Submit Failed!
  6. In the Appearance section, select the icon that you want to display in the Forms elements pane.

In the Form elements pane, when you click add a submit action, you can now select the Log Submit action:

Picture 2

Send feedback about the documentation to docsite@sitecore.net.