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 call a model class from the workflow process step ?

Avatar

Level 9
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
{
    ...
}
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

 

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

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

 

Avatar

Level 9

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 ?

Avatar

Community Advisor

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