Call Model Inside Service | Community
Skip to main content
May 28, 2021
Solved

Call Model Inside Service

  • May 28, 2021
  • 3 replies
  • 1599 views

Hi,

We are trying to create a filter component (that filters results based on tags). Query should accept parent page from which the pages will be shown as a list. We got the value that is given as input on the component. We got it using Sling Model. We are trying to pass it to Service class, in order to pass it to Query. But, when we call the methods of model, all return null. Thanks!

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 Asutosh_Jena_

Hi @ad-engineer 

 

You can call the service class inside the Sling Model using below annotation where QueryResultsService is a custom service that is implemented to filter out the results based on the tags.

 

@OSGiService
private QueryResultsService queryService;

 Which one is returning null?

3 replies

Asutosh_Jena_
Community Advisor
Asutosh_Jena_Community AdvisorAccepted solution
Community Advisor
May 28, 2021

Hi @ad-engineer 

 

You can call the service class inside the Sling Model using below annotation where QueryResultsService is a custom service that is implemented to filter out the results based on the tags.

 

@OSGiService
private QueryResultsService queryService;

 Which one is returning null?

May 28, 2021

Hi @asutosh_jena_ 

Thanks for your reply!

I tried this one, but this returns null too.

The errorr: 

org.apache.sling.scripting.sightly.SightlyException: org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Identifier com.project.core.models.Filter cannot be correctly instantiated by the Use API

 

Thanks!

Asutosh_Jena_
Community Advisor
Community Advisor
May 28, 2021
Can you psate your model class code here? I believe there are few mandatory fields which are not injected during sling model instantiation. You can make the class as optional and try it out.
May 30, 2021

Hi @asutosh_jena_,

I do not want to access Service from Sling Model, but Sling Model from Service. Thanks!