How to make an API call to a REST ws from an Alfresco workflow?

cancel
Showing results for 
Search instead for 
Did you mean: 
tlosada
Active Member

How to make an API call to a REST ws from an Alfresco workflow?

Jump to solution

I have followed this tutorial: https://ecmarchitect.com/alfresco-developer-series-tutorials/workflow/tutorial/tutorial.html For now, I have created a HelloWorld workflow and deployed it successfully using Alfresco Maven SDK for Alfresco 6.2. This is the part of corresponding .bpmn file that prints Hello World! in server's log:

    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1">
        <extensionElements>
            <activiti:executionListener event="start" class="org.alfresco.repo.workflow.activiti.listener.ScriptExecutionListener">
                <activiti:field name="script">
                    <activiti:string>logger.log("Hello, World!");</activiti:string>
                </activiti:field>
            </activiti:executionListener>
        </extensionElements>
    </sequenceFlow>

However, I need to make an API call to https://google.com, just for testing purposes. But, when I try this:

    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1">
        <extensionElements>
            <activiti:executionListener event="start" class="org.alfresco.repo.workflow.activiti.listener.ScriptExecutionListener">
                <activiti:field name="script">
                    <activiti:string>
                        var url = "https://google.com";
                        
                        var xhr = new XMLHttpRequest();
                        xhr.open("GET", url);
                        
                        xhr.onreadystatechange = function () {
                           if (xhr.readyState === 4) {
                              logger.log(xhr.status);
                              logger.log(xhr.responseText);
                           }};
                        
                        xhr.send();                 
                    </activiti:string>
                </activiti:field>
            </activiti:executionListener>
        </extensionElements>
    </sequenceFlow>

I get this exception:

org.alfresco.scripts.ScriptException: 01030043 Failed to execute supplied script: 01030042 ReferenceError: "XMLHttpRequest" is not defined. (AlfrescoJS#3)
    at org.alfresco.repo.jscript.RhinoScriptProcessor.executeString(RhinoScriptProcessor.java:287)
    at org.alfresco.repo.processor.ScriptServiceImpl.executeString(ScriptServiceImpl.java:293)
    at org.alfresco.repo.processor.ScriptServiceImpl.executeScriptString(ScriptServiceImpl.java:200)

Investigating, I checked this post: https://hub.alfresco.com/t5/alfresco-content-services-forum/calling-rest-service-via-rule-script/td-.... It seems (from my understanding) that I need to make the API call using a Java class and then, somehow, invoke that object from js on the .bpmn file. So:

  1. Could you please tell me where do I have to create this class in my Maven project? Is it a package? Could you tell me the exact steps on how to create it?
  2. Could you paste here the exact code that implements this class?
  3. Could you tell me how to invoke the java object from js in the .bpmn file?

IMPORTANT: I need to log the API Call response some way. It could be either log that you want, but I need to know if Google is answering me accordingly.

Thanks in advance!

1 Solution

Accepted Solutions
jpotts
Professional

Re: How to make an API call to a REST ws from an Alfresco workflow?

Jump to solution

Hi, thanks for reading the tutorial! You've posted almost the exact question here already and twice on stackoverflow.

I tried to help you out on SO. The summary of my advice is to not use JavaScript to make this call from Activiti but instead use a Java delegate. I included some code snippets.

In case you ever do need to make a remote API call from a web script rather than a workflow, I've included how to define the connector and make that call as well.

View solution in original post

2 Replies
jpotts
Professional

Re: How to make an API call to a REST ws from an Alfresco workflow?

Jump to solution

Hi, thanks for reading the tutorial! You've posted almost the exact question here already and twice on stackoverflow.

I tried to help you out on SO. The summary of my advice is to not use JavaScript to make this call from Activiti but instead use a Java delegate. I included some code snippets.

In case you ever do need to make a remote API call from a web script rather than a workflow, I've included how to define the connector and make that call as well.

tlosada
Active Member

Re: How to make an API call to a REST ws from an Alfresco workflow?

Jump to solution

Thank you very much and excuse me for all of my questions. I am new to Alfresco and not a developer jeje.

This is the answer I wanted! More on this video: