Skip to Content
0
Jan 04, 2023 at 09:54 AM

Iimplement unbound action/ funcion in service.cds

438 Views

Hello experts

I need some help (as per the title) on the actions and/or functions of my CAP project: I have currently created an unbound action which, given a parameter, returns the key of all the entities with that parameter.

The part that troubles me comes now: in the controller of my SAPUI5 freestyle application I can't in any way call this function (or in any case it doesn't return any value, even if the parameter is correct).

Could anyone help me? Thanks a lot for your time

base-service.cds

service BaseService
{
    entity DocumentHeader as
        projection on extHeader.Z_SFA_SALESORDER
        {
            SalesOrder as DocumentId,
            OverallSDProcessStatus as Status,
            TotalNetAmount as TotalValue,
            TransactionCurrency as Currency,
            SalesOrderDate as DocumentDate,
            PurchaseOrderByCustomer as CustomerOrder,
            RequestedDeliveryDate as RequestDeliveryDate,
            ServicesRenderedDate as EffectiveDeliveryDate,
            OverallDeliveryStatus as DeliveryStatus,
            OverallDeliveryBlockStatus as DeliveryBlock,
            HeaderBillingBlockReason as InvoiceBlock,
            CustomerPaymentTerms as PayConditions,
            SoldToParty as CustomerId,
            //to_SoldToParty,
            to_PricingElement as to_PriceConditions,
            to_Partner as to_Partners,
        }

    entity DocumentItem as
        projection on extItem.Z_SFA_SALESORDERITEM
        {
            SalesOrder as DocumentId,
            SalesOrderItem as DocumentItemId,
            Product as ProductId,
            Product_Text as ProductDesc,
            OrderQuantity as BaseQuantity,
            OrderQuantityUnit as BaseUoM,
            RequestedQuantity as SalesQuantity,
            RequestedQuantityUnit as SalesUoM,
            Batch as BatchStatus,
            TransactionCurrency as Currency,
            DeliveryStatus as DeliveryStatus,
            Subtotal1Amount as Discounts,
            IncotermsClassification as Incoterms,
            SDProcessStatus as ItemStatus,
            NetPriceAmount as Price,
            to_ScheduleLine as to_DeliveryPlans,
            to_IncotermsClassification as to_Incoterms,
            to_PricingElement as to_PriceConditions,
            NetAmount as TotalValue,
        }

    action DocumentIdByProduct(ProductId: String) returns array of String;   //if needed it can be changed to function
}

base-service.js

const cds = require("@sap/cds");
let requires = [cds.requires['Z_SFA_SALESORDER_CDS'], cds.requires['Z_SFA_SALESORDERITEM_CDS'], cds.requires['****']];

module.exports = async srv =>{
  const connItem = await cds.connect.to('Z_SFA_SALESORDERITEM_CDS');
  const conn = await cds.connect.to('Z_SFA_SALESORDER_CDS');
  const connExtra = await cds.connect.to('****');
  const connDB = await cds.connect.to('db');

  srv.on('READ', 'DocumentHeader', async req => {
    let ret = await conn.run(req.query);
    return ret;       
  });
  srv.on('READ', 'DocumentItem', async req => {   
    return connItem.run(req.query);       
  });
  
  //http://localhost:4004/BaseService/DocumentIdByProduct(ProductId='TG13')
  srv.on('DocumentIdByProduct', async(req) => {
    
    let headers = (await srv.run(SELECT('DocumentId').from('DocumentItem').where({ProductId : req.data.ProductId})));
    console.error('>>> ' + JSON.stringify(req.query));

    return headers;
  
  });
  
};<br>

controller.js

testFunction: function () {

        //http://localhost:4004/BaseService/DocumentIdByProduct(ProductId='TG13') exists
				var sRet, oActionContext;
				
				//get the model that contains the action we need to use
				let oModel = this.getView().getModel(), 
				//get the bind context of the action delayed
				oActionODataContextBinding = oModel.bindContext("/DocumentIdByProduct(...)");
	  
				//set parameter DocumentId of the action
				oActionODataContextBinding.setParameter("ProductId", "TG13");
	  
				//execute the action and get the returned value (in this case is true or false if the action succeded or not)
				oActionODataContextBinding.execute().then(
					function () {
						oActionContext = oActionODataContextBinding.getBoundContext();
						sRet = oActionContext.getObject().value;

						if(sRet)
							MessageToast.show("Test riuscito!");
						else
							MessageToast.show("Test non riuscito!");

						this.getView().updateBindings();
					}.bind(this)
				);
			},<br>