To check if a record exists, you can simply use the HEAD query.
“HEAD is almost identical to GET, but without the response body.” (https://www.w3schools.com/tags/ref_httpmethods.asp)
Unfortunately, there is no shortcut in CAP for a HEAD
query, so simply use send
with method HEAD
.
sfsfSrv = await cds.connect.to('sfsf')
try {
await sfsfSrv.send('HEAD', `/User('${userId}')`)
return true // status 200
} catch (err) {
return false // status 404
}