2
votes

TL;DR version:

  • In CQ workflows, is there a difference between what's available to the OR Split compared to the Process Step?
  • Is it possible to access the /history/ nodes of a workflow instance from within an OR Split?
  • How?!

The whole story:

I'm working on a workflow in CQ5 / AEM5.6.

In this workflow I have a custom dialog, which stores a couple of properties on the workflow instance.

The path to the property I'm having trouble with is: /workflow/instances/[this instance]/history/[workItem id]/workItem/metaData and I've called the property "reject-or-approve".

The dialog sets the property fine (via a dropdown that lets you set it to "reject" or "approve"), and I can access other properties on this node via a process step (in ecma script) using:

var actionReason;
var history = workflowSession.getHistory(workItem.getWorkflow());

// loop backwards through workItems
// and as soon as we find a Action Reason that is not empty
// store that as 'actionReason' and break.
for (var index = history.size() - 1; index >= 0; index--) {
  var previous = history.get(index);

    var tempActionReason = previous.getWorkItem().getMetaDataMap().get('action-message');

    if ((tempActionReason != '')&&(tempActionReason != null)) {
        actionReason = tempActionReason;
        break;
    }
}

The process step is not the problem though. Where I'm having trouble is when I try to do the same thing from inside an OR Split.

When I try the same workflowSession.getHistory(workItem.getWorkflow()) in an OR Split, it throws an error saying workItem is not defined.

I've tried storing this property on the payload instead (i.e. storing it under the page's jcr:content), and in that case the property does seem to be available to the OR Split, but my problems with that are:

  • This reject-or-approve property is only relevant to the current workflow instance, so storing it on the page's jcr:content doesn't really make sense. jcr:content properties will persist after the workflow is closed, and will be accessible to future workflow instances. I could work around this (i.e. don't let workflows do anything based on the property unless I'm sure this instance has written to the property already), but this doesn't feel right and is probably error-prone.
  • For some reason, when running through the custom dialog in my workflow, only the Admin user group seems to be able to write to the jcr:content property. When I use the dialog as any other user group (which I need to do for this workflow design), the dialog looks as though it's working, but never actually writes to the jcr:content property.

So for a couple of different reasons I'd rather keep this property local to the workflow instance instead of storing it on the page's jcr:content -- however, if anyone can think of a reason why my dialog isn't setting the property on the jcr:content when I use any group other than admin, that would give me a workaround even if it's not exactly the solution I'm looking for.

Thanks in advance if anyone can help! I know this is kind of obscure, but I've been stuck on it for ages.

2
Hi Ben, very nice description! I have one more question just to make sure what do you want to achieve. What is your use case for this workflow? In which cases and by who it should be launched? Regarding the problem with storing property under jcr:content - probably the user, who starts the workflow or uses the dialog does not have permissions to store under this jcr:content. Please check to which group such user belongs and check it's permissions. It is done probably by XHR POST - check the response in your browser.Michal Chudy
Thanks for the suggestion Michal. As I was about to look into XHR POST, my colleague told me he'd solved this one. I'll post an answer separately.Ben Flanagan
Update: turns out we had solved one part of the problem, but we're not quite done. What we've figured out is that the Approver was unable to update the approve-or-reject property (on the payload's jcr:content) because our workflow has the payload locked during the dialog step. Admin can update the property because the payload is locked as admin. Unfortunately this doesn't actually solve our problem, because we still need the payload to be locked. If we can find a way to unlock, update the jcr:content and then lock again, all at once, that would get us out of trouble.Ben Flanagan
This one would be hard without backend development. Could you tell me what is your use case for this workflow? One more hint would be taking look on how OR split looks like in JCR. It is not a normal step. As far as I remember it consists of couple nodes.Michal Chudy
Yeah I had a look at the OR Split structure and it seems pretty complex. I was considering developing a custom workflow step, but at this stage that would be a steep learning curve I don't have time for! I have found a workaround (different to yesterday's) which I think is fairly neat. I'll post my findings as an answer.Ben Flanagan

2 Answers

2
votes

a couple of days ago i ran into the same issue. The issue here is that you don't have the workItem object, because you don't really have an existing workItem. Imagine the following: you are going through the workflow, you got a couple of workItems, with means, either process step, either inbox item. When you are in an or split, you don't have existing workItems, you can ensure by visiting the /workItems node of the workflow instance. Your workaround seems to be the only way to go through this "issue".

1
votes

I've solved it. It's not all that elegant looking, but it seems to be a pretty solid solution.

Here's some background:

Dialogs seem to reliably let you store properties either on:

  • the payload's jcr:content node (which wasn't practical for me, because the payload is locked during the workflow, and doesn't let non-admins write to its jcr:content)
  • the workItem/metaData for the current workflow step

However, Split steps don't have access to workItem. I found a fairly un-helpful confirmation of that here: http://blogs.adobe.com/dmcmahon/2013/03/26/cq5-failure-running-script-etcworkflowscriptscaworkitem-ecma-referenceerror-workitem-is-not-defined/

So basically the issue was, the Dialog step could store the property, but the OR Split couldn't access it.

My workaround was to add a Process step straight after the Dialog in my workflow. Process steps do have access to workItem, so they can read the property set by the Dialog. I never particularly wanted to store this data on the payload's jcr:content, so I looked for another location. It turns out the workflow metaData (at the top level of the workflow instance node, rather than workItem/metaData, which is inside the /history sub-node) is accessible to both the Process step and the OR Split. So, my Process step now reads the workItem's approveReject property (set by the Dialog), and then writes it to the workflow's metaData node. Then, the OR Split reads the property from its new location, and does its magic.

The way you access the workflow metaData from the Process step and the OR Split is not consistent, but you can get there from both.

Here's some code: (complete with comments. You're welcome)

In the dialog where you choose to approve or reject, the name of the field is set to rejectApprove. There's no ./ or anything before it. This tells it to store the property on the workItem/metaData node for the current workflow step under /history/.

Straight after the dialog, a Process step runs this:

var rejectApprove;
var history = workflowSession.getHistory(workItem.getWorkflow());

// loop backwards through workItems
// and as soon as we find a rejectApprove that is not empty
// store that as 'rejectApprove' and break.
for (var index = history.size() - 1; index >= 0; index--) {
  var previous = history.get(index);
    var tempRejectApprove = previous.getWorkItem().getMetaDataMap().get('rejectApprove');
    if ((tempRejectApprove != '')&&(tempRejectApprove != null)) {
        rejectApprove = tempRejectApprove;
        break;
    }
}
// steps up from the workflow step into the workflow metaData, 
// and stores the rejectApprove property there
// (where it can be accessed by an OR Split)
workItem.getWorkflowData().getMetaData().put('rejectApprove', rejectApprove);

Then after the Process step, the OR Split has the following in its tabs:

function check() {
    var match = 'approve';
    if (workflowData.getMetaData().get('rejectApprove') == match) {
        return true;
    } else {
        return false;
    }
}

Note: use this for the tab for the "approve" path, then copy it and replace var match = 'approve' with var match = 'reject'

So the key here is that from a Process step:

workItem.getWorkflowData().getMetaData().put('rejectApprove', rejectApprove);

writes to the same property that:

workflowData.getMetaData().get('rejectApprove') reads from when you execute it in an OR Split.

To suit our business requirements, there's more to the workflow I've implemented than just this, but the method above seems to be a pretty reliable way to get values that are entered in a dialog, and access them from within an OR Split.

It seems pretty silly that the OR Split can't access the workItem directly, and I'd be interested to know if there's a less roundabout way of doing this, but for now this has solved my problem.

I really hope someone else has this same problem, and finds this useful, because it took me waaay to long to figure out, to only apply it once!