Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kyle Goulding 30 posts 192 karma points
    Jan 02, 2019 @ 16:19
    Kyle Goulding
    0

    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

  • Amir Khan 1287 posts 2744 karma points
    Jan 03, 2019 @ 03:56
    Amir Khan
    0

    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.

  • David Morgan 3 posts 73 karma points
    Jan 03, 2019 @ 18:59
    David Morgan
    0

    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.

  • Kyle Goulding 30 posts 192 karma points
    Jan 11, 2019 @ 09:03
    Kyle Goulding
    0

    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.

  • Heather 13 posts 125 karma points
    Jun 30, 2020 @ 01:11
    Heather
    0

    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.

  • Naty 14 posts 81 karma points
    Oct 29, 2020 @ 20:15
    Naty
    0

    Hi

    https://github.com/a2wd/UmbracoForms.ConditionalWorkflows

    This was the solution for me.

    hope this helps

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Feb 03, 2021 @ 22:51
    Alex Skrypnyk
    0

    You can use Umbraco Forms functionality, this should help:

    var sendEmailWorkflow = new Umbraco.Forms.Core.Providers.WorkflowTypes.SendEmail(_xmlService, _contentSection)
    {
        Email = email,
        SenderEmail = SenderEmail,
        Subject = Subject,
        Message = Message,
        Attachment = Attachment
    };
    sendEmailWorkflow.Execute(record, e);
    
  • Mikkel.S 33 posts 167 karma points
    Jun 30, 2021 @ 11:51
    Mikkel.S
    0

    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:

    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.

Please Sign in or register to post replies

Write your reply to:

Draft