cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SAP Fiori validation before commit

TatianaFetecua
Explorer
0 Likes
601

Hi,

I have a fiori elements list report, and I want to validate data before doing the commit (I am using draft), I have found in the documentation that this could be done in a hook that was introduced in Feb 2022, req.before('commit'), I implemented this code but it is not working, does any one have an example or have used this before? did it worked? any other option to do a validation before saving the data (not when entering data)

based on the documentation it should be implemented like this:

srv.before('CREATE', Order, function(req) {

req.before('commit', async function() {

const { creditscore } = await SELECT.one.from(Customers)

.where({ ID: req.data.customer_ID })

if (creditscore < 42) throw new Error("We shouldn't make this sale")

})

})

but in my example it cannot find the before function property in the request (req.before), I also tried with the service, but it is not working either.

srv.before('COMMIT', Order, function(req) {}

Thanks,

Tatiana F.

Accepted Solutions (0)

Answers (1)

Answers (1)

Dinu
Active Contributor
0 Likes

I would suggest that you reconsider your approach.

When the user saves data, that is what is commit from the User's perspective, data is copied from draft area to the active area. This is when your validations should run for best experience. The validation logic should be attached to SAVE event for this. SAVE is actually UPDATE or INSERT.

See sample code for doing this.

Torben_Jakobsen
Explorer
0 Likes

The linked sample file has changed type from JavaScript to TypeScript.
See https://github.com/SAP-samples/cap-sflight/blob/main/srv/travel-service.ts