Consuming On-Premise OData using XSJS in Multiple Cloud Connector Scenario
What is Multiple Cloud Connector Scenario ?
With new version release of SAP Cloud Connector , it is possible to connect different Backend system from Different Locations to one SAP Cloud Platform via multiple cloud connectors. Below Image helps to understand more.
I faced issue while connecting to the on-Premise systems due to the Location ID. Also couldn’t find access related issue in Cloud Connector.
In this blog i will technically help how to define the destination file and setting the header parameter for Location in case of Multiple Cloud Connector scenario
Scenario : I want to consume an On-Premise OData service in SCP using XSJS based on a business requirement.
Note : We can use OData Provisioning in SCP for Connectivity to On-Premise OData Service.
Lets define Destination file : gateway.xshttpdest
host = "ecc.virtual.address"; // Virtual Host defined on Cloud Connector
port = 5000; // Virtual Port defined on Cloud connector
pathPrefix = "/sap/opu/odata/sap/EHS_INC_REPORTINCIDENT_SRV/"; // OData Service
useProxy = true; // Its should be true , as we re using On-Premise system
proxyHost = "localhost"; //connects, via a Cloud Connector tunnel,
//to on-premise services and resources
proxyPort = 20003; // Proxy Port
authType = basic; // Authentication Type is Basic Authentication
useSSL = false;
timeout = 30000;
Maintain the Authentication details in XS ADMIN Tool.
The Basic Authentication details , it should be the User/Password of the On-Prem ECC System .
After any changes in .xshttpdest file , need to update the User details in XS Admin Tool.Because its gets refresh after any changes .
Create a XSJS file to consume the service : GetLocation.xsjs
var destination_package = "Test_XSJS.OdataFromXSJS"; //
var destination_name = "gateway"; //e.g. demo
var dest = $.net.http.readDestination(destination_package, destination_name);
var client = new $.net.http.Client();
var response = client.getResponse();
try {
var req = new $.web.WebRequest($.net.http.GET, "Locations?$format=json");//Entity Set
req.headers.set("SAP-Connectivity-SCC-Location_ID", "Bangalore");
// This is mandatory .Else the Call from XSJS fails to find the exact
//cloud Cloud Connector Instance which is mapped to the Backend ECC System
req.headers.set("X-CSRF-Token", "CSRF");
client.request(req, dest);
$.response.setBody(response.body.asString());
$.response.contentType = "application/json";
$.response.status = $.net.http.OK;
}
catch (e)
{
$.response.contentType = "text/plain";
$.response.setBody(e.message);
}
Activate the project and Run the XSJS . It should get the results .
Finally !! We have consumed an On-Prem OData Service in a Multiple Cloud Connector Scenario.
Hi, Aisurya Puhan.
Hi
I am getting the error "500 Internal server error" while trying to run the XSJS application.
Are you familiar with this error?
Regards,
Florence
Hello Florence,
Sorry for the late reply. Could you please post the code snippet if possible .
Best
Aisurya
Hi Aisurya,
Here is the code:
$.trace.error("Can you see me?");
var custnum = $.request.body.asString();
var essai = custnum.toString().replace("\r", "").replace("\n", "").replace("\t", "");
essai = JSON.parse(essai);
var Prioritytickt = essai.Prioritytickt;
try {
$.response.contentType = "text/html";
var dest = $.net.http.readDestination("itsm.services", "Destination");
var client = new $.net.http.Client();
var req = new $.web.WebRequest($.net.http.GET, "MessageResultSet?$format=json"); //Entity Set
req.headers.set("SAP-Connectivity-SCC-Location_ID", "PSB");
client.request(req, dest);
//var response = client.getResponse();
var response = client.getResponse().body.asString();
//$.response.contentType = "application/json";
var result = response.body.asString();
var jsonResult = JSON.parse(result);
var chatbotResponse;
if (jsonResult.d.results.length > 0) {
if (Prioritytickt === "2") {
chatbotResponse = "Here are the high priority tickets ";
}
if (Prioritytickt === "3") {
chatbotResponse = "Here are the medium priority tickets ";
}
if (Prioritytickt === "4") {
chatbotResponse = "Here are the low priority tickets ";
}
if (Prioritytickt === "All") {
chatbotResponse = "List of all the tickets ";
}
if (Prioritytickt !== "All") {
var ticketdetails = jsonResult.d.results.filter(function(hero) {
return hero.Priority === Prioritytickt;
});
for (var i = 0; i < ticketdetails.length; i++) {
chatbotResponse += ticketdetails[i].ObjectId;
if (i < ticketdetails.length - 1) {
chatbotResponse += ", ";
}
}
} else {
for (var i = 0; i < jsonResult.d.results.length; i++) {
chatbotResponse += jsonResult.d.results[i].ObjectId;
if (i < jsonResult.d.results.length - 1) {
chatbotResponse += ", ";
}
}
}
var output = {
replies: [{
type: 'text',
content: chatbotResponse
}],
conversation: {
memory: chatbotResponse
}
};
}
var body = JSON.stringify(output);
$.response.contentType = 'application/json';
$.response.setBody(body);
$.response.status = $.net.http.OK;
} catch (e) {
$.response.contentType = "text/plain";
$.response.setBody(e.message);
}
I am getting error from line: var custnum = $.request.body.asString();
Any ideas?
Regards,
Florence
Hi Florence,
Your error is in XSJS program. Hope the below link might help
https://answers.sap.com/answers/12335031/view.html
Cheers,
Aisurya