Get sling service using ECMA script. Workflow goto step.
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();
}
@8220494(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?