Umbraco Forms Custom Workflow to send email to different address based on a dropdown value
I am looking for some instructions on how to create a custom workflow to send emails on submissions to different addresses based on the selected value of a dropdown field.
Second this one. I've had countless clients request this but have never found a good solution besides manually editing the form templates and using javascript to change the value of a hidden field.
Has anyone had any luck doing this sort of thing? I guess a custom workflow is the way to go, but how to actually accomplish this is unclear. You would think this is a common ask.
I have created this in an Umbraco 7 instance, using an older version of Umbraco Forms. In there, I was able to get it working without larger hiccups. But in Umbraco 8 I seem to have hit an issue in which I get no errors, but builds are failing because of my custom workflow. This is my code:
public class DynamicOfficeWorkFlow : WorkflowType
{
[Setting("Email", Description = "Enter the receiver email", View = "TextField")]
public string Email { get; set; }
[Setting("SenderEmail", Description = "Enter the sender email (if blank it will use the settings from /config/umbracosettings.config)", View = "TextField")]
public string SenderEmail { get; set; }
[Setting("Subject", Description = "Enter the subject", View = "TextField")]
public string Subject { get; set; }
[Setting("Message", Description = "Enter the intro message", View = "TextArea")]
public string Message { get; set; }
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly IXmlService _xmlService;
private readonly IWorkflowEmailService _workflowEmailService;
public DynamicOfficeWorkFlow(IUmbracoContextFactory umbracoContextFactory, IXmlService xmlService, IWorkflowEmailService workflowEmailService)
{
this._umbracoContextFactory = umbracoContextFactory;
this._xmlService = xmlService;
this._workflowEmailService = workflowEmailService;
this.Id = new Guid("ccbeb0d5-adaa-4729-8b4c-4bb439dc0202");
this.Name = "DynamicOfficeWorkFlow";
this.Description = "This workflow is for changing the offices based on a dropdown";
this.Icon = "icon-chat-active";
this.Group = "Email";
}
public override List<Exception> ValidateSettings()
{
var exceptionList = new List<Exception>();
if (string.IsNullOrEmpty(this.Email))
exceptionList.Add(new Exception("'Email' setting has not been set"));
if (string.IsNullOrEmpty(this.Message))
exceptionList.Add(new Exception("'Message' setting has not been set'"));
return exceptionList;
}
public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
{
try
{
string email = "";
//Start of dropdown-recipient picker
var dropdownValues = record.GetRecordField("Henvendelse").ValuesAsString(false);
var settingsNode = _umbracoContextFactory.EnsureUmbracoContext().UmbracoContext.Content.GetAtRoot()
.OfTypes("UmbracoFormsSettings");
var umbracoFormsSettings = settingsNode.DescendantsOrSelf<UmbracoFormsSettings>().FirstOrDefault();
var emails = umbracoFormsSettings?.Emails;
if (emails != null)
{
var recipientEmail =
emails.FirstOrDefault(x => x.GetPropertyValue<string>("title") == dropdownValues)
.GetPropertyValue<string>("email");
email = recipientEmail;
}
//End of dropdown-recipient picker
var _workflowEmail =
new SendEmail(_xmlService, _workflowEmailService)
{
SenderEmail = SenderEmail,
Email = email,
Subject = Subject,
Message = Message,
};
_workflowEmail.Execute(record, e);
return WorkflowExecutionStatus.Completed;
}
catch (Exception ex)
{
Current.Logger.Error<SendEmail>(ex, "There was a problem sending an email to {Email} from Workflow for Form {FormName} with id {FormId} for Record with unique id {RecordId}", (object)this.Email, (object)e.Form.Name, (object)e.Form.Id, (object)record.UniqueId);
return WorkflowExecutionStatus.Failed;
}
}
}
There are no errors being flagged nor anything that doesn't work (well, intellisense doesn't ping it at the very least).
Can you verify that this is a valid way to create a custom workflow?
Thank you very much in advance!
Update:
Alright, it was a Visual Studio 2019 error, it turned out to be a Out-of-date-modelsbuilder issue. The above code works and it enables you to change the recipient-email based on a dropdown value. In case others would like to do this in an Umbraco 8.8.0 with the "newest" Umbraco Forms version, installed through NuGet.
Umbraco Forms Custom Workflow to send email to different address based on a dropdown value
I am looking for some instructions on how to create a custom workflow to send emails on submissions to different addresses based on the selected value of a dropdown field.
I have seen a few threads pointing to the extending Umbraco Forms documentation https://our.umbraco.com/documentation/Add-ons/UmbracoForms/Developer/Extending/Adding-a-Workflowtype
Unfortunately I havent found this very user friendly to follow... is anyone able to break this down or share anything?
Thanks
Second this one. I've had countless clients request this but have never found a good solution besides manually editing the form templates and using javascript to change the value of a hidden field.
Yes, this would be a great feature. It seems like it may be possible through Extending but the exact way you would approach this isn't clear.
I agree - It looks like there is a way and some instructions but for a newbie to extending umbraco its not very clear.
Hopefully so one can help.
Has anyone had any luck doing this sort of thing? I guess a custom workflow is the way to go, but how to actually accomplish this is unclear. You would think this is a common ask.
Hi
https://github.com/a2wd/UmbracoForms.ConditionalWorkflows
This was the solution for me.
hope this helps
You can use Umbraco Forms functionality, this should help:
Hi Alex,
I have created this in an Umbraco 7 instance, using an older version of Umbraco Forms. In there, I was able to get it working without larger hiccups. But in Umbraco 8 I seem to have hit an issue in which I get no errors, but builds are failing because of my custom workflow. This is my code:
There are no errors being flagged nor anything that doesn't work (well, intellisense doesn't ping it at the very least). Can you verify that this is a valid way to create a custom workflow?
Thank you very much in advance!
Update: Alright, it was a Visual Studio 2019 error, it turned out to be a Out-of-date-modelsbuilder issue. The above code works and it enables you to change the recipient-email based on a dropdown value. In case others would like to do this in an Umbraco 8.8.0 with the "newest" Umbraco Forms version, installed through NuGet.
is working on a reply...