BLS Transaction soap request in Nodejs
I am new to nodejs and recently thought how difficult would it be to call a SAP Mii BLS transaction from nodejs.
The BLS transaction will retrieve some details of a Material Document by passing in the document number and year .
The output will be data related to the Material document.
Steps taken:
After installing nodejs , install the soap package using
npm install soap
if you get an error try
npm install -g soap
My Project folder structure and files will be
BLSApp\bls.js
BLSApp\lib\modules.js
First create a modules.js file in a folder call lib to hold the functions ,
module.exports = {
// function to move a file to a new path
fnMoveFile: function(sFilePathFrom,sFilePathTo){
var fs = require('fs');
fs.rename(sFilePathFrom,sFilePathTo,(err) => {
if (err) throw err;
console.log('move complete');
});},
// function to call a BLS transaction via soap
fnSoapRequest: function(sServer,sPort,sTransaction,sUser,sPassword,Params,callback){
var soap = require('soap');
var auth = "Basic " + new Buffer(sUser + ':' + sPassword).toString("base64");
var url = "http://" + sServer + ":" + sPort + "/XMII/WSDLGen/" + sTransaction;
var args = {LoginName: sUser,LoginPassword: sPassword,InputParams: Params};
soap.createClient(url, { wsdl_headers: {Authorization: auth,Connection: "Keep-Alive"} },function(err, client) {
client.Xacute(args, function(err, result) {
callback (null,result);
});
});}
}
In the above included is an additional function simply just to show newbies ( like me) how to add additional functions 🙂
In the function fnSoapRequest you can see that soap library is present by using the require function .
First the wsdl gets executed , and following the return the BLS transaction is execute using client.Xacute , the args variable contains all of the Parameters required to execute the soap request.
To call my new library function fnSoapRequest i created a new file call bls.js this contains the following
// includes
const fs = require('fs');
const path = require('path');
// function import from modules.js
var fnMethods = require('./lib/modules.js');
// standard variables
var sServer = "SERVER";
var sTransaction = "MIIPROJECT/PATHTO/Transaction/WSGetMaterialDocument";
var sUser = "USER";
var sPassword = "PASSWORD";
var sPort = "50000";
var Params = { MaterialDocument: 5000016026, MaterialDocumentYear: 2017 };
//call to exported function fnSoapRequest
fnMethods.fnSoapRequest(sServer, sPort, sTransaction, sUser, sPassword, Params, handleReturn);
// handle the callback
function handleReturn(err, result) {
if (err) {
console.log(err.stack || err.message);
return;
}
//log results to console
console.log("Material:\t" + result.Rowset.Row[0].Material);
console.log("Batch:\t" + result.Rowset.Row[0].Batch);
console.log("Found:\t" + result.Rowset.Row[0].Output);
console.log("QtyWeight:\t" + result.Rowset.Row[0].QtyWeight);
console.log("Vendor:\t" + result.Rowset.Row[0].Vendor);
console.log("VendorBatch:\t" + result.Rowset.Row[0].VendorBatch);
}
here you can see that the last parameter i am passing is the callback function to handle the response .
To execute i just call nodejs and pass bls.js
PS C:\nodejs\BLSApp> node bls.js
Material: 000000000180001230
Batch: 1000006299
Output: Found
QtyWeight: 2100.000
Vendor: 1800001218
VendorBatch: TEST PO08 VEND
Very little code and very re-usable for BLS Transaction calls !
Hope you found this interesting !
Hey Garreth,
This is a nice informative blog, to invoke soap API's from Node in SAP Space..
I am trying a similar approach to harness Soap API's on SAP Process Orchestration..
But for some reason I am getting directed to port 443 and get a connection refused error.. I explicitly specified port as 50000 in the URL.. did you face any similar issue or have idea of how to get round it.. I would greatly appreciate it...
Thank you,
Ajay
Hi Ajay,
I didn’t get any errors and wasn’t direct to port 443 , if you wish to share your code i try and mimic it on our system here to test for you , also did you try port 50001 for https ?
Kind regards,
Garreth.