on 2022 Nov 14 7:46 PM
Hi Experts,
We have a requirement to process (bulk load) some requests using customs action in CAP JAVA. We need to validate these requests and process only the correct ones and skipping the errored ones.
We have a validate function which currently collects any messages.errors and returns true or false. we don't throw any errors
requests.forEach(request -> {
if (validateRequest(request)) {
context.getCdsRuntime().changeSetContext().run(ctx -> {
// all good so far
x newRequest = db.run(Insert.into(x_.class).entry(request)).single(x.class);
logger.debug(" Created Record " + newRequest.toJson());
outList.add(newRequest);
Released event = Released.create();
event.setId(newRequest.getId());
// set event context
ReleasedContext evContext = ReleasedContext.create();
evContext.setData(event);
// Emit event
adminService.emit(evContext);
});
}
});
messages.throwIfError();
context.setResult(outList)
The problem is i don't see this processing all the requests when there are some invalidate ones in the list. it will process some but then cancel some valid ones.
In the validate Request function, we are just collecting messages.Error to use later in the process.Is this the correct way to process multiple requests??? Basically we want to validate and insert only the correct ones and return a response to what happened to each request.
Any suggestions/help appreciated
marcbecker : any suggestions/ help Please.
Cheers
Dharmesh
Request clarification before answering.
Can you say something more specific on validateRequest? Does it return false in case there is an error message? If yes it would explain that after the first request which adds to messages.error the loop is exited. Directly afterwards the handler will then throw due to messages.throwIfError().
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Matthias
In the validateRequest function, errors are collected and the function return a boolean. We are not calling throwifError() till the loop is completed. Is there a reason why the loop is exited?? do we need to move the call to validateRequest into its own changeContext???
Thanks
Dharmesh
Hi Marc,
The validateRequest function collects errors and success messages and returns a boolean. What seems to happen is it will process some requests successfully then on rolls back. Say we have 4 requests.
Request 1 - returns False form validateRequest. A error Message likely added to the Messages API.
Request 2 - 4 return true. All are processed but request 4 would rollback because request 1 caused an exception?
I have since changed my code to this. This seems to work but open to suggestions/ comments.
Thanks
D
requests.forEach(request -> {
context.getCdsRuntime().changeSetContext().run(ctx -> { if (validateRequest(request)) {
// all good so far
x newRequest = db.run(Insert.into(x_.class).entry(request)).single(x.class);
logger.debug(" Created Record " + newRequest.toJson());
outList.add(newRequest);
Released event = Released.create();
event.setId(newRequest.getId());
// set event context
ReleasedContext evContext = ReleasedContext.create();
evContext.setData(event);
// Emit event
adminService.emit(evContext); } });
});
messages.throwIfError();
context.setResult(outList)
| User | Count |
|---|---|
| 18 | |
| 7 | |
| 6 | |
| 6 | |
| 6 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.