3

Prefilling Fields When Creating a New Entity with ... - SAP Community

 7 months ago
source link: https://community.sap.com/t5/technology-blogs-by-members/prefilling-fields-when-creating-a-new-entity-with-cap-node-js/ba-p/13593259
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Introduction

Fiori elements list report supports the prefilling of fields with default values when creating a new entity. Users will see a popup asking for input parameters. Based on these inputs, an event handler determines the default values to be pre-filled.

prefill-2-1.png
prefill-2-2.png

This functionality is specified through the use of annotations for prefilling fields, as illustrated below:

<Annotation Term="Common.DraftRoot">
    <Record Type="Common.DraftRootType">
        <PropertyValue Property="NewAction" String="com.sap.gateway.srvd.c_salesordermanage_sd.v0001.CreateWithSalesOrderType"/>
        ....
        ....
    </Record>
</Annotation>

Source: Prefilling Fields When Creating a New Entity | UI5 Demo Kit

My aim is to achieve this functionality with CAP Node.js. While CAP Java documentation provides guidance on implementing this feature, there is currently no equivalent documentation for CAP Node.js.

Implementation

Here goes the implementation. As you can see, it is fairly simple.

Service Definition

Service definition should follow the instructions provided in the CAP Java documentation.

  1. Define an action bound to the draft-enabled entity with an explicitly binding parameter typed with many $self.
  2. Annotate the draft-enabled entity with @Common.DraftRoot.NewAction: '<action name>'.
using { my.booklist as db } from '../db/schema';

service BooklistService {
    @odata.draft.enabled
    @Common.DraftRoot.NewAction: 'BooklistService.createDraft'
    entity Books as projection on db.Books actions {
    action createDraft(in: many $self, title: String) returns Books;
  };
}

Action Implementation

The action must perform the following tasks:

  1. Set initial data.
  2. Create a draft with initial data.
  3. Return the draft.
import cds from '@sap/cds'

module.exports = class BooklistService extends cds.ApplicationService {
    init() {
        const { Books } = this.entities;

        this.on('createDraft', Books, async (req) => {  
            //set initial data
            const data =  {
                ID: cds.utils.uuid(),
                title: req.data.title,
                publisher: 'Test'
            };
            
            //create a draft
            const book = await this.send({
                query: INSERT.into(Books).entries(data),
                event: "NEW",
            });

            //return the draft
            return book;
        })
        return super.init();
    }
}

According to the Q&A below, CAP Node.js does not provide draft API, so srv.send() has to be used instead.

https://community.sap.com/t5/technology-q-a/how-can-i-return-a-draft-entity-using-cap-node-js/qaq-p/...

Conclusion

In this blog, we explored how to prefill fields with default values when creating a new entity using CAP Node.js. By defining an action bound to the draft-enabled entity and implementing this action to set initial data and create a draft, developers can effectively prefill fields in their applications.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK