0
votes

I am on WFFM 8 rev. 150625

I have created a form that would submit data to WFFM programmatically.

The code snipped to submit data is:

var simpleForm = new SitecoreSimpleForm(sitecoreFormItem);
var actionList = simpleForm.FormItem.ActionsDefinition;

var actionDefinitions = new List<ActionDefinition>();
actionDefinitions.AddRange(actionList.Groups.SelectMany(x => x.ListItems)
                 .Select(li => new ActionDefinition(li.ItemID, li.Parameters)  
                 { UniqueKey = li.Unicid }));

//crList is ControlResult[] and contains field values. 
SubmitActionManager.Execute(sitecoreFormItem.ID, 
                            crList.ToArray(), actionDefinitions.ToArray());

My WFFM form in sitecore has no save action, as I am sending out the email in the code itself. I noticed that the data was being saved in MongoDB, but not in the reporting database.

Is there any way I can trigger the save to reporting DB action? Do I need to call some other function to execute that bit?

2
There is a Fact table for WFFM in the reporting database that you need to install with WFFM. Do you want to add the data in here? I think the table stores rolled up data rather than individual submission data entries like the old version did. - Ian Graham
Hey Ian, I am referring to the data stored in SQL for reporting purposes. Tables like FormFieldValues still store the form data and linked to Fact_FormSummary etc. Just wondering why it won't process and store that data if form was submitted programmatically. Or do I need to call some other function to do that? - NomadTraveler
Did you resolve this issue? I have the same problem, while submitting the form through a service - Kristian Nissen

2 Answers

0
votes

It should be possible. Essentially you need create a new Instance of the SaveToDatabase in your code. Then you need Execute the Save Action passing in the FormId, AdaptedResultList, containing the Fields and the Values.

    public void SubmitToDatabase(FormItem formItem, string[] values)
    {
        // Create AdaptedControlResult for the Fields of the form 
        // even better if you can pass the AdaptedResultList
        // from the form directly
        var adaptedControlResults = new List<AdaptedControlResult>();
        foreach (FieldItem fieldItem in formItem.FieldItems.ToList())
        {
            adaptedControlResults.Add(new AdaptedControlResult(new ControlResult(fieldItem.Name, values[formItem.FieldItems.ToList().IndexOf(fieldItem)], null), true));
        }

        var adaptedResultList = new AdaptedResultList(adaptedControlResults);

        Sitecore.Form.Submit.SaveToDatabase saveToDatabaseSaveAction = new Sitecore.Form.Submit.SaveToDatabase();
        try
        {
            saveToDatabaseSaveAction.Execute(formItem.ID, adaptedResultList, null);
        }
        catch (Exception ex)
        {
            Sitecore.Diagnostics.Log.Error(ex.Message, ex, this);
        }

The most complex bit will be passing the values the user has entered to this method to save. I'd recommend passing the AdaptedResultList directly where possible.

Reference - http://mikerobbins.co.uk/2014/10/15/write-to-web-forms-programmatically/

0
votes

Try this code. It will work for Sitecore 8.0 and higher. Note that this code doesn't trigger the save actions. If you want to trigger them you'll need to manually pass them into the FormDataHandler.ProcessData

 var controlResults = new List<ControlResult>();
                    controlResults.Add(new ControlResult(Pdf_Request_Form.Name.ItemID.ToString(), "Name", name, string.Empty));
                    controlResults.Add(new ControlResult(Pdf_Request_Form.Email.ItemID.ToString(), "Email", email, string.Empty));

    #pragma warning disable 618
                    FormDataHandler.ProcessData(Pdf_Request_Form.ItemID, controlResults.ToArray(), new IActionDefinition[] {}, DependenciesManager.ActionExecutor);
    #pragma warning restore 618