Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
MioYasuatke
Active Contributor

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.pngprefill-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.

 

3 Comments
Dinu
Active Participant

That's how you create a draft!!

Thanks.

 

akuller_q
Participant

Another option is to use @Common.DefaultValuesFunction to define a function that returns the parameters for the new object.

MioYasuatke
Active Contributor
0 Kudos

@akuller_q 

Thank you for your suggestion! I looked into it and discovered the following blog post on the topic.

https://community.sap.com/t5/technology-blogs-by-sap/cap-with-fiori-elements-side-effects-custom-act...

Labels in this area