cancel
Showing results for 
Search instead for 
Did you mean: 

S/4 HANA Cloud SDK JavaScript Code

mahesh_z
Participant
0 Kudos

Hi Dennis,

Thought of starting a new thread here for this discussion on JavaScript Code.

My Complete Code is as follows -

const express = require("express");

const BusinessPartners = require("@sap/cloud-sdk-vdm-business-partner-service");

exports.businessPartners = function(req, res) {

getAllBusinessPartners() .

then(businessPartners => { res.status(200).send(businessPartners); })

.catch(error => { res.status(500).send(error.message);

});;

}

function getAllBusinessPartners() {

return BusinessPartner.requestBuilder()

.getAll()

.withCustomHeaders({ 'APIKey': 'mcJX5Yp6iMh3jXlGbDt17vNzyWgARQ1E' }) .select(BusinessPartner.FIRST_NAME, BusinessPartner.LAST_NAME) .filter(BusinessPartner.FIRST_NAME.equals("John"))

.top(5)

.skip(0)

.execute({ destinationName: 'SAPS4HANASandBox' });

}

Please let me know if there any corrections to be made here ?

Thanks,

Mahesh Z.

0 Kudos

Hi Mahesh,

I just wanted to know more about your S4/HANA cloud environment. Where is it hosted? I'm tying to do something similar but not able to access S4/HANA cloud using cloud sdk.

Hope to hear from you soon.

Regards,

Aniruddh Mathur

Accepted Solutions (1)

Accepted Solutions (1)

dhem
Active Participant
0 Kudos

Hi Mahesh,

skip(0) is redundant, I would simply omit that.
When you do the following:

const BusinessPartners = require("@sap/cloud-sdk-vdm-business-partner-service");

then BusinessPartners will be an object that holds everything that is exported by the business partner service package.

What you probably want is the following:

const BusinessPartners = require("@sap/cloud-sdk-vdm-business-partner-service").BusinessPartner;

BusinessPartner.requestBuilder()...

Alternatively, you could also do:

const BusinessPartners = require("@sap/cloud-sdk-vdm-business-partner-service");

BusinessPartners.BusinessPartner.requestBuilder()...

While functionally equivalent, I recommend going with the first approach for readibility.

By the way, if you use a modern editor like VSCode, you should get full autocompletion showing you which properties and/or function are available from which object.

Best regards

Dennis

mahesh_z
Participant
0 Kudos

Thanks Dennis , It is working for me now.

Now I am trying to implement the connection with our onPremise S/4 HANA system. I am getting one error in that for one of the header parameter which is missing.

Error in Logs -> Unable to create "SAP-Connectivity-Authentication" header: no JWT found on the current request.

I have already mentioned required connectivity service for this in my manifest.yml along with onPremise Destination. Not sure where i need to set this parameter and what value i need to set for this.

Thanks ,

Mahesh Z.

dhem
Active Participant
0 Kudos

The message you mentioned here is not an error per sé, but a warning. The header is not necessary for all scenarios.

In order to get JWT, you will need to setup an approuter. JWTs are an essential mechanism for authorization on Cloud Foundry. For the setup I recommend this blog post. While it describes how to secure a Java app, the approuter setup and the concepts are the same for JS.

If you have an approuter setup, you should be able to get the JWT from the Authorization header of incoming requests by doing something like this (assuming you're using express.js or something based on express.js):

const jwt: string = /^Bearer (.*)$/.exec(req.headers.authorization)[1];

Finally, you can use it when doing a VDM call like this:

BusinessPartner.requestBuilder()
    .getAll()
    ...
    .execute({ destinationName: 'YourDestination', jwt: 'yourjwt' });


Now theoretically you should not need this to integrate with an on-premise system as long as your using a technical user (?).
However, what you will need is a Cloud Connector. Have you set one up?

Answers (0)