umbraco / Umbraco.Forms.Issues

Public issue tracker for Umbraco Forms
29 stars 0 forks source link

Workflow fails when submitting a form. #1179

Closed Aki-Gra closed 3 months ago

Aki-Gra commented 4 months ago

Reproduction

Bug summary

Upon attempting to submit a basic form, it fails after a custom database saving workflow.

Specifics

Steps to reproduce

get repository instance using IRepositoryFactory do logic and saves the changes made. _repo = repositoryFactory.GetRepository<EnquiryDto, int>(); var result = this._repo.Save(enquiry, false);

after workflow complete it goes to default, save form workflow, and it fails image image (10)

Expected result

Form saves to database (custom table) and show up in Backoffice enquiries

Actual result

Form saves to database (custom table) but does not show up in Backoffice enquiries

AndyButland commented 4 months ago

Are you able to share a minimal example of your custom workflow that illustrates this problem please @Aki-Gra? And also the version of Forms you are using?

Aki-Gra commented 4 months ago

sorry, I'm bad at describing issues. here is the workflow It is very basic so I think it's okay to share. image (12) this workflow was completed without issue. I can see an entry in the database. but whatever runs after it makes it break. here is the whole flow of submitting my form. (don't mind the descriptions its WIP) image

Using: Umbraco.Forms Version 13.0.2 Umbraco.UIBuilder Version 13.1.0-rc2 Umbraco.Cms Version 13.1.1

AndyButland commented 4 months ago

Thanks, I can replicate, and at first glance I think the issue may lie in UI Builder, but we're investigating further.

Meantime you might try injecting a scope accessor and doing your database update using that.

Something like this seems to work without throwing the exception:

using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Scoping;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Enums;
using Umbraco.Forms.TestSite.Models;
using Umbraco.UIBuilder.Persistence;

namespace Umbraco.Forms.Testsite.Business.Workflows
{
    public class SaveToDatabaseWorkflow : WorkflowType
    {
        private const string WorkflowId = "5a3bf560-1e96-4888-b570-cc8953c03dbf";

        private readonly IScopeAccessor _scopeAccessor;

        public SaveToDatabaseWorkflow(IScopeAccessor scopeAccessor)
        {
            Id = new Guid(WorkflowId);
            Name = "Save Enquiry To Database";
            Description = "A test workflow that saves to the database using UI Builder";
            Icon = "icon-umbrella";

            _scopeAccessor = scopeAccessor;
        }

        public override List<Exception> ValidateSettings() => new();

        public override Task<WorkflowExecutionStatus> ExecuteAsync(WorkflowExecutionContext context)
        {
            var enquiry = new EnquiryDto
            {
                Name = context.Record.GetRecordFieldByAlias("name")?.ValuesAsString(false) ?? string.Empty,
                Form = context.Form.Name,
            };

            IUmbracoDatabase? db = _scopeAccessor.AmbientScope?.Database;
            if (db is null)
            {
                // Shouldn't in a workflow.
                return Task.FromResult(WorkflowExecutionStatus.Failed);
            }

            db.Insert(enquiry);

            return Task.FromResult(WorkflowExecutionStatus.Completed);
        }
    }
}
Aki-Gra commented 4 months ago

That worked Thank you.

AndyButland commented 3 months ago

I'll close this now as the original issue will be resolved within UI Builder, which you can expect in the next round of patch releases (10.0.5, 12.0.4 and 13.1.1).