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
BLSAppls.js
BLSApplibmodules.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 = "https://" + 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: " + result.Rowset.Row[0].Material); console.log("Batch: " + result.Rowset.Row[0].Batch); console.log("Found: " + result.Rowset.Row[0].Output); console.log("QtyWeight: " + result.Rowset.Row[0].QtyWeight); console.log("Vendor: " + result.Rowset.Row[0].Vendor); console.log("VendorBatch: " + 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: odejsBLSApp> 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 !
New NetWeaver Information at SAP.com
Very Helpfull