Question

I need to add a property to a page on page activation. I have decided to set up a workflow process that does the same before an activation step. My custom workflow step (the one before the activation step) makes use of an ECMA script to achieve this. Here's what I have so far.

var workflowData = graniteWorkItem.getWorkflowData();
if (workflowData.getPayloadType() == "JCR_PATH") {
    var path = workflowData.getPayload().toString();
    var jcrsession = graniteWorkflowSession.adaptTo(Packages.javax.jcr.Session);
    var node = jcrsession.getNode(path);
    if (!node.hasProperty("foo")){
    var cal = Packages.java.util.Calendar.getInstance();
            node.setProperty("foo", cal);
            node.save();
         }
if (!node.hasProperty("foo2")){
            node.setProperty("foo2", "2020-08-26T22:30:00.000+05:30");
            node.save();
        }
}

However, when I run the workflow on a page, the properties that I need to get created (foo and foo2 in this instance) do not get created.

What am I doing wrong?

Was it helpful?

Solution

have you tried tailing your error.log? i tried your script and it didn't work--this particular version of it does, though:

var workflowData = workItem.getWorkflowData();
if (workflowData.getPayloadType() == "JCR_PATH") {
    var path = workflowData.getPayload().toString();
    var jcrsession = workflowSession.getSession();
    var node = jcrsession.getNode(path);
    if (!node.hasProperty("foo")){
    var cal = Packages.java.util.Calendar.getInstance();
            node.setProperty("foo", cal);
            node.save();
         }
if (!node.hasProperty("foo2")){
            node.setProperty("foo2", "2020-08-26T22:30:00.000+05:30");
            node.save();
        }
}

note that instead of granite*, it's just workItem and workSession. also note that WorkflowSession doesn't have an adaptTo() method (unless i'm using an older cq version than you). it already has a getSession() method as part of the interface.

even when that's all said and done, this failed because of the content i was sending through the workflow--make sure the node you're trying to write to accepts those property names. cq:Page is very restrictive, but cq:PageContent is not (so retrieve the jcr:content subnode, assuming you're launching workflows against cq:Page or dam:Asset nodes):

    var node = jcrsession.getNode(path).getNode("jcr:content");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top