Jonathan Robbins
Jonathan Robbins

Reputation: 2047

WFFM Save Action get Field Mapping from Editor

I have created a custom Save Action which will write WFFM Field values to a 3rd party service. The custom Save Action uses the out-of-the-box FieldMappings Editor so that the Content Editor can specify which Fields map to what properties which is sent to the service.

I have it working so all the properties appear in the Editor for the User to select the relating Field.

enter image description here

The issue is that I can't find how to get those mappings at point of the Execute method of the Save Action. I've decompiled the existing Tell a Field Save Action as it also uses the MappingField editor but it ultimately ignores the mapping.

public class SaveToSalesForceMarketingCloud : ISaveAction
{
    public string Mapping { get; set; }

    public void Execute(ID formid, AdaptedResultList fields, params object[] data)
    {
        FormItem formItem = Sitecore.Context.Database.GetItem(formid);
        if (formItem == null)
            return;

        string mappingXml = Mapping;

        // Using the Property Name does not return the Mapped Field
        var emailAddressField = fields.GetEntryByName("Email address");
        // Using the actual name of the Field on the Form returns the Field
        var emailField = fields.GetEntryByName("Email");
    }
}

Anyone know how to get the mapping?

Upvotes: 3

Views: 1332

Answers (2)

jammykam
jammykam

Reputation: 16990

The mapping is stored as a key/value pair in the Save Action field of your Form, which is then populated into the Mapping property which you have defined.

Check the Save Field of your Form and you will notice that the format of the string is similar to <mapping>key=value1|key=value2</mapping>. This is the string value which you have available in your save action. You need to handle it yourself, WFFM will not wire anything up for you. In order to access the mappings, you use the Sitecore util method:

NameValueCollection nameValueCollection = StringUtil.ParseNameValueCollection(this.Mapping, '|', '=');

This gives you access to the key/value pairs. You will then need to enumerate either these fields or the submitted form data (as appropriate) to populate your objects for further action.

Assuming the key is the WFFM Field ID and value is the field to map to, something similar to this

foreach (AdaptedControlResult adaptedControlResult in fields)
{
    string key = adaptedControlResult.FieldID; //this is the {guid} of the WFFM field
    if (nameValueCollection[key] != null)
    {
        string value = nameValueCollection[key]; //this is the field you have mapped to
        string submittedValue = adaptedControlResult.Value; //this is the user submitted form value
    }
}

Take a look at Sitecore.Form.Submit.CreateItem in Sitecore.Forms.Custom for an example of a similar action and Field Mapping Editor where this is used.

Upvotes: 4

Martin Davies
Martin Davies

Reputation: 4456

I think it gets wired up by matching the fields with public properties in your Save Action class.

So for your example:

public string EmailAddress { get; set; }
public string ConfirmEmailAddress { get; set; }
public string Title { get; set ;}
etc..

Upvotes: 2

Related Questions