const { BusinessPartnerAddress } = require("@sap/cloud-sdk-vdm-business-partner-service");
const _prepareBody = (address) => {
return {
businessPartner: address.BusinessPartner,
country: address.Country,
postalCode: address.PostalCode,
cityName: address.CityName,
streetName: address.StreetName,
houseNumber: address.HouseNumber
}
}
const buildAddressForCreate = (req) => {
const address = BusinessPartnerAddress.builder().fromJson(_prepareBody(req.data));
if (req.params[0]) {
const { BusinessPartner } = req.params[0];
address.businessPartner = BusinessPartner;
}
return address;
}
const buildAddressForUpdate = (req) => {
const { BusinessPartner, AddressID } = req.params[0];
const address = BusinessPartnerAddress.builder().fromJson(_prepareBody(req.data));
address.businessPartner = BusinessPartner;
address.addressId = AddressID;
return address;
}
const prepareResult = (address) => {
return {
BusinessPartner: address.businessPartner,
AddressID: address.addressId,
Country: address.country,
PostalCode: address.postalCode,
CityName: address.cityName,
StreetName: address.streetName,
HouseNumber: address.houseNumber
}
}
module.exports = {
buildAddressForCreate,
buildAddressForUpdate,
prepareResult
}
const { BusinessPartnerAddress } = require("@sap/cloud-sdk-vdm-business-partner-service");
const sdkDest = { "destinationName": 's4hc_simple' };
const {
buildAddressForCreate,
buildAddressForUpdate,
prepareResult,
prepareBody
} = require('./helpers')
//this event handler is triggered when we call
//POST http://localhost:4004/address-manager/BusinessPartnerAddresses
this.on('CREATE', BusinessPartnerAddresses, async (req) => {
try {
const address = buildAddressForCreate(req);
const result = await BusinessPartnerAddress
.requestBuilder()
.create(address)
.execute(sdkDest);
return prepareResult(result);
} catch (err) {
req.reject(err);
}
});
//this event handler is triggered when we call
//PUT http://localhost:4004/address-manager/BusinessPartnerAddresses(BusinessPartner='',AddressID='')
this.on('UPDATE', BusinessPartnerAddresses, async (req) => {
try {
const address = buildAddressForUpdate(req);
const result = await BusinessPartnerAddress
.requestBuilder()
.update(address)
.execute(sdkDest);
return prepareResult(result);
} catch (err) {
req.reject(err);
}
});
//this event handler is triggered when we call
//DELETE http://localhost:4004/address-manager/BusinessPartnerAddresses(BusinessPartner='',AddressID='')
this.on('DELETE', BusinessPartnerAddresses, async (req) => {
try {
const { BusinessPartner, AddressID } = req.params[0];
await BusinessPartnerAddress
.requestBuilder()
.delete(BusinessPartner, AddressID)
.execute(sdkDest);
} catch (err) {
req.reject(err);
}
});
{
"BusinessPartner": "10300001",
"CityName": "string",
"Country": "DE",
"HouseNumber": "string",
"PostalCode": "12345",
"StreetName": "string"
}
{
"CityName": "Timisoara",
"Country": "DE",
"HouseNumber": "string",
"PostalCode": "12345",
"StreetName": "string"
}
"ui": "cd app/address-manager-ui && npm start"
annotate AddressManagerService.BusinessPartners with @title : 'Business Partners' {
BusinessPartner @title : 'Business Partner';
LastName @title : 'Last Name';
FirstName @title : 'First Name';
};
annotate AddressManagerService.BusinessPartners with @(UI : {
HeaderInfo : {
TypeName : 'Business Partner',
TypeNamePlural : 'Business Partners'
},
LineItem : [
{
Value : BusinessPartner,
Label : 'Business Partner'
},
{
Value : LastName,
Label : 'Last Name'
},
{
Value : FirstName,
Label : 'First Name'
}
],
});
const constructBusinessPartnerFilter = (req) => {
if (req && req.params && req.params[0]) {
return {
'BusinessPartner': req.params[0].BusinessPartner
}
} else if (req && req._.odataReq._queryOptions && req._.odataReq._queryOptions.$search) {
const searchValue = JSON.parse(req._.odataReq._queryOptions.$search);
return `BusinessPartner = ${searchValue} or FirstName = ${searchValue} or LastName = ${searchValue}`
}
}
const constructBusinessPartnerAddressFilter = (req) => {
if (req && req.params && req.params[0]) {
return req.params[0].AddressID ? {
'BusinessPartner': req.params[0].BusinessPartner,
'AddressID': req.params[0].AddressID
} : {
'BusinessPartner': req.params[0].BusinessPartner
}
}
}
const buildQuery = (entity, columns, filter) => {
if (filter) {
return SELECT.from(entity)
.columns(columns)
.where(filter)
} else {
return SELECT.from(entity)
.columns(columns)
}
}
module.exports = {
buildAddressForCreate,
buildAddressForUpdate,
prepareResult,
constructBusinessPartnerAddressFilter,
constructBusinessPartnerFilter,
buildQuery
}
module.exports.ENTITIES = {
BusinessPartner: {
name: 'A_BusinessPartner',
columns: ['BusinessPartner', 'FirstName', 'LastName']
},
BusinessPartnerAddress: {
name: 'A_BusinessPartnerAddress',
columns: ['BusinessPartner', 'AddressID', 'Country', 'PostalCode', 'CityName', 'StreetName', 'HouseNumber']
}
}
const {
buildAddressForCreate,
buildAddressForUpdate,
prepareResult,
constructBusinessPartnerAddressFilter,
constructBusinessPartnerFilter,
buildQuery
} = require('./helpers')
const { ENTITIES } = require('./constants');
const service = await cds.connect.to('API_BUSINESS_PARTNER');
const _buildHandler = async (entityName, req, filterFunction) => {
try {
const { name: entity, columns } = ENTITIES[entityName];
const filter = filterFunction(req);
const query = buildQuery(entity, columns, filter);
return await service.transaction().run(query);
} catch (err) {
req.reject(err);
}
}
//this event handler is triggered when we call
//GET http://localhost:4004/address-manager/BusinessPartners
//GET http://localhost:4004/address-manager/BusinessPartners('1000000')
this.on('READ', BusinessPartners, async (req) => {
return await _buildHandler('BusinessPartner', req, constructBusinessPartnerFilter);
});
HeaderInfo : {
TypeName : 'Business Partner',
TypeNamePlural : 'Business Partners',
Title : {Value : BusinessPartner},
Description : {Value : FirstName}
}
Facets : [{
$Type : 'UI.ReferenceFacet',
Target : 'to_BusinessPartnerAddress/@UI.LineItem',
Label : 'Business Partner Addresses'
}]
annotate AddressManagerService.BusinessPartnerAddresses with @(UI : {
HeaderInfo : {
TypeName : 'Business Partner Address',
TypeNamePlural : 'Business Partner Addresses',
Title : {Value : AddressID}
},
LineItem : [
{
Value : AddressID,
Label : 'Address ID'
},
{
Value : Country,
Label : 'Country'
},
{
Value : PostalCode,
Label : 'Postal Code'
},
{
Value : CityName,
Label : 'City Name'
},
{
Value : StreetName,
Label : 'Street Name'
},
{
Value : HouseNumber,
Label : 'House Number'
}
]
});
//this event handler is triggered when we call
//GET http://localhost:4004/address-manager/BusinessPartnerAddresses
//GET http://localhost:4004/address-manager/BusinessPartnerAddresses(BusinessPartner='10300001',AddressID='24642')
//GET http://localhost:4004/address-manager/BusinessPartners('1000000')/to_BusinessPartnerAddress
this.on('READ', BusinessPartnerAddresses, async (req) => {
return await _buildHandler('BusinessPartnerAddress', req, constructBusinessPartnerAddressFilter);
});
annotate AddressManagerService.BusinessPartnerAddresses with @(
Capabilities : {InsertRestrictions : {Insertable : false}},
UI : {…}
);
Facets : [{
$Type : 'UI.ReferenceFacet',
Target : '@UI.FieldGroup#Address',
Label : 'Address'
}],
FieldGroup #Address : {Data : [
{
Value : Country,
Label : 'Country'
},
{
Value : PostalCode,
Label : 'Postal Code'
},
{
Value : CityName,
Label : 'City Name'
},
{
Value : StreetName,
Label : 'Street Name'
},
{
Value : HouseNumber,
Label : 'House Number'
}
]}
SAP Fiori Element using CAP Model – Introduction
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
11 | |
10 | |
9 | |
8 | |
7 | |
6 | |
6 | |
6 | |
5 |