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; }
//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."; } } }
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());
}
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
Comment author was deleted
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
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:
Comment author was deleted
Great, glad it's working now!
is working on a reply...