on 2020 Jun 04 1:03 AM
Hi,
I am developing a solution using CAPM.
I am using the S/4 HANA Cloud API API_JOURNALENTRYITEMBASIC_SRV as an external service.
My problem is when I try to consume an EntitySet that returns an attribute of tipe Edm.Date (for example A_CostCenter entityset).
The error is "Invalid value /Date(253402214400000)/ (JavaScript string). A string value in the format YYYY-MM-DD must be specified as value for type Edm.Date."
I think the CAPM framework is expecting a format YYYY-MM-DD, but S/4 HANA Cloud is returning the dates with this format: /Date(253402214400000)/
Here is an image where the error happens:
Does anyone have the same error?
Does exist any fix?
Thanks in advance
Request clarification before answering.
David,
I find the way to do the conversion (basically the `run` method returns a Promise that its resolved value is the JSON response of the external service, already parsed, without doing any validation):
this.on('READ', entity, async req => {
let response = await journalEntryItemBasicSrv.tx(req).run(req.query)
response.forEach( instance => {
if (instance.ValidityEndDate)
instance.ValidityEndDate = ODataV2toODataV4Date(instance.ValidityEndDate)
});
return response;
})
It would be very helpful if the problem is fixed.
Thanks a lot for your support!
Kind regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi jsg24582 ,
Could you please share the ODataV2toODataV4Date function implementation?
david.kunz2 Do you know if this issue is going to be fixed soon?
Thanks
Trinidad.
Hi mariatrinidad.martinezgea,
Here is the code that is doing the magic 🙂
function ODataV2toODataV4DateTime(value){
const groups = value.match(/\/Date\((?<arg>.*)\+.*\)\//).groups
if (!groups)
return value
return new Date(Number(groups.arg))
.toISOString()
}
Best regards.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you Juan!
I just did some small changes as the groups option didn't work in my case.
I took your transformation from Number to ISO, thanks!
The date format I receive from my backend is: '/Date(1605810872000)/'
I share my code here in case other people has the same problems I had 😉
function ODataV2toODataV4DateTime(value) {
var thenum = value.match(/\d+/)[0];
if (!thenum)
return value
return new Date(Number(thenum)).toISOString()
}
Best Regards,
Trinidad.
Dear Juan,
Thanks for reporting this. We'll look into it.
For now, I'm afraid you'll need to do the conversion yourself.
Best regards,
David
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi David,
Thank you for your response.
I think the problem is that the framework is not considering that the external service is OData 2.0.
Then it tries to parse an OData 2.0 JSON Date as an OData 4.0 JSON Date, and the format is very different.
Do you have any suggestion to do the conversion myself?
I am doing the following to fordward the requests to the external services:
const cds = require('@sap/cds')
module.exports = cds.service.impl(async function () {
const journalEntryItemBasicSrv = await cds.connect.to('API_JOURNALENTRYITEMBASIC_SRV')
const { A_CompanyCode, A_GLAccountInChartOfAccounts, A_CostCenter } = this.entities
const entities = [ A_CompanyCode, A_GLAccountInChartOfAccounts, A_CostCenter ]
entities.forEach( entity => {
this.on('READ', entity, req => journalEntryItemBasicSrv.tx(req).run(req.query))
});
})
Thanks in advance.
User | Count |
---|---|
53 | |
9 | |
8 | |
6 | |
6 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.