Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to set worfklow end time in AEM6.5

Avatar

Level 7

I need to set the workflow end date and time in node.

 

like how I am setting the worfklow initiator name:

String workflowInitiator = workItem.getWorkflow().getInitiator();
if (node.hasNode("jcr:content")) {
 node.getNode("jcr:content").setProperty("InitiatedBy", workflowInitiator);
}

 

How to set the workflow end time.

 

Thanks

 

@arunpatidar   @Jagadeesh_Prakash 

1 Accepted Solution

Avatar

Correct answer by
Level 7

Thank you @arunpatidar  @Siva_Sogalapalli  for your response really helped me and got to learn the new things reagrding the exection time of workflow.

 

I tried the new way to execute which fulfil my requirement: 

 

Date initiatedDate = new Date();

public static String convertToUtc(Date date) {
    final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:00.00'Z'");
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    return formatter.format(date);
}

setPropertyForNodeAndChildNodes(archived.getNode(payloadName), "archivedDate", convertToUtc(initiatedDate));

private void setPropertyForNodeAndChildNodes(Node node, String propertyName, String propertyValue) throws RepositoryException {
    try {
        if (node.hasNode("jcr:content")) {
            node.getNode("jcr:content").setProperty(propertyName, propertyValue);
        }
        NodeIterator childNodes = node.getNodes();
        while (childNodes.hasNext()) {
            Node childNode = childNodes.nextNode();
            setPropertyForNodeAndChildNodes(childNode, propertyName, propertyValue);
        }
    } catch (RepositoryException e) {
        loggerService.postLog(LogLevel.ERROR, "RepositoryException exception occurred at :: {}" + node.getPath());
    }

 

 

View solution in original post

9 Replies

Avatar

Community Advisor

If you want to complete the step automatically after certain time, you can advance workflow step which completes the workflow:

// querying work items
WorkItem[] workItems = wfSession.getActiveWorkItems();
// getting routes
List<Route> routes = wfSession.getRoutes(workItem);
// completing or advancing to the next step
wfSession.complete(workItem, routes.get(0));

https://experienceleague.adobe.com/docs/experience-manager-64/developing/extending-aem/extending-wor... 

Hi @Siva_Sogalapalli , Thankyou for your response.
But the problem here is need to set the workflow end date and time in node.

Avatar

Community Advisor

@tushaar_srivastava who is going to set the workflow end date- is that author through workflow dialog or you need to set through programatically when the workflow is initiated? 

Also, just to understand better, can you share more details about the workflow steps like when you set end date, should the workflow execute all the steps? or just abort/complete steps without executing the other steps.

 

Sure So I have created a process step where I a move node from one path to another path :

 


public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
  String payload = workItem.getWorkflowData().getPayload().toString();
  ResourceResolver resourceResolver = resourceResolverUtil.getResourceResolver();
  Session session = resourceResolver.adaptTo(Session.class);
  Page rootPage = resourceResolver.getResource(payload).adaptTo(Page.class);
  ReplicationOptions replicationOptions = new ReplicationOptions();
  if (rootPage != null && payload.startsWith(ROOT_PATH)) {
    String workflowInitiator = workItem.getWorkflow().getInitiator();
    List < String > list = new ArrayList < > ();
    list.add(payload);
    Iterator < Page > childPages;
  }
  try {
    Node payloadNode;
    if (session != null) {
      payloadNode = session.getNode(payload);
      //Move logic page from one source to destiantion setPropertyForNodeAndChildNodes(archived.getNode(payloadName), "initiatedBy", workflowInitiator);
    }
    session.save();
    resourceResolver.close();
  }
}

private void setPropertyForNodeAndChildNodes(Node node, String propertyName, String propertyValue) {
  try {
    if (node.hasNode("jcr:content")) {
      node.getNode("jcr:content").setProperty(propertyName, propertyValue);
    }
    NodeIterator childNodes = node.getNodes();
    while (childNodes.hasNext()) {
      Node childNode = childNodes.nextNode();
      setPropertyForNodeAndChildNodes(childNode, propertyName, propertyValue);
    }
  }

 

And like how we are setting programatically the "initiatedBy" and the value is workflow initiator,

Similarly I need to set the valuse "initiatedDate" and the value would be Workflow executed date and time.

Avatar

Community Advisor

If you just want to set the initiatedDate baed on workflow initiation, you can get the start time through workflow API.  https://developer.adobe.com/experience-manager/reference-materials/6-4/javadoc/com/adobe/granite/wor... 

 

Avatar

Community Advisor

Hi @tushaar_srivastava 
What do you mean by end time for workflow?

Could you please explain in details with use case?



Arun Patidar

Hi @arunpatidar  Thanks for your response.

Sure So I have created a process step where I a move node from one path to another path :

 


public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
  String payload = workItem.getWorkflowData().getPayload().toString();
  ResourceResolver resourceResolver = resourceResolverUtil.getResourceResolver();
  Session session = resourceResolver.adaptTo(Session.class);
  Page rootPage = resourceResolver.getResource(payload).adaptTo(Page.class);
  ReplicationOptions replicationOptions = new ReplicationOptions();
  if (rootPage != null && payload.startsWith(ROOT_PATH)) {
    String workflowInitiator = workItem.getWorkflow().getInitiator();
    List < String > list = new ArrayList < > ();
    list.add(payload);
    Iterator < Page > childPages;
  }
  try {
    Node payloadNode;
    if (session != null) {
      payloadNode = session.getNode(payload);
      //Move logic page from one source to destiantion setPropertyForNodeAndChildNodes(archived.getNode(payloadName), "initiatedBy", workflowInitiator);
    }
    session.save();
    resourceResolver.close();
  }
}

private void setPropertyForNodeAndChildNodes(Node node, String propertyName, String propertyValue) {
  try {
    if (node.hasNode("jcr:content")) {
      node.getNode("jcr:content").setProperty(propertyName, propertyValue);
    }
    NodeIterator childNodes = node.getNodes();
    while (childNodes.hasNext()) {
      Node childNode = childNodes.nextNode();
      setPropertyForNodeAndChildNodes(childNode, propertyName, propertyValue);
    }
  }

 

And like how we are setting programatically the "initiatedBy" and the value is workflow initiator,

Similarly I need to set the valuse "initiatedDate" and the value would be Workflow executed date and time.

Avatar

Correct answer by
Level 7

Thank you @arunpatidar  @Siva_Sogalapalli  for your response really helped me and got to learn the new things reagrding the exection time of workflow.

 

I tried the new way to execute which fulfil my requirement: 

 

Date initiatedDate = new Date();

public static String convertToUtc(Date date) {
    final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:00.00'Z'");
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    return formatter.format(date);
}

setPropertyForNodeAndChildNodes(archived.getNode(payloadName), "archivedDate", convertToUtc(initiatedDate));

private void setPropertyForNodeAndChildNodes(Node node, String propertyName, String propertyValue) throws RepositoryException {
    try {
        if (node.hasNode("jcr:content")) {
            node.getNode("jcr:content").setProperty(propertyName, propertyValue);
        }
        NodeIterator childNodes = node.getNodes();
        while (childNodes.hasNext()) {
            Node childNode = childNodes.nextNode();
            setPropertyForNodeAndChildNodes(childNode, propertyName, propertyValue);
        }
    } catch (RepositoryException e) {
        loggerService.postLog(LogLevel.ERROR, "RepositoryException exception occurred at :: {}" + node.getPath());
    }