Copied to clipboard

Flag this post as spam?

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


  • SC Digital Services Team 104 posts 171 karma points
    Jan 09, 2014 @ 17:49
    SC Digital Services Team
    0

    Custom Workflow not updating field value

    Having some trouble getting a custom workflow to generate a URL with a querystring, which then should be recorded in a hidden field. Despite a couple of rewrites to the code, the generated string isn't being saved to the hidden field.

    The URL is a string that set in the workflow, and the query string is a concatenation of the captions and values within the form. From browsing the forums I've followed the advice given and added the workflow item to the Approved step, yet this still isn't working - any help is appreciated!

    EDIT - we are using Contour 3.0.18 on Umbraco 7.0.1

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Forms.Core;
    using Umbraco.Forms.Core.Enums;
    using Umbraco.Forms.Core.Services;
    using Umbraco.Forms.Data.Storage;
    using Umbraco.Core;


    namespace Shropshire.Contour.CustomWorkflows
    {
    public class BuildUrlWithParams : Umbraco.Forms.Core.WorkflowType
    {
    [Umbraco.Forms.Core.Attributes.Setting("Target URL",
    description = "Enter full URL to redirect to on form submission (excluding parameters)",
    control = "Umbraco.Forms.Core.FieldSetting.TextField")]
    public string TargetUrl { get; set; }

    [Umbraco.Forms.Core.Attributes.Setting("Select field to populate",
    description = "Pick the hidden 'redirectURL' field that will be updated",
    control = "Umbraco.Forms.Core.FieldSetting.FieldPicker")]
    public string field { get; set; }

    public override WorkflowExecutionStatus Execute(Umbraco.Forms.Core.Record record, RecordEventArgs e)
    {
    string paramList = "?";
    string fullUrl = string.Empty;

    //we can then iterate through the fields
    foreach (RecordField rf in record.RecordFields.Values)
    {
    //if this isn't the hidden field used to hold the generated URL
    if (rf.Field.Id != new Guid(field))
    {
    //if this isn't the first item to be added to the parameter list, add the ampersand separator
    if (paramList.Length > 1)
    {
    paramList = paramList + "&";
    }
    //add the URL parameter and value
    paramList = paramList + rf.Field.Caption.ToSafeFileName() + "=" + HttpUtility.UrlEncode(rf.Field.Values.ToString());
    }
    }


    fullUrl = TargetUrl + paramList;

    //set URL
    foreach (RecordField rf in record.RecordFields.Values)
    {
    if (rf.Field.Id == new Guid(field))
    {
    rf.Values.Clear();
    rf.Values.Add(fullUrl);
    break;
    }
    }

    FormStorage fs = new FormStorage();
    Form f = fs.GetForm(record.Form);
    RecordStorage rs = new RecordStorage();
    rs.UpdateRecord(record, f);
    rs.UpdateRecordXml(record, f);

    fs.Dispose();
    rs.Dispose();

    return WorkflowExecutionStatus.Completed;
    }


    public override List ValidateSettings() {
    List exceptions = new List();

    if (string.IsNullOrWhiteSpace(TargetUrl))
    {
    exceptions.Add(new Exception("Target URL not set"));
    }
    return exceptions;
    }



    public BuildUrlWithParams()
    {
    this.Name = "Build a redirect URL using field values as parameters";
    this.Id = new Guid("442e4331-9ba7-4610-9548-91d032932b61");
    this.Description = "This needs a hidden 'redirectURL' field, which will be picked up and used by a custom event handler to force a redirect.";
    }
    }
    }

  • Comment author was deleted

    Jan 10, 2014 @ 12:41

    IF you debug code, does this part get hit

    rf.Values.Add(fullUrl);

    make sure that if (rf.Field.Id == new Guid(field)) actually passes think it might have to do with casing of the field guid

  • SC Digital Services Team 104 posts 171 karma points
    Jan 10, 2014 @ 12:57
    SC Digital Services Team
    100

    Thanks Tim,

    It's given us a clue & a bit more debugging has led us to the cause of the problem - it was blank values in fields that were breaking the construction of the paramList variable, which somehow prevented the value from being saved properly. Our code within the first loop through the fields now looks like this:

    //add the URL parameter and value
    paramList = paramList + rf.Field.Caption.ToString() + "=";
    
    if (!string.IsNullOrWhiteSpace(rf.ValuesAsString()))
    {
       paramList = paramList + HttpUtility.UrlEncode(rf.ValuesAsString());
    }
    
  • Comment author was deleted

    Jan 10, 2014 @ 13:48

    Great, glad it's working now!

Please Sign in or register to post replies

Write your reply to:

Draft