6

I have a (hopefully) simple question about creating a custom submit action.

My template for the submit action is /sitecore/templates/System/Forms/Submit Action and I only need to use the FormSubmitContext parameter in the execute method.

If the method signature looks like this then the execute method is never hit. The constructor is hit.

protected override bool Execute(string value, FormSubmitContext formSubmitContext)

My class derives from SubmitActionBase like so

public class SendContactEmail : SubmitActionBase<string>

If I change like so:

public class SendContactEmail : SubmitActionBase<RedirectActionData>

and method

protected override bool Execute(RedirectActionData data, FormSubmitContext formSubmitContext)

And add a reference to the existing redirect action in the core database in the editor field then my execute method will be hit.

enter image description here

To simply my question the constructor IS being hit so I can see that sitecore is instantiating the class but the execute method is not being called possibly because the string parameter is not being passed

1
  • 1
    You need to override TryParse too
    – Edwin
    Commented May 25, 2018 at 18:37

3 Answers 3

10

The reason your Execute() method is not being hit is due to the use of string parameter type of your SubmitActionBase. There is a check in this base class if there parameters passed from your Submit Action item are empty, and if so the action is not executed.

The solution depends on what your Submit Action is doing.

1.Override TryParse method

If you are not passing any parameters to the Submit Action from the item, then you can override the TryParse method and return true. The Execute() method will then be carried out as expected:

public class CustomSubmitAction : SubmitActionBase<string>
{
    public CustomSubmitAction (ISubmitActionData submitActionData) : base(submitActionData)
    {
    }

    protected override bool Execute(string data, FormSubmitContext formSubmitContext)
    {
        // your custom logic
        return true;
    }

    // This is the problematic method which needs to be overriden
    protected override bool TryParse(string value, out string target)
    {
        target = string.Empty;
        return true;
    }
}

2. Use strongly typed parameter for your base class

If your custom submit action requires parameters passed from the item then it is recommended that you use strongly typed objects. There's no need to override the TryParse method in this case.

public class CustomSubmitAction: SubmitActionBase<CustomClass>
{
}

public class CustomClass
{
    public Guid FirstNameFieldId { get; set; }
    public Guid LastNameFieldId { get; set; }
    public Guid EmailFieldId { get; set; }
}

3. Set parameters in your Submit Action

Or set the values in the Parameters field of the Submit Action. It is not recommended to rely on this method, since you need to ensure that the parameter is added every time the Submit Action is used.

Submit Action

You can read more in this blog post by Joao Neto. Also also note that the TryParse() method is overridden in the official Sitecore walkthrough for creating a Submit Action.

2
  • 1
    Thanks jammykam I think option one would have solved it.
    – JKerny
    Commented Aug 30, 2018 at 5:29
  • No problem, hopefully it will help someone in the future.
    – jammykam
    Commented Aug 30, 2018 at 12:05
4

I got this to work using a different approach. I implemented ISubmitAction and the ExecuteAction() method.

I then add an FormActionError.ErrorMessage if the submit action fails.

I hope this helps someone. I did test with and without specifying the assembly name as mentioned by Peter but believe if you are using Helix you do need it.

2

Followed the same steps as its described in the below link, but debug is hitting only at the LogSubmit constructor not in the Execute. any idea on how to resolve it. I'm using sitecore 9.0.2

https://doc.sitecore.net/sitecore_experience_platform/digital_marketing/sitecore_forms/setting_up_and_configuring/walkthrough_creating_a_custom_submit_action

Update: This is how I resolved, there was an issue in the form Submit action order, my custom action(LogSubmit) is added after the redirect to Page task, So every time before my custom action, redirect to Page is triggered. So rearranging the Action Worked for me. See the below screen shot

Initially:

enter image description here

Fixed:

enter image description here

1
  • Try how I solved it. Otherwise de-compile one of sitecore's submit action's and have a look how they do it. If you look at the submit actions in the content editor they have the namespace so it's easy to find in the forms dll
    – JKerny
    Commented Aug 7, 2018 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.