WFFM Custom Save Action Send Email based on Dropdown Selection.

WFFM Custom Save Action Send Email based on Dropdown Selection.

WFFM have its own send email action but we can’t change the email field during run time. Suppose we have a category dropdown and based on category WFFM need to send email to different group. So if category is “Marketing” is selected then email should go to the marketing group and if category “HR” is selected then email should go to the human resource group.

To resolve this kind of problem we have created a custom droplist field which is just copy of existing droplist field. Motive behind creating this duplicate field is just to differentiate default dropdown field for our custom save action. Datasource for this field should be a template which have all required email field such as TO, CC, BCC, Subject etc.

We have created a custom save action for sending email based on selection by navigating sitecore/System/Modules/Web Forms for Marketers/Settings/Actions/Save Actions

You need to right click on the save actions item and select save action button from insert option, it will open the dialog and you need to enter the name of the action.

In the new save action we need to fill the Assembly and Class fields by entering the name of the assembly and class name.

We will also use the parameter field for SMTP information for sending custom email by using this SMTP information.

We are inheriting the Sitecore.WFFM.Actions.Base.WffmSaveAction abstract class and overriding the Execute method.

using System;

using Castle.Core.Internal;

using Glass.Mapper.Sc;

using Sitecore;

using Sitecore.Data;

using Sitecore.WFFM.Abstractions.Actions;

using Sitecore.WFFM.Actions.Base;

using Log = Sitecore.Diagnostics.Log;

 

namespace Namespace.FormActions

{

public class CustomSendEmail : WffmSaveAction

{

public override void Execute(ID formId, AdaptedResultList adaptedFields,

ActionCallContext actionCallContext = null, params object[] data)

{

var sitecoreService = new SitecoreService(Context.Database);

var adaptedDriverField = FindCustomEmailDropListField(adaptedFields);

if (adaptedDriverField == null)

{

// Log the warning if this save action is applied without using custom field

Log.Warn(

“Warning: Custom Send Email Custom action will not work without Custom Email Drop List field, please make sure you have this type of field (Field Type ID =add the field id for ease) in WFFM Form: ” +

formId, this);

return;

}

try

{

if (!adaptedDriverField.Value.IsNullOrEmpty())

{

//Get the ID for the selected item

var adaptedFieldValue = ID.Parse(adaptedDriverField.Value);

if (!adaptedFieldValue.IsGlobalNullId)

{

// Get the Item from sitecore based on selected ID

var emailItem = Context.Database.GetItem(adaptedFieldValue);

if (emailItem != null)

{

// emailItem will have field for email action such as to, cc, bcc, subject etc. this method will use these field as well as the smtp information on the action to send the email

EmailUtility.SendCustomEmail(this, emailItem, adaptedFields);

}

}

}

}

catch (Exception ex)

{

Log.Error(“Error: Custom Send Email Custom action is not working for WFFM Form: ” + formId, ex, this);

}

}

 

private AdaptedControlResult FindCustomEmailDropListField(AdaptedResultList adaptedFields)

{

AdaptedControlResult driverField = null;

foreach (AdaptedControlResult adpatedControlResult in adaptedFields)

{

var fieldItem = Context.Database.GetItem(adpatedControlResult.FieldID);

// get the custom duplicate dropdown field

if (fieldItem.Fields[“Field Link”].Value != “ID of the Field”) continue;

driverField = adpatedControlResult;

break;

}

return driverField;

}

}

}

Advertisements

One thought on “WFFM Custom Save Action Send Email based on Dropdown Selection.

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 )

Google+ photo

You are commenting using your Google+ 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