How to call a model class from the workflow process step ? | Community
Skip to main content
Mario248
Level 7
January 30, 2023
Solved

How to call a model class from the workflow process step ?

  • January 30, 2023
  • 1 reply
  • 1455 views
I have a custom workflow that will be used to publish pages. As part of the same workflow I want to send the API details to an external system. So I am planning to use the WorkflowProcess step, this workflow process step needs to call model class and get the json response. 
 
Here is the model class that uses Jackson exporter to export aem page content into json, sample model class - http:4502/content/tco/visa/jcr:content/root/container.model.json
 
Do we have any OOTB method or APIs available to make a model class call from WorkflowProcess?
 
Model Class
 
@Model( adaptables = { SlingHttpServletRequest.class, Resource.class }, adapters = ProductDetail.class, resourceType = ProductDetailImpl.RESOURCE_TYPE) @Exporter( name = CommonConstants.JACKSON, extensions = CommonConstants.JSON, options = { @ExporterOption( name = CommonConstants.SERIALIZATION_FEATURE + "." + CommonConstants.WRAP_ROOT_VALUE, value = CommonConstants.TRUE) }) @JsonRootName("ProductDetail") public class ProductDetailImpl implements ProductDetail { ... }
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Peter_Puzanovs

Hi Mario,

 

Inside execute method of your workflow, you can use ResorceResolver rr; to get resource and then ask adaptTo method to get your Model.

 

Something along these lines would work:

 

Resource productResource = rr.getResourceResolver().getResource(workflow.getWorkflowData().getPayload().toString());

ProductDetail product = productResource.adaptTo(ProductDetail.class);

 

Regards,

Peter

 

1 reply

Peter_Puzanovs
Community Advisor
Peter_PuzanovsCommunity AdvisorAccepted solution
Community Advisor
January 30, 2023

Hi Mario,

 

Inside execute method of your workflow, you can use ResorceResolver rr; to get resource and then ask adaptTo method to get your Model.

 

Something along these lines would work:

 

Resource productResource = rr.getResourceResolver().getResource(workflow.getWorkflowData().getPayload().toString());

ProductDetail product = productResource.adaptTo(ProductDetail.class);

 

Regards,

Peter

 

Mario248
Mario248Author
Level 7
January 30, 2023

Thanks for your response. Yeah, I could adapt resource object to my model class. But how to get the json response with "product" obj? I could see all the methods of "product" object but there is no method available to get all field as json value. Can you guide me please ?

Peter_Puzanovs
Community Advisor
Community Advisor
January 30, 2023

Hi Mario,

 

Once you get hold of your object you can ask Jackson to render the object for you:

 

JsonNode jsonNode = new ObjectMapper().valueToTree(product);

String json = jsonNode.writeValueAsString(jsonNode);


Regards,

Peter