2

I have a Workflow which is in draft state with submit action , based on few condition i have to decide if the next state is asian approver or american approver programattically how do i do this ? If i do that then What do i give in the next state field (it shud be decided dynamically but sitecore throws an error if that field is empty)

Please refer the images enter image description here enter image description here

  • You could create a custom workflow State that inherits from the State template ({4B7E2DA9-DE43-4C83-88C3-02F042031D04}) where that would have a rules field to use the sitecore rules engine to determine which workflow state to be in. You can set the insert options of that new template to be workflow states. You would also have to override the Workflow Provider to leverage this new template and the rules engine – Robert Michael Watson May 22 '18 at 23:31
5

You can create an interim Workflow State which specifies an Action to decide which state to move the workflow to next.

For example:

Workflow State Selector

Ensure you have set the following:

  • The Submit Action on the Draft state should specify interim state as the Next State
  • The interim state should contain:
    • An Action item, and set the Type string value to your custom class (see code below). This Action will run immediately upon entering this State.
    • A Command item with the Next state field field set to move to the Asia Approver state
    • A Command item with the Next state set to move to the US Approver state
  • Your 2 Workflow State definitions for your Asia and US Approver

You need to create a class which will contain the logic of when to move to a particular state. You would ideally use the Rules Engine (as mentioned in the comments) but for the sake for simplicity we will hard code the logic.

SetWorkflowState.cs

using System.Linq;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Workflows;
using Sitecore.Workflows.Simple;

namespace MyProject.WOrkflow
{
  public class SetWorkflowState
  {
    public void Process(WorkflowPipelineArgs args)
    {
      Assert.ArgumentNotNull((object)args, nameof(args));
      Item dataItem = args.DataItem;

      if (dataItem["Continent"] == "Asia") // or whatever logic you need
        ExecuteCommand(dataItem, "Send to Asia Approver");
      else
        ExecuteCommand(dataItem, "Send to US Approver");
    }

    public WorkflowResult ExecuteCommand(Item item, string commandName, string comment = null)
    {
      IWorkflow workflow = item.Database.WorkflowProvider.GetWorkflow(item);

      if (workflow == null)
        return new WorkflowResult(false, "No workflow assigned to item");

      WorkflowCommand command = workflow.GetCommands(item[FieldIDs.WorkflowState])
                                        .FirstOrDefault(c => c.DisplayName == commandName);

      if (command == null)
        return new WorkflowResult(false, "Workflow command not found");

      return workflow.Execute(command.CommandID, item, comment, false, new object[0]);
    }
  }
}

Update the logic to suit your needs, the above will trigger the "Send to Asia Approver" or "Send to US Approver" Command and move the item (based on the Next state field on that command) to the correct state. This will also execute all/any actions which may be specified below the command as well, mimicking the behaviour of a user clicking a Workflow button in the UI.

You can read more about Actions in section 3.1.4 of the Workflow Reference document and more about changing workflow programatically in this blog post.

  • what would be the next state for set approver state ? – user3621 May 25 '18 at 15:33
  • Nothing, because a State does not have a next state, a Command does. – jammykam May 25 '18 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.