Using JS SDK helpers in Task Scripting?

Was working with execution variable in Scripting tasks and was navigating through the ProcessEngineServices provided by DelegateExecution, and was wondering if there was a way to leverage the API wrapper that camunda-bpm-sdk-js provides. Obviously not every scenario is likely covered by the js sdk. but a lot of common requests are covered.

As an example, even just the process data inside of the camForm object is super valuable for use. But also all of the api wrappers around the camund api are useful as well.

Any thoughts?

As an example, if you wanted to collect all of the Process, Deployment etc info, you end up having to run something like this (correct me if this info is located in a easier spot):

var obj = new Object();
obj.process = {};
obj.deployment = {};

obj.process.processDefinitionId = execution.getProcessDefinitionId();

obj.process.processInstanceId = execution.getProcessInstanceId();

obj.deployment.deploymentid = execution.getProcessEngineServices().getRepositoryService().getProcessDefinition(obj.process.processDefinitionId).getDeploymentId();

obj.deployment.deploymentVersionTag = execution.getProcessEngineServices().getRepositoryService().getProcessDefinition(obj.process.processDefinitionId).getVersionTag();

obj.deployment.deploymentVersion = execution.getProcessEngineServices().getRepositoryService().getProcessDefinition(obj.process.processDefinitionId).getVersion();

JSON.stringify(obj);

Hi Stephen,

there is no endpoint to gather aggregated statistics on process\execution\definition, but there are API calls providing you objects with corresponding fields on every specific part

https://docs.camunda.org/manual/7.5/reference/rest/process-definition/
https://docs.camunda.org/manual/7.5/reference/rest/execution/get/
https://docs.camunda.org/manual/7.5/reference/rest/process-instance/get/

every call to those endpoints can be executed from scripts, then you just have to map the output data properly. Does that help you? If you would like to have separate endpoint with aggregated values, we can create a feature request and add it to the backlog.

If you would like to just invoke all methods one by one in js, you can iterate over execution object functions and invoke them using for (var function in execution) construction, I assume. I hope that helps, if it doesn’t could you attach sample project, so that I can better understand your problem?

@aakhmerov, I am not sure I understand your response, based on my question. (i may be miss-understanding).

Rephrase: Was looking at the JS SDK, and there are many functions/wrappers in the SDK that could be very helpful in Task Scripting that would save a lot of config/scripting. Is there a way to use the JS SDK from within Scripting Tasks rather than using the Java API or having to write full HTTP calls that do not use the SDK. ?

Thanks!

@StephenOTT, I think I’ve got you now, no, there is no way to use JS SDK inside of scripted task execution. You can write JS script that invokes different services accessible through execution.getProcessEngineServices() but you won’t have CamSDK.client or other objects defined in sdk. In general, CamSDK.client is invoking REST endpoints which are delegating implementation to the same services, so they should be 100% interchangeable, did you find something missing?

about your example, I think it should be possible to invoke execution.getProcessDefinition() and wrap that object in JSON, which would save you some typing, or execution.getProcessDefinition().getDeploymentId()

about your example, I think it should be possible to invoke execution.getProcessDefinition() and wrap that object in JSON, which would save you some typing, or execution.getProcessDefinition().getDeploymentId()

In the docs: DelegateExecution (camunda BPM Javadocs 7.5.9-ee), the docs just link to itself. What interface does getProcessInstance() return? Where is the docs on the methods that are available to the getProcessDefinition() method?


agreed. Why i was looking at CamSDK, was because CamSDK seemed to have nicer “api” to work with, and the REST API is already returning everything as JSON rather than the internal java objects from the Java API. The REST API also seemed to resolve some of the more “tricky” scenarios such as the following: Ability to get deployment resources' IDs without resource bytes? Java API (using scripting)

edit: made some corrections to the first question.

@aakhmerov the only ref in the docs i could find was:
https://docs.camunda.org/javadoc/camunda-bpm-platform/7.5/org/camunda/bpm/engine/impl/context/ExecutionContext.html

But the docs say those are deprecated.

@StephenOTT DelegateExecution is an interface, method invocation will get specific class instance, that implements this interface here is JavaDoc

https://docs.camunda.org/javadoc/camunda-bpm-platform/7.5/org/camunda/bpm/engine/impl/persistence/entity/ExecutionEntity.html
https://docs.camunda.org/javadoc/camunda-bpm-platform/7.5/org/camunda/bpm/engine/impl/pvm/runtime/ExecutionImpl.html
https://docs.camunda.org/javadoc/camunda-bpm-platform/7.5/org/camunda/bpm/engine/impl/pvm/runtime/PvmExecutionImpl.html

as script execution is not typesafe you would be able then to just invoke all those methods in implementation class without any classcasts.

You can see links to implementations under All Known Implementing Classes: in https://docs.camunda.org/javadoc/camunda-bpm-platform/7.5/org/camunda/bpm/engine/delegate/DelegateExecution.html

to be more specific, in script task you will get an instance of class org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity as input parameter.

1 Like

@aakhmerov thank you for the detailed response!

:head_bandage: I did not look within “All Known Implementing Classes”. I was focused on “Methods inherited” sections. (i looked for a meme, but could not find a appropriate one).

Thanks!

@StephenOTT ja, javaDocs are always a bit painful to read :slight_smile: Glad I could help. I’ll take a look at second topic of yours tomorrow if noone picks it up.