Skip to content Skip to sidebar Skip to footer

Add A New Property To A Node(page) Using ECMA Script

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 be

Solution 1:

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");

Post a Comment for "Add A New Property To A Node(page) Using ECMA Script"