cancel
Showing results for 
Search instead for 
Did you mean: 

CAP: Select inside Action

MioYasutake
Active Contributor
0 Kudos
984

Hello.

I'm struggling to implement an Action inside CAP.

What I want to do is simple SELECT, but it throws me an error...

Service definition

using { demo.callcenter as call } from '../db/schema';

service CallCenterService {
    entity Inquiries as select from call.Inquiries
    actions {
        @sap.applicable.path: 'startEnabled'
        action start();
    }; 
}

Service implementation

const cds = require('@sap/cds')

module.exports = async function () {
    const { Inquiries } = cds.entities

    this.on ('start', async (req)=> {
        console.log('data: ' + req.data)
        const id = req.params[0]

        //Get createdAt
        const createdAt = await SELECT.from(Inquiries).columns(['createdAt']).where({ID:id})

        //Calculate hoursBeforeStart
        const startedAt = Date.now()
        const difference = startedAt - new Date(createdAt).getTime()
        const hoursBefoerStart =  Math.floor(difference/1000/60/60)        

        const n = await UPDATE(Inquiries).set({ 
            status_code:'2',
            startedAt: Date.now(),
            hoursBefoerStart: hoursBefoerStart
        }).where ({ID:id}).and({status_code:'1'})
        n > 0 || req.error (404) 
    })
}

The error is occurring at below statement.

const createdAt = await SELECT.from(Inquiries).columns(['createdAt']).where({ID:id})

Can anybody point out what's wrong with this statement?

Thanks,

Mio

OlenaT
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Mio,

There is not enough information provided to find the reason behind your issue. Could you please provide us a link to your project or a minimal working example which will help us to reproduce the problem?

Best regards,

Olena

MioYasutake
Active Contributor
0 Kudos

Hi Olena,

Thank you for your attention.

I have uploaded my project to below git repository.

https://github.com/miyasuta/cap-callcenter

You can reproduce the issue by executing the POST request in srv/test.http file.

Best regards,

Mio

MioYasutake
Active Contributor
0 Kudos

I tried debugging below module.

@sap\cds-runtime\lib\db\data-conversion\post-processing.js:128

When below statement was called, nothing was returned to the variable entity.

This is the reason of the error 'TypeError: Cannot read property 'elements' of undefined'.

const entity = csn.definitions[ensureUnlocalized(entityName)]

entityName was 'localized.demo.callcenter.Inquiries', and ensureUnlocalized(entityName) returned 'o.callcenter.Inquiries', which was not in csn.definitions.


Accepted Solutions (1)

Accepted Solutions (1)

MioYasutake
Active Contributor

It was my namespace that was causing the issue: demo.callcenter.

ensureUnlocalized method compares the localized table name with below localized prefixes.

localized.de, localize.fr ....

In my case, localized table name is localized.demo.callcenter.Inquiries and it matches with localized.de, so the method truncates the table name, resulting the error.

I changed the namespace to zdemo.callcenter and now it is working fine.

It seems we cannot use namespaces starting with 'de', 'fr', or any other language keys.

Answers (1)

Answers (1)

carmenpalmisano
Explorer
0 Kudos

Title: ErroreMessage: Non è stato possibile aprire l’app perché il componente SAP UI5 dell’applicazione non è stato caricato.Details: {"info": "Il componente UI5 non è stato caricato per l’intento \"#Action-toappstateformsample\"","technicalMessage": "failed to load 'sap/ushell/demo/AppStateFormSample/Component.js' from ../../../../../test-resources/sap/ushell/demoapps/AppStateFormSample/Component.js: 404 - Not Found\nError: failed to load 'sap/ushell/demo/AppStateFormSample/Component.js' from ../../../../../test-resources/sap/ushell/demoapps/AppStateFormSample/Component.js: 404 - Not Found\n at makeNestedError (https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:94:37)\n at requireModule (https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:102:2782)\n at requireAll (https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:104:527)\n at Object.r [as require] (https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:109:597)\n at https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:671:6952\n at new Promise (<anonymous>)\n at https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:671:6919\nCaused by: Error: 404 - Not Found\n at a (https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:98:88)\n at XMLHttpRequest.<anonymous> (https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:98:321)\n at loadSyncXHR (https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:98:415)\n at requireModule (https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:102:2737)\n at requireAll (https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:104:527)\n at Object.r [as require] (https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:109:597)\n at https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:671:6952\n at new Promise (<anonymous>)\n at https://sapui5.hana.ondemand.com/1.78.6/resources/sap-ui-core.js:671:6919"}