Web API to Process model integration

 Hi Folks, 

 

I'm trying to achieve the following:

  • Create WebAPI for JSON request - this Web API accepts a JSON request 
  • After accepting a JSON-type input - a process model is initiated to which this JSON value is passed 
  • If the process initiation is successful, a success response is sent back as acknowledgement 

Any inputs would be great, as I'm just beginning, I can implement more efficient code.  

 

Thanks in advance!!

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Hi,

    Your web api code should be something like below:

    with(
    a!startProcess(
    processModel: cons!yourProcessModel,
    processParameters: a!fromJson(
    http!request.body
    ),
    onSuccess: a!httpResponse(
    statusCode: 200,
    headers: {
    a!httpHeader(name: "Content-Type", value: "application/json")
    },
    body: a!toJson(
      {"Your Success Message"}
    )
    ),
    onError: a!httpResponse(
    statusCode: 500,
    headers: {
    a!httpHeader(name: "Content-Type", value: "application/json")
    },
    body: a!toJson(
    {
    error: "There was an error starting the process"
    }
    )
    )
    )
    )

  • 0
    Certified Lead Developer
    Adding to @Adityau comment, also make sure to choose the method as POST while creating the web API
  • Steps:

    -> Go to designer, inside designer go to your application
    -> Select create new Web api
    -> Give the name to the web api. description and end point which should be unique.Also set the security of the web api to appropriate
    user.
    -> Also the method type of the web api should be post because when we trigger a smart service from a web api ,the method should
    always be post
    -> Once the above steps are done and you proceed you will get multiple web api templates ,from that select "Start Process Model"
    template
    -> A predefined code will occur then in that code make the alteration like setting the property processModel with a constant which holds
    your process model name and processParameters to a!fromJson(http!request.body),
    -> Done


    Hope this will help you
  • Additionally to the steps, you have to keep in mind that you can configure the response on the web Api as you want, it could be based on the start of the process or you can validate structure, information, etc. within the web api.
  • Responding to an old thread as I ran in to a problem related to this.

    I have a Web API that accepts JSON request with few attributes. One of the attribute in the request is in JSON format and I ran in to issue while passing this value to process model. Below is the request format.

    "payload" attribute is mapped to a text variable in process model but the variable value is in dictionary format and not able to retrive each values from this variable (like Attribute2_2_1) in the process model.

    Is a there way I can retain the JSON format in process model variable ? I have already tried converting this to a JSON using a!toJson/a!fromJson but unable to convert the format. Thank you.


    {
    "Request":
    {
    "Attribute1":"value",
    "payload":
    {
    "Attribute2_1":"value",
    "Attribute2_2":
    {
    "Attribute2_2_1":"value",
    "Attribute2_2_2":"value"
    },
    "Attribute3":"value"
    }
    }
    }

    Value in Payload variable on Process Model:

    [payload:[Attribute2_1:value, Attribute2_2:[Attribute2_2_1:value, Attribute2_2_2:value], Attribute3:value]]
  • Hello,

    Did you find a way to have the format correctly in variable?

  • It isn't possible to create process variable of "Dictionary" or "Any Type", so your best bet is to create a custom data type that matches the structure of this JSON request. Then, create a variable using that CDT and it will preserve the structure in your process variable.

  • Yes. Extracted the values from the request and assigned to Process parameter. This way you can get the actual request in JSON format in to the process model. Sample below.

    Request :

    {
    "Request":
    {
    "Attribute1":"value",
    "payload":
    {
    "Attribute2_1":"value",
    "Attribute2_2":
    {
    "Attribute2_2_1":"value",
    "Attribute2_2_2":"value"
    },
    "Attribute3":"value"
    }
    }
    }

    WebAPI:

    a!startProcess(

             processModel:cons!PM_NAME,

            processParameters:

             {

                       Attribute1:a!fromJson(http!request.body).Request.Attribute1,

                       payload:a!toJson(a!fromJson(http!request.body).Request.payload)

              }

    )