Hi,
- Create Order triggers the Rest End point and starts the workflow (Its a TASK ). CreateOrderController
Problem is CreateOrderController is always returning Success.I want to return ResponseEntity.ok("Not Success ");
as shown in 2nd image and stop the call of Save Order Database
How to achieve it?
@RestController
public class CreateOrderController {
@Autowired
private RuntimeService runtimeService;
@PostMapping(“/rest/create/order”)
public ResponseEntity<?> createOrder(@RequestBody OrderInfo orderInfo) {
Map<String, Object> inputData = new HashMap<String, Object>();
inputData.put(“orderInfo”, orderInfo);
ProcessInstance p = runtimeService.startProcessInstanceByKey(“hello-world-process”,inputData);
return ResponseEntity.ok("Success");
}
Hi @my_camunda_profie_1,
have a look at this example: camunda-7-code-examples/snippets/async-on-error at master · camunda-consulting/camunda-7-code-examples · GitHub
It may help you to get a different response in case of an error in the service tasks.
Hope this helps, Ingo
@Ingo_Richtsmeier
My requirement is differenta bit. I don’t need to throw error.I have posted the code here.It;s a samll workflow.Can u help
GitHub - rahul-raj-1/camunda-rnd-repo: RnD repo
I think that you’re having a few problems that are all inter-related with not fully documenting what you’re hoping for before you try to do it.
What you have right now is something like this:
Since you aren’t checking the OrderID before you call the Order Process, the Order Process will always start. Since you aren’t checking the outcome of the Order Process, your custom REST interface will always return SUCCESS.
Since your Step1Delegate isn’t actually throwing an error, it’s also returning success.
I think you want to do something more like this:
@GotnOGuts Thanks for the reply.Did u get the chance to see the github README and code.Its simple code.
I want to return process variable from camunda delegate in response body
GitHub - rahul-raj-1/camunda-rnd-repo: RnD repo
@my_camunda_profie_1 yes, I did, but since I don’t write code, I can’t tell you what you need to do to fix it.
The first diagram that I sent is what I understood from your code.
For example:
To implement the first < X > in the REST Controller interface, in your file CreateOrderController.java around line 24, add your IF check
if(orderId < 0 )
{
// Return an Error Message to user
OrderResponse orderResponse = new OrderResponse();
orderResponse.setOrderId(orderId);
orderResponse.setMessage("ORDER COULD NOT BE PROCESSED");
} else {
// This is the default flow (see / on arrow in diagram)
//Add OrderID to the parameters that will be sent with the request to start the process
Map<String, Object> parameters = new HashMap<>();
parameters.put("orderId", orderId);
//Call Camunda Process
runtimeService.startProcessInstanceByKey("poc-workflow",parameters);
System.out.println("done");
//TODO: Check Response from Camunda Here!
//TODO: Only send Success if Process did not return an error
OrderResponse orderResponse = new OrderResponse();
orderResponse.setOrderId(orderId);
orderResponse.setMessage("SUCCESS");
}
@GotnOGuts Ur solution will work but i have simplified the issue .I need to do same in Step2 also…and there are many steps in real case.I m just using simplified case for simplicity.
I think i found the solution below using HistoricVariableInstance
provided by Camunda.
And in my Step1Delegate class in if clause i set execution.setVariable("orderResponse", orderResponse);
so that value updated by Delegate class for orderResponse object is avilable in comtroller too
Component
public class Step1Delegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
int orderId = (int) execution.getVariable("orderId");
System.out.println(" Step1Delegate : " + orderId);
Thread.sleep(20000);
if(orderId < 0 )
{
// If orderId is "-ve" then RestController should return the below orderResponse object
// and the workslow should end i.e it should not go to step 2
OrderResponse orderResponse = new OrderResponse();
orderResponse.setOrderId(orderId);
orderResponse.setMessage("FAILED DUE TO INVALID ORDER ID ");
execution.setVariable("orderResponse", orderResponse);
return;
}
}
The usual way to deal with that sort of thing is to do:
Where your Check Data step can be a Function (could even be in a different library) that returns 1/0 or True/False.
so you do (pseudocode)
if ( CheckData(OrderID, SomeOtherData, AccountToCheck) ) {
//Process the Data
} else {
execution.setvariable("DataState","Invalid");
throw new exception("The data is bad");
//for example: Invalid OrderID
}