Skip to Content
Technical Articles

Manage (Start/Cancel) SCP Neo Workflows from CPI

Hello Community,

If you’ve worked with recent SAP® Tech, you’ll know about X-CSRF-Token (find out more here). Using Neo SCP Workflows as an example, in order to start or cancel a Workflow, you need to execute 2 API Calls:

1 – Getting the Token;

2 – Executing the action (Start, Cancel, etc.) passing the Token;

Find out more about the API here at the API Business Hub (other available options, etc):

So basically my goal here is to “extend” a previous Blog that combines both(Token and Start) calls in a iFlow, to also Cancel the Workflow at the same iFlow if needed.

Here is what we’re gonna do:

Before we start, you need to follow the tutorial here in order to get your Neo SCP Trial Account and Workflow setup/creation (in case you don’t have it yet).

Final iFlow:

Did it on a “graphical” way, in order to be clear what we’re doing. 🙂

On my Neo Account, i’ve created a very simple Workflow called zsample_project, and deployed:

You’ll need the ID later:

Before creating the iFlow, don’t forget to create credentials for your Neo Trial Account here:

Mine is named NEOWF.

Let’s create the iFlow now, i’ve used the named WF_NEO_MANAGE:

Place the DeleteID as Allowed Header:

Sender adapter as HTTPs, Address called /NEOWF.

Next, i’ve created a Groovy Script to collect the DeleteID header and set 2 properties (DeleteME and DeleteID):

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    //Headers 
      def map = message.getHeaders();
      def value = map.get("DeleteID");
      if (value) {
        message.setProperty("DeleteME", "Yes");          
        message.setProperty("DeleteID", value);
      } else {
        message.setProperty("DeleteME", "No");    
      }
      return message;
}

Next, i’ve create a Content Modifier to place the X-CSRF-Token = Fetch header, in order to make the first call to API and get the Token:

Also created some Properties:

Next, created a Request Reply to the Workflow Token URL.

How can you find your URL? After you’ve setup Workflow service on your Neo environment, check out the bpmworkflowruntime Destination, there you’ll find your URL:

Next, created another Content Modifier like this:

Here, you should place your WF ID as the definitionId.

{
  "definitionId": "zsample_project",
"context": ${property.requestPayload}
}

 

Next, created another Groovy Script (just like the original Blog), to pass the Cookies received on the Token call to the next call (Start or Cancel):

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap
import java.util.ArrayList
import java.util.Map
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import groovy.xml.*
import java.io.*;
def Message processData(Message message) {
     def headers = message.getHeaders();
     def cookie = headers.get("Set-Cookie");
     StringBuffer bufferedCookie = new StringBuffer();
    
     for(Object item : cookie){
          bufferedCookie.append(item + ";");
     }
    
     message.setHeader("Cookie", bufferedCookie.toString());
     Logger log = LoggerFactory.getLogger(this.getClass());
     log.error("cookie"+ bufferedCookie);
     
      return message;    
    }

Now, we’re going the create the part that “decides” if we want to Start or Cancel a WF. Create a Router:

We’re gonna do it like this:

On the “Start” path, the HTTP receiver:

The final URI changes a little bit from the Token one, now it’s /rest/v1/workflow-instances.

Now on the “Cancel” path, Content Modifier:

Code:

{
  "status": "CANCELED"
}

HTTP Receiver:

It’s the same endpoint as the creation one, but passing the WF ID on the end and executing a PATCH.

Don’t forget to user your WF URL, from your Neo account, in all HTTP calls. 🙂

Again, in the end it should look something like this:

Now let’s test it, using Postman:

This will be the Context of your Workflow, what you pass here should be used on the WF instance created. I’m just showing this 2 values on the decision form:

{
  "User": "Jose Sequeira", 
  "Email": "josesequeirasap@gmail.com"
}

Executing, 201 response:

Checking the Workflow Instances, it’s there:

Available for Approval:

Now let’s Cancel it, passing the generated WF ID on the deleteid header:

Executing, 202 response:

Workflow instance is now Cancelled:

Enjoy!

Regards.

Be the first to leave a comment
You must be Logged on to comment or reply to a post.