<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Cannot create draft for Node.js CAP Service in custom handler in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663537#M4750982</link>
    <description>&lt;P&gt;Hello Olena,&lt;/P&gt;&lt;P&gt;Thanks again for your response.  I am definitely not trying to re-implement the out-of-the-box draft handling.  As noted in my original post, when I call the draft handling via a POST call from an external HTTP client, it behaves exactly as expected.  All I am wanting to do is trigger the same POST behavior, but from within my custom code.&lt;/P&gt;&lt;P&gt;With regard to the &lt;STRONG&gt;srv.send &lt;/STRONG&gt;API, inn the example above, my data set is empty (empty brackets).  I did this as this is allowed for a POST from an external HTTP client (as demonstrated in my original post).  If I pre-fill data for the &lt;STRONG&gt;srv.send&lt;/STRONG&gt; call:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;const cds = require("@sap/cds")

module.exports = cds.service.impl(async (srv) =&amp;gt; {
  srv.on("createDraft", async (req) =&amp;gt; {
    const { Customers } = srv.entities

    //const { ID } =  await srv.post(Customers, {})

    const generatedID = cds.utils.uuid()
    const customerID = 1234
    const customerName = "Example Customer"
    const ID = await srv.send("POST", Customers, { ID: generatedID, CustomerID: customerID, CustomerName: customerName })
    return { ID }
  })
})&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I still receive the same error message from &lt;STRONG&gt;crud.js&lt;/STRONG&gt;:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;[INTERNAL ERROR] TypeError: Cannot read properties of undefined (reading 'INSERT')
    at ApplicationService.&amp;lt;anonymous&amp;gt; (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/libx/_runtime/common/generic/crud.js:46:32)
    at next (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:75:36)
    at ApplicationService.handle (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:79:6)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ApplicationService.&amp;lt;anonymous&amp;gt; (/Users/stevenmason/dev/cap/draft-concern/srv/customer-service.js:12:16)
    at async next (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:75:17)
    at async ApplicationService.handle (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:73:10)
    at async /Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/handlers/action.js:52:16&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Am I missing something with regard to providing data to the &lt;STRONG&gt;srv.send&lt;/STRONG&gt; call that is causing this issue?&lt;/P&gt;&lt;P&gt;Thanks again,&lt;/P&gt;&lt;P&gt;Steven&lt;/P&gt;</description>
    <pubDate>Mon, 05 Dec 2022 18:34:52 GMT</pubDate>
    <dc:creator>Steven-Mason</dc:creator>
    <dc:date>2022-12-05T18:34:52Z</dc:date>
    <item>
      <title>Cannot create draft for Node.js CAP Service in custom handler</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaq-p/12663533</link>
      <description>&lt;P&gt;I have created a simple project to showcase the issue that I am having at repo: &lt;A href="https://gitlab.com/the-mason-abides/draft-concern" target="_blank"&gt;draft-concern&lt;/A&gt;.&lt;/P&gt;
  &lt;P&gt;In this example repo, I have created a very simple service with entity project that has been annotated with @odata.draft.enabled. In the same service, I have created an additional action (createDraft) to add my custom handler code.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;using { Customers as schemaCustomers } from '../db/schema';

service CustomerService{
  entity Customers as projection on schemaCustomers;
  annotate Customers with @odata.draft.enabled;

  action createDraft() returns {ID: UUID};
  
}
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;If I run a simple post command to the Customer entity set, a draft is created as expected:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;curl -X POST -H "Content-Type:application/json" -d '{}' &lt;A href="http://localhost:4004/customer/Customers" target="test_blank"&gt;http://localhost:4004/customer/Customers&lt;/A&gt;
{"@odata.context":"$metadata#Customers/$entity","ID":"1a8b20eb-c044-422e-a137-d7c724c6f91d","CustomerID":null,"CustomerName":null,"IsActiveEntity":false,"HasActiveEntity":false,"HasDraftEntity":false}%&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Specifically, you will see that the &lt;STRONG&gt;IsActiveEntity&lt;/STRONG&gt; field is set to &lt;STRONG&gt;false &lt;/STRONG&gt;since the draft has not been activated yet.&lt;/P&gt;
  &lt;P&gt;The issue I am running into in my project is when I try to replicate this behavior in a custom handler. In this case I have a very simple handler for the "createDrafts" action defined on the service.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;const cds = require("@sap/cds")

module.exports = cds.service.impl( async (srv) =&amp;gt; {
  srv.on('createDraft', async (req) =&amp;gt; {
    const { Customers } = srv.entities
    const { ID } =  await srv.post(Customers, {})
    return { ID }
  })
})&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;When I call this action against the service, the behavior is&lt;STRONG&gt; not &lt;/STRONG&gt;as expected:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;curl -X POST -H "Content-Type:application/json" -d '{}' &lt;A href="http://localhost:4004/customer/createDraft" target="test_blank"&gt;http://localhost:4004/customer/createDraft&lt;/A&gt;
{"@odata.context":"$metadata#CustomerService.return_CustomerService_createDraft","ID":"9d552f07-275c-4a7f-a79f-0fb7e59d94f9"}%

curl -X GET &lt;A href="http://localhost:4004/customer/Customers" target="test_blank"&gt;http://localhost:4004/customer/Customers&lt;/A&gt;
{"@odata.context":"$metadata#Customers","value":[{"ID":"9d552f07-275c-4a7f-a79f-0fb7e59d94f9","CustomerID":null,"CustomerName":null,"IsActiveEntity":true,"HasActiveEntity":false,"HasDraftEntity":false}]}%
                                         &lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Here you will see that the &lt;STRONG&gt;IsActiveEntity&lt;/STRONG&gt; field has been set to &lt;STRONG&gt;true &lt;/STRONG&gt;even though the draft has not been activated. &lt;/P&gt;
  &lt;P&gt; While in this example, I am using the post convenience method for a CAP service in my custom handler, the same results have occurred regardless of the way I have attempted a post on the Customer entity set via a custom handler. &lt;/P&gt;
  &lt;P&gt;This appears broken to me due to the fact that an direct POST command to the Customers entity set is not behaving in the same manner as the post method within the custom action of the service.&lt;/P&gt;
  &lt;P&gt;Any help on how to properly mimic the POST command within a custom handler where a draft entry gets created (with IsActiveEntity is set to false) would be truly appreciated!&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;cds -v
@sap/cds: 6.3.2
@sap/cds-compiler: 3.4.2
@sap/cds-dk: 4.1.5
@sap/cds-foss: 4.0.0
@sap/cds-mtx: -- missing --
Node.js: v16.18.1
draft-concern: 1.0.0
home: /Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds&amp;lt;br&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Nov 2022 00:15:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaq-p/12663533</guid>
      <dc:creator>Steven-Mason</dc:creator>
      <dc:date>2022-11-23T00:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot create draft for Node.js CAP Service in custom handler</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663534#M4750979</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;the_mason_abides&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;Could you please check if this question and answer related to your issue?https://answers.sap.com/answers/13768341/view.html&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Olena&lt;/P&gt;</description>
      <pubDate>Fri, 02 Dec 2022 12:30:22 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663534#M4750979</guid>
      <dc:creator>OlenaT</dc:creator>
      <dc:date>2022-12-02T12:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot create draft for Node.js CAP Service in custom handler</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663535#M4750980</link>
      <description>&lt;P&gt;Hello Olena,&lt;/P&gt;&lt;P&gt;Thank you very much for your response. Unfortunately that question is similar, but does not answer my question. I have tried both the&lt;STRONG&gt; srv.post&lt;/STRONG&gt; statement (which I am including in my example repo above) and I have also tried the &lt;STRONG&gt;srv.send&lt;/STRONG&gt; statement as well. When I have used the &lt;STRONG&gt;srv.send &lt;/STRONG&gt;method, I actually receive an error message in inside the cds &lt;STRONG&gt;crud.js&lt;/STRONG&gt; source file. Here is the code when attempting to use the &lt;STRONG&gt;srv.send &lt;/STRONG&gt;method:&lt;/P&gt; 
&lt;PRE&gt;&lt;CODE&gt;const cds = require("@sap/cds")

module.exports = cds.service.impl( async (srv) =&amp;gt; {
  srv.on('createDraft', async (req) =&amp;gt; {
    const { Customers } = srv.entities
    //const { ID } =  await srv.post(Customers, {})
    const ID = await srv.send('POST', Customers, {})
    return { ID }
  })
})&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When I do this, I get the following error message:&lt;/P&gt; 
&lt;PRE&gt;&lt;CODE&gt;[INTERNAL ERROR] TypeError: Cannot read properties of undefined (reading 'INSERT')
    at ApplicationService.&amp;lt;anonymous&amp;gt; (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/libx/_runtime/common/generic/crud.js:46:32)
    at next (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:75:36)
    at ApplicationService.handle (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:79:6)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ApplicationService.&amp;lt;anonymous&amp;gt; (/Users/stevenmason/dev/cap/draft-concern/srv/customer-service.js:7:16)
    at async next (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:75:17)
    at async ApplicationService.handle (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:73:10)
    at async /Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/handlers/action.js:52:16&amp;lt;br&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here you can see where that error is occurring within crud.js:&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2120571-crudjs-debug-error.png" /&gt;&lt;/P&gt;&lt;P&gt;It is possible I am calling the &lt;STRONG&gt;srv.send&lt;/STRONG&gt; method incorrectly, but I am following the example set in the CAP documentation &lt;A rel="nofollow" href="https://cap.cloud.sap/docs/node.js/services#srvsend---method-path--query--event-data-headers---results-"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;As mentioned in my previous post, if I call this service directly with a POST method via a HTTP client, I have no issues and draft works correctly. All I am wanting to do is programmatically replicated the POST request in my code. Any suggestions on how to accomplish this is greatly be appreciated!&lt;/P&gt;</description>
      <pubDate>Sun, 04 Dec 2022 23:35:54 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663535#M4750980</guid>
      <dc:creator>Steven-Mason</dc:creator>
      <dc:date>2022-12-04T23:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot create draft for Node.js CAP Service in custom handler</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663536#M4750981</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;the_mason_abides&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;You are not using the .send API correctly. As described in the documentation you mentioned, you need to provide either data or query. &lt;/P&gt;&lt;P&gt;As I mention in the link I gave you, we don't recommend rewriting our draft implementation and don't provide any API to support it. You can use draft events to add your custom logic as described here &lt;A href="https://cap.cloud.sap/docs/node.js/app-services#draft" target="test_blank"&gt;https://cap.cloud.sap/docs/node.js/app-services#draft&lt;/A&gt;, but if you decide to reimplement our out-of-the-box draft handling - we cannot assist you.&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Olena&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 16:22:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663536#M4750981</guid>
      <dc:creator>OlenaT</dc:creator>
      <dc:date>2022-12-05T16:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot create draft for Node.js CAP Service in custom handler</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663537#M4750982</link>
      <description>&lt;P&gt;Hello Olena,&lt;/P&gt;&lt;P&gt;Thanks again for your response.  I am definitely not trying to re-implement the out-of-the-box draft handling.  As noted in my original post, when I call the draft handling via a POST call from an external HTTP client, it behaves exactly as expected.  All I am wanting to do is trigger the same POST behavior, but from within my custom code.&lt;/P&gt;&lt;P&gt;With regard to the &lt;STRONG&gt;srv.send &lt;/STRONG&gt;API, inn the example above, my data set is empty (empty brackets).  I did this as this is allowed for a POST from an external HTTP client (as demonstrated in my original post).  If I pre-fill data for the &lt;STRONG&gt;srv.send&lt;/STRONG&gt; call:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;const cds = require("@sap/cds")

module.exports = cds.service.impl(async (srv) =&amp;gt; {
  srv.on("createDraft", async (req) =&amp;gt; {
    const { Customers } = srv.entities

    //const { ID } =  await srv.post(Customers, {})

    const generatedID = cds.utils.uuid()
    const customerID = 1234
    const customerName = "Example Customer"
    const ID = await srv.send("POST", Customers, { ID: generatedID, CustomerID: customerID, CustomerName: customerName })
    return { ID }
  })
})&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I still receive the same error message from &lt;STRONG&gt;crud.js&lt;/STRONG&gt;:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;[INTERNAL ERROR] TypeError: Cannot read properties of undefined (reading 'INSERT')
    at ApplicationService.&amp;lt;anonymous&amp;gt; (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/libx/_runtime/common/generic/crud.js:46:32)
    at next (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:75:36)
    at ApplicationService.handle (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:79:6)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ApplicationService.&amp;lt;anonymous&amp;gt; (/Users/stevenmason/dev/cap/draft-concern/srv/customer-service.js:12:16)
    at async next (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:75:17)
    at async ApplicationService.handle (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:73:10)
    at async /Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/handlers/action.js:52:16&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Am I missing something with regard to providing data to the &lt;STRONG&gt;srv.send&lt;/STRONG&gt; call that is causing this issue?&lt;/P&gt;&lt;P&gt;Thanks again,&lt;/P&gt;&lt;P&gt;Steven&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2022 18:34:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663537#M4750982</guid>
      <dc:creator>Steven-Mason</dc:creator>
      <dc:date>2022-12-05T18:34:52Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot create draft for Node.js CAP Service in custom handler</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663538#M4750983</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;the_mason_abides&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;Could you please try the following CQN notation? It worked for me:   &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;await srv.send({query: INSERT.into('CatalogService.Books').entries({}), event: 'NEW'})&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Olena&lt;/P&gt;</description>
      <pubDate>Tue, 06 Dec 2022 14:59:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663538#M4750983</guid>
      <dc:creator>OlenaT</dc:creator>
      <dc:date>2022-12-06T14:59:26Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot create draft for Node.js CAP Service in custom handler</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663539#M4750984</link>
      <description>&lt;P&gt;I can confirm that this works. Check my comment &lt;A href="https://answers.sap.com/comments/13771791/view.html" target="_blank"&gt;here&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Dec 2022 22:23:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663539#M4750984</guid>
      <dc:creator>gregorw</dc:creator>
      <dc:date>2022-12-06T22:23:18Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot create draft for Node.js CAP Service in custom handler</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663540#M4750985</link>
      <description>&lt;P&gt;Hello Olena,&lt;/P&gt;&lt;P&gt;This solution did work and I really appreciate the help!&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Steven&lt;/P&gt;</description>
      <pubDate>Wed, 07 Dec 2022 20:10:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/12663540#M4750985</guid>
      <dc:creator>Steven-Mason</dc:creator>
      <dc:date>2022-12-07T20:10:40Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot create draft for Node.js CAP Service in custom handler</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/13680558#M4826902</link>
      <description>I have the same problem</description>
      <pubDate>Wed, 24 Apr 2024 08:52:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/13680558#M4826902</guid>
      <dc:creator>Youssef-kh1</dc:creator>
      <dc:date>2024-04-24T08:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot create draft for Node.js CAP Service in custom handler</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/13680560#M4826903</link>
      <description>I have the same problem,</description>
      <pubDate>Wed, 24 Apr 2024 08:53:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/13680560#M4826903</guid>
      <dc:creator>Youssef-kh1</dc:creator>
      <dc:date>2024-04-24T08:53:01Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot create draft for Node.js CAP Service in custom...</title>
      <link>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/13680622#M4826914</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I see that you have resolved the problem in your code.&lt;BR /&gt;I've reviewed your approach, and it seems to be aligned with what I've implemented in my project. However, I'm encountering an unexpected behavior when executing the DUPLICATE action in my TypeScript-based project.&lt;/P&gt;&lt;P&gt;Here's a breakdown of my code:&lt;BR /&gt;In OutletsController.ts, I have a method for handling outlet duplication:&lt;/P&gt;&lt;PRE&gt;public async duplicateOutlet(req: Request): Promise&amp;lt;any&amp;gt; {&lt;BR /&gt;try {&lt;BR /&gt;let selectedItemID: string;&lt;BR /&gt;const firstParam = req.params[0] as { ID: string };&lt;BR /&gt;console.log('firstParam:', firstParam.ID);&lt;BR /&gt;&lt;BR /&gt;selectedItemID = firstParam.ID;&lt;BR /&gt;&lt;BR /&gt;const { Outlets } = cds.entities(Constants.CDS_NAMESPACE);&lt;BR /&gt;const sQuery = SELECT.from(Outlets).where`(ID) = ${selectedItemID}`;&lt;BR /&gt;let selectedItem = await cds.run(sQuery);&lt;BR /&gt;console.log('selectedItem:', selectedItem);&lt;BR /&gt;&lt;BR /&gt;// Setting IsActiveEntity to true (unexpected behavior)&lt;BR /&gt;selectedItem[0].IsActiveEntity = true;&lt;BR /&gt;const newItemData = { ...selectedItem[0] };&lt;BR /&gt;delete newItemData.ID;&lt;BR /&gt;console.log('newItemData: ',newItemData);&lt;BR /&gt;&lt;BR /&gt;const insertedItem = await INSERT.into(Outlets).entries(newItemData);&lt;BR /&gt;console.log(insertedItem);&lt;BR /&gt;return insertedItem;&lt;BR /&gt;} catch (error) {&lt;BR /&gt;console.error('Error duplicating outlet:', error);&lt;BR /&gt;throw error;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;In service.cds, I've defined the Outlets entity with the DUPLICATE action:&lt;/PRE&gt;&lt;PRE&gt;service.cds&lt;BR /&gt;&lt;BR /&gt;@odata.draft.enabled&lt;BR /&gt;entity Outlets as select ID, code @mandatory, description from planification.Outlets actions {&lt;BR /&gt;action DUPLICATE() returns Outlets;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;PRE&gt;In service.ts, I'm binding the DUPLICATE action to the duplicateOutlet method in my controller:&lt;BR /&gt;this.on(&lt;BR /&gt;"DUPLICATE",&lt;BR /&gt;this.outletsController.duplicateOutlet.bind(this.outletsController)&lt;BR /&gt;);&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;Despite following similar patterns to your solution, I'm facing issues where the IsActiveEntity field is unexpectedly set to true after executing the DUPLICATE action.&lt;BR /&gt;Your guidance on resolving this unexpected behavior would be greatly appreciated.&lt;BR /&gt;&lt;BR /&gt;Youssef.&lt;/P&gt;</description>
      <pubDate>Wed, 24 Apr 2024 09:43:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cannot-create-draft-for-node-js-cap-service-in-custom-handler/qaa-p/13680622#M4826914</guid>
      <dc:creator>Youssef-kh1</dc:creator>
      <dc:date>2024-04-24T09:43:06Z</dc:date>
    </item>
  </channel>
</rss>

