Expand my Community achievements bar.

SOLVED

Get sling service using ECMA script. Workflow goto step.

Avatar

Level 3

I've got a workflow GOTO process step and the below ECMA script as the routing expression. 

 

function check() {
  log.info("Inside script to fetch enabled flag");
  var service = sling.getService(Packages.com.adobe.service.TrialServiceInterface);
  if (service && service.isEnabled()) {
    return true
  }
  return false;
}

 

 

Component and Service Code:

TrialServiceInterface is service interface which is implemented by a component as follow.

 

public interface TrialServiceInterface {
    ----
    boolean isEnabled();
}

 

 

 

@component(immediate = true, service = TrialServiceInterface.class)
@Designate(ocd = TrialServiceInterfaceConfig.class)
public class TrialServiceInterfaceImpl implements TrialServiceInterface {
----
---
    
    public boolean isEnabled() {
        LOGGER.debug("Call from ECMA script");
        return enabled;
    }
}

 

 

Exception:

 

30.04.2022 21:58:34.402 *INFO* [JobHandler: /var/workflow/instances/server0/2022-04-30_1/asset-post-processing_3:/content/dam/content/brands/bbasic_BR_201216.png] libs.workflow.scripts.dynamic$ecma Inside script to fetch enabled flag
30.04.2022 21:58:34.412 *ERROR* [JobHandler: /var/workflow/instances/server0/2022-04-30_1/asset-post-processing_3:/content/dam/content/brands/bbasic_BR_201216.png] com.adobe.granite.workflow.core.rule.ScriptingRuleEngine Unable to execute rule function check() {
  log.info("Inside script to fetch enabled flag");
  var service = sling.getService(Packages.com.adobe.service.TrialServiceInterface);
  if (service && service.isEnabled()) {
    return true
  }
  return false;
} : Failure running script /libs/workflow/scripts/dynamic.ecma: Can't find method org.apache.sling.scripting.core.impl.InternalScriptHelper.getService(object). (NO_SCRIPT_NAME#3)
org.apache.sling.api.scripting.ScriptEvaluationException: Failure running script /libs/workflow/scripts/dynamic.ecma: Can't find method org.apache.sling.scripting.core.impl.InternalScriptHelper.getService(object). (NO_SCRIPT_NAME#3)
	at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:423) [org.apache.sling.scripting.core:2.4.6]
	at 


org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporter.java:77)
	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:954)

 

 

Environment: AEM as cloud service

According to these docs sling is one of objects available within ECMA scripts.

AEM gurus, any idea why i'm running into this can't find method exception, pls advice?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @subsul1,

I was able to reproduce the issue you have described. I think I have also find what is the root cause and solution.

The implementation itself look fine. However there is one small details that could be problematic.

Could you please make sure your interface and class are in java pages that are exported by your bundle. In other words it needs to be exposed to be consumed by other services outside your bundle.

You can verify above very easy. Just go to /system/console/bundles find your bundle and check it details, especially Exported Packages section.

Adding package to be exported solved issue in my local instance.

Depending which plugin you are using below links could be useful:

So in general the error shown in the logs could more descriptive and is not helping in the investigation. Nevertheless I hope this will solve your problem.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @subsul1,

I was able to reproduce the issue you have described. I think I have also find what is the root cause and solution.

The implementation itself look fine. However there is one small details that could be problematic.

Could you please make sure your interface and class are in java pages that are exported by your bundle. In other words it needs to be exposed to be consumed by other services outside your bundle.

You can verify above very easy. Just go to /system/console/bundles find your bundle and check it details, especially Exported Packages section.

Adding package to be exported solved issue in my local instance.

Depending which plugin you are using below links could be useful:

So in general the error shown in the logs could more descriptive and is not helping in the investigation. Nevertheless I hope this will solve your problem.

Avatar

Level 3

Hi @lukasz-m 

Many thanks for your time and effort. That too on a Sunday

Exporting both the service and the component has resolved the issue.

 

For those stumbling upon the same problem, here is a ref for Export-Package usage using bnd-maven-plugin

https://wcm-io.atlassian.net/wiki/spaces/WCMIO/pages/1267040260/How+to+switch+from+maven-bundle-plug...

 

Regards,

Sub