cancel
Showing results for 
Search instead for 
Did you mean: 

CAP - JAVA action and function

ananth12
Product and Topic Expert
Product and Topic Expert
0 Kudos
392

HI Team,

I have defined the action like this in my service

aspect cuid {
  key ID : UUID; 
}

entity PDMDocument : cuid, managed {
    docId           : String(100);
    docName         : String(100);
    versionNumber   : Integer;
    isActiveVersion : Boolean;
}
service DocumentService {
    entity PDMDocument as projection on db.PDMDocument
        actions {
            action   deactivateDocument(docId : String) returns array of PDMDocument;
            function getDocumentByDocId(docId : String) returns array of PDMDocument;
        }
}

As per XMl Content , action and fucntions are bounded to my entity.

 

<Function Name="getDocumentByDocId" IsBound="true" IsComposable="false" EntitySetPath="in">
<Parameter Name="in" Type="DocumentService.PDMDocument"/>
<Parameter Name="docId" Type="Edm.String"/>
<ReturnType Type="DocumentService.PDMDocument"/>
</Function>
<Action Name="deactivateDocument" IsBound="true" EntitySetPath="in">
<Parameter Name="in" Type="DocumentService.PDMDocument"/>
<Parameter Name="docId" Type="Edm.String"/>
<ReturnType Type="DocumentService.PDMDocument"/>
</Action>

 

Below is the my service handler methods

 

@On(event = "getDocumentByDocId", entity = PDMDocument_.CDS_NAME)
public void getDocumentByDocId(PDMDocumentGetDocumentByDocIdContext context) {
String docId = context.getDocId();
logger.info("Fetching document with docId: {}", docId);

PDMDocument document = documentService.getDocumentById(docId);
if (document == null) {
logger.warn("No document found with docId: {}", context.get("docId"));
context.setResult(null);
} else {
context.setResult(document);
}
}

@On(event = "deactivateDocument", entity = PDMDocument_.CDS_NAME)
public void deactivateDocument(PDMDocumentDeactivateDocumentContext context) {
String docId = context.getDocId();
logger.info("Deactivating document with docId: {}", docId);

boolean updated = documentService.deactivateDocument(docId);
if (!updated) {
logger.warn("No document found with docId: {}", docId);
}

PDMDocument document = documentService.getDocumentById(docId);
if (document == null) {
logger.warn("No document found with docId: {}", context.get("docId"));
context.setResult(null);
} else {
context.setResult(document);
}
}

 

 

When I execute the request from postman getting the below error message for both GET and POST request

GET : https://procurement-reuse-policy-test-w3ex5y3d-dev-poc-ams-policy-srv.cfapps.eu10-004.hana.ondemand.com/odata/v4/DocumentService/PDMDocument('DOC-457')/DocumentService.getDocumentByDocId 

POST:  https://procurement-reuse-policy-test-w3ex5y3d-dev-poc-ams-policy-srv.cfapps.eu10-004.hana.ondemand.com/odata/v4/DocumentService/PDMDocument('DOC-457')/DocumentService.deactivateDocument

payload for post request:
{
"docId": "DOC-457"
}

Error for the above requests in postman:

{
"error": {
"code": "400",
"message": "The key value '' is invalid."
}
}

Please let me know where I went wrong in the above approach

View Entire Topic
ClearQueries
Explorer
0 Kudos

Hi @ananth12,

While invoking instance function/action, you've to follow below syntax - 

Function - 
GET http://<serverpath>/odata/v4/service-name/SampleEntity(EntityKey='EntityKeyValue')/EntityBoundFunction(FuncitonKey='FunctionKeyValue')

Action - 
POST http://<serverpath>/odata/v4/service-name/SampleEntity(EntityKey='EntityKeyValue')/EntityBoundAction
Content-Type: application/json
{
   "ActionParameter" : "ActionParameterValue"
}

As per what you've shared in your query, it's not quite clear - what is/are the key parameters for your entity PDMDocument. Additionally, you're trying to invoke your function without passing function key - docId you speicified in service definition. 

Kindly adjust the URI and try out again.

If it does not work, share the data-model design for entity PDMDocument.

Hope this resolves your query.

Please accept the solution if it does.

Read following blogs to understand how to invoke static function and action in CAP - 

https://www.clearqueries.com/Blogs/how-to-use-functions-in-cap-node-js.html

https://www.clearqueries.com/Blogs/how-to-use-call-database-procedure-from-cap-action.html

 

Regards,

ClearQueries.com 

Follow on LinkedIn

ananth12
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi ,
ananth12
Product and Topic Expert
Product and Topic Expert
0 Kudos
Hi,
ananth12
Product and Topic Expert
Product and Topic Expert
0 Kudos
I have added the required details , please let me know where it went wrong
ClearQueries
Explorer
0 Kudos

Hi @ananth12,

thanks for sharing the details.

Entity defined in your data model is guid based.

It seems you're trying to pass docId while invoking the endpoint and not the actual guid key. You've to pass GUID key while referring to Entity PDMDocument (as key for your entity is ID, i.e., GUID ) and docId while invoking the action (as you've defined it as an additional importing parameter in service definition. I feel you even don't need docId as importing parameter as your action/function is an instance/bound one).

Your HTTP calls in Postman should look something like below - 

GET : https://procurement-reuse-policy-test-w3ex5y3d-dev-poc-ams-policy-srv.cfapps.eu10-004.hana.ondemand.com/odata/v4/DocumentService/PDMDocument(4149f5eb-f262-3000-f000-0002b7fb3600)/getDocumentByDocId(docId='DOC-457') 

POST:  https://procurement-reuse-policy-test-w3ex5y3d-dev-poc-ams-policy-srv.cfapps.eu10-004.hana.ondemand.com/odata/v4/DocumentService/PDMDocument(4149f5eb-f262-3000-f000-0002b7fb3600)/deactivateDocument

payload for post request:
{
"docId": "DOC-457"
}

Note - I've passed some random guid for now. You're required to pass GUID of the actual record on which you want to execute this action/function.

Hope this resolves your query.

Please accept the solution if it does.

Regards,

ClearQueries.com 

Follow on LinkedIn

 

ananth12
Product and Topic Expert
Product and Topic Expert
0 Kudos

Thank you, I have got to know from the below doc with the below request I am able to do my required task.

https://cap.cloud.sap/docs/guides/providing-services#calling-actions-functions

https://procurement-reuse-policy-test-w3ex5y3d-dev-poc-ams-policy-srv.cfapps.eu10-004.hana.ondemand.... 

Do you know , how to read the primary key value which is being passed in request from the context object in code?