Sitecore WFFM – Custom Save Action with Field Mappings

If you need to create a custom save action using Web Forms For Marketers and you need to map those fields to a specific model, it is possible using Field Mappings.

This tutorial will teach you how you can easily map al your form fields with a model, specially when you are working on a third-party integration.

The first step is to create a custom submit action item. Go to  /sitecore/system/Modules/Web Forms for Marketers/Settings/Actions/Save Actions, right click and select  Insert > “Save Action”. We will need to specify the Assembly and Class which will handle the functionality of the button. Take a look at the image below for reference.

2016-02-25 17_35_30-Desktop
Sitecore Item: Custom Submit Action

The next thing we should do in order to establish a relationship between your model properties and the form fields is to fill the QueryString field (Save Action item) with the model properties. Please, take a look at the image below which describes how it should be set:

2016-02-25 19_21_17-Desktop

The editor fields will tell the Form Designer to open a specific control to edit the save action. In this case we are telling Sitecore to open the dialog MappingFields when the content editors wants to edit our custom action.

The format to add a QueryString is: fields=FieldName|FieldDisplayText. Remember, the same fields you add in the QueryString field should be the same you add in your model as properties. In this sample I will add three fields:

fields=FirstName|First Name,LastName|Last Name,EmailAddress|Email Address.

After applying these changes to the custom save action, let’s create the class that will handle the custom action. Since our class, CustomSubmitAction, it must implement ISaveAction interface. In the Execute method will add the functionality once the user clicked the submit button. Please see the code below for reference:


namespace Sitecore.Custom.Web.Configuration
{
    public class CustomSubmitAction : ISaveAction
    {
        #region Properties

        public string EmailAddress { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
 
        #endregion
        
        public void Execute(ID formid, AdaptedResultList fields, params object[] data)
        {
            Assert.ArgumentNotNull(fields, "fields");

            var model = GetFieldMappigns(fields);

            ...
	    your code
	    ...
        }

        /// <summary>
        /// Gets the field mappigns.
        /// </summary>
        /// <param name="fields">The form fields.</param>
        /// <returns></returns>
        private Model GetFieldMappigns(AdaptedResultList fields)
        {
            var model= new Model()
            {
                EmailAddress = GetValue(this.EmailAddress, fields),
                FirstName = GetValue(this.FirstName, fields),
                LastName = GetValue(this.LastName, fields)
            };

            return Model;
        }

        private string GetValue(string formFieldId, AdaptedResultList fields)
        {
            if (string.IsNullOrWhiteSpace(formFieldId))
            {
                return null;
            }
            if (fields == null || fields.GetEntryByID(formFieldId) == null)
            {
                return null;
            }
            return fields.GetValueByFieldID(formFieldId);
        }
    }
}

Done! Now we have a custom save action so let’s build a  sample form in order to see this feature live!

We will go to the Form Designer and create three fields, see the image below:

2016-02-25 19_24_06-Desktop

Then, we need to edit the Save Actions in order to add our new custom item. Click “Edit” in the “Save Actions” section and it will display a dialog where you can select the “Custom Action” item that we’ve recently created. Then we need to edit the item:

2016-02-25 19_27_56-Desktop

After we click on “Edit” button, it will open an Editor dialog where we can map each model properties with each form fields. As you can see in the image below, the fields in the right pane will be the ones you defined in the QueryString field (sames as your Model) and the dropdown will contain all the form fields.

2016-02-25 19_31_10-Desktop

Click Ok and save changes.

The fields we selected to match will be populated dynamically in the custom action class.

I had to decompile the Sitecore.Forms dlls in order to understand how the MappingFields control works.

If you have any advise how to improve this mechanism, let me know!

See you in another chapter 😉

Happy Sitecoring!

One thought on “Sitecore WFFM – Custom Save Action with Field Mappings

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 )

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