Question

Photo of Adam Zeuske

0

Launching Workflows from code

Good morning,

I have a custom block akin to the Photo Request block with the filter.  Except, instead of sending an email, I would like to launch a workflow instance of a custom workflow for each person that the filter resolves.  I believe I have the code correct for assigning workflow attributes as parameters (person entities...)

workflow.SetAttributeValue("Person", person.PrimaryAlias.Guid.ToString());

However, when I execute the following code, the workflow object gets created in the workflow table, and the initial activity object gets created as well, but it doesn't seem to process.  It never makes it to the Send Email action in the first activity.  I am working locally with my Rockit solution.

This is the code I am using to launch the workflow, borrowed from the ActivateWorkflow Rock block..

                            List<string> workflowErrors;
                            if (workflow.Process(rockContext, out workflowErrors))
                            {
                                if (workflow.IsPersisted || workflowType.IsPersisted)
                                {
                                    var workflowService = new Rock.Model.WorkflowService(rockContext);
                                    workflowService.Add(workflow);

                                    rockContext.WrapTransaction(() =>
                                    {
                                        rockContext.SaveChanges();
                                        workflow.SaveAttributeValues(rockContext);
                                        foreach (var activity in workflow.Activities)
                                        {
                                            activity.SaveAttributeValues(rockContext);
                                        }
                                    });
                                }
                            }

What am I missing?  Thanks so much for the assistance.

Adam

  • Photo of Adam Zeuske

    0

    Yes, that was helpful in determining which lines of code above was causing the failure.  In case anyone attempts this in the future, the Attribute setting code above was causing the error.  It turns out that all I had to do was make sure the workflow.Process method used the person in scope as the operating entity.  So, I had to remove my SetAttribute methods on the workflow and change the following line...

                                if (workflow.Process(rockContext, person, out workflowErrors))
                                {

    After that, the code above ran the workflow.  Other than the email being sent from the wrong email address (the workflow kicked off from code is not using the correct Sender email address, although the CurrentPerson is being set correctly according to the logs) it all works.

    Thanks for pointing me in the right direction.

    Adam

  • Photo of Rock RMS

    0

    Adam,

    Couple of suggestions when debugging workflows. First configure the workflow type to be automatically persisted (even if later it doesn't need to always persist ), and set the logging level to 'Action.'  Then run the workflow again. An instance should now appear in the list when you select Tools > Workflows  and then select the "Manage" option to the right of your workflow type. Select the workflow instance, and click 'Edit' to view a log of the processing. The log should indicate which actions ran succesfully and which did not.

    Does that help?