Copied to clipboard

Flag this post as spam?

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


  • Quentin Lindsey 7 posts 108 karma points
    1 week ago
    Quentin Lindsey
    0

    Get Prevalue Value in Email Template

    I'm working on an email template for a form that has fields that use Pre-value Sources. My fields display values to our users but when submitted the Pre-value ID is received.

    I would like to know how I would use the ID to retrieve the Pre-value Value for these fields within our Email template.

    I would also like to know if it's possible to retrieve an enumerable collection of all of the Pre-value items for a specific source in the Email template? We would like to display all the possible choices in the template and highlight the options selected by the user filling out the form.

  • Steve Morgan 977 posts 3014 karma points c-trib
    1 week ago
    Steve Morgan
    1

    Hi,

    I would think you'll need a custom workflow in which you can create an Umbraco Helper and go get these values.

    I've done one where I have to go get a third party reference (CRM) and then generate my own email using the form data and this CRM reference. It includes a picker where I store the email template under a settings node and then I replace the fields with the form and other values.

    Contact me at steve @ siempresolutions.co.uk if you think this approach would work for you and you'd like to see the workflow as I can't post it here.

    I'd be interested if someone else has a better, easier suggestion though (I bet there is!).

    Steve

  • Quentin Lindsey 7 posts 108 karma points
    1 week ago
    Quentin Lindsey
    100

    Okay, I appreciate the response, but I ended up going in a different direction.

    No idea if this was the best route or not, but it fit my needs. Here is what I ended up with...

    First, I created a custom ViewModel. This ViewModel inherits the

    Umbraco.Forms.Mvc.Models.FormViewModel
    

    which is used in the partial view for rendering the form.

    The constructor for the new ViewModel accepts a

    Umbraco.Forms.Core.Models.FormsHtmlModel
    

    object which is used in the partial view for rendering the email.

    I added a new Build method to my implementation of the FormViewModel that accepts the form ID, which can be used to retrieve the Umbraco Form from the

    Umbraco.Forms.Data.Storage.FormStorage
    

    class for building the FormViewModel.

    By doing this not only can I retrieve PreValue data for my form fields, but I can also use the FormViewModel to get the fieldsets, containers, and fields for creating an email that is close to or identical to the form template.

    The downside is that you have to hardcode the Form Guid into the template which is fine I guess, but it would be nice if you could get the Form name or Guid from the FormHtmlModel.

    Here is the ViewModel class I created:

    using System;
    using Umbraco.Forms.Core.Models;
    using Umbraco.Forms.Data.Storage;
    using Umbraco.Forms.Mvc.Models;
    
    namespace TestProject.Models {
        /// <summary>
        /// This class is used to wrap a FormViewModel and FormHtmlModel into one
        /// object for rendering an Email Template with form structure and form POST data.
        /// </summary>
        public class FormViewHtmlModel : FormViewModel {
            #region Constructors
    
            /// <summary>
            /// Constructs a vew form view html model using the FormsHtmlModel object provided.
            /// </summary>
            /// <param name="data">The FormsHtmlModel object.</param>
            public FormViewHtmlModel(FormsHtmlModel data) {
                FormData = data;
            }
    
            #endregion
    
            #region Properties
    
            /// <summary>
            /// This property contains form data submitted on the current request.
            /// </summary>
            public FormsHtmlModel FormData { get; protected set; }
    
            #endregion
    
            #region Methods
    
            /// <summary>
            /// Retrieves and builds a form from Form Storage using the provided Umbraco Form ID.
            /// </summary>
            /// <param name="id">The Umbraco Form ID needed to retrieve the form from Form Storage.</param>
            public virtual void Build(Guid id) {
                if (id == default(Guid)) {
                    throw new ArgumentException("id");
                }
    
                using (var storage = new FormStorage()) {
                    Build(storage.GetForm(id));
                }
            }
    
            /// <summary>
            /// Retrieves and builds a form from Form Storage using the provided Umbraco Form Name.
            /// </summary>
            /// <param name="name">The Umbraco Form Name needed to retrieve the form from Form Storage.</param>
            public virtual void Build(string name) {
                if (string.IsNullOrWhiteSpace(name)) {
                    throw new ArgumentException("name");
                }
    
                using (var storage = new FormStorage()) {
                    Build(storage.GetForm(name));
                }
            }
    
            #endregion
        }
    }
    

    Here is a simple Email template that leverages the new ViewModel class:

    @inherits UmbracoViewPage<Umbraco.Forms.Core.Models.FormsHtmlModel>
    
    @{
        var fvhm = new TestProject.Models.FormViewHtmlModel(Model);
    
        // Replace the Guid with the ID for the form you want to render with form data.
        fvhm.Build(Guid.Parse("ecc98cb0-de93-4d26-9b03-078330ec2841"));
    }
    
    <!DOCTYPE html>
    
    <html>
    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>@fvhm.FormName</title>
    </head>
    
    <body style="margin: 0 !important; padding: 0 !important;">
        <h1>@fvhm.FormName</h1>
    
        @foreach (var fieldset in fvhm.CurrentPage.Fieldsets) {
            <table cellpadding="5" cellspacing="0" border="0" width="100%">
                @if (!string.IsNullOrWhiteSpace(fieldset.Caption)) {
                    <tr>
                        <th style="border-bottom: 2px solid #aaa; color: #669; font-size: 110%; text-align: left;">@fieldset.Caption</th>
                    </tr>
                }
    
                @foreach (var container in fieldset.Containers) {
                    foreach (var field in container.Fields) {
                        <tr>
                            <td>
                                <p><strong>@field.Caption</strong></p>
                                <p style="margin-left: 20px;">
                                    @if (field.FieldType.SupportsPrevalues) {
                                        var values = fvhm.FormData.GetValues(field.Alias)
                                            .Select(v => v.ToString()) @* Convert to String Array *@
                                            .Where(v => !string.IsNullOrWhiteSpace(v)); @* Remove Empty or WhiteSpace entries *@
    
    
                                        if (values.Any() && field.PreValues.Any()) {
                                            <span>
                                                @(string.Join(", ", field.PreValues
                                                    .Where(pv => values.Contains(pv.Id))
                                                    .Select(m => m.Value)))
                                            </span>
                                        } else {
                                            <span style="color: #999;">No options were selected.</span>
                                        }
                                    } else {
                                        var value = fvhm.FormData.GetValue(field.Alias).ToString();
    
                                        if (!string.IsNullOrWhiteSpace(value)) {
                                            <span>@value</span>
                                        } else {
                                            <span style="color: #999;">No response provided.</span>
                                        }
                                    }
                                </p>
                            </td>
                        </tr>
                    }
                }
            </table>
        }
    </body>
    
    </html>
    
  • Steve Morgan 977 posts 3014 karma points c-trib
    1 week ago
    Steve Morgan
    0

    Nice work!

Please Sign in or register to post replies

Write your reply to:

Draft