Tuesday, April 17, 2018

How to call BAPI in SAP from nodejs app

Recently I used nodejs RFC Connector provided by SAP. The module called node-rfc provides bindings for SAP NetWeawer RFC Library. Via the library it is possible to call RFC enabled function modules residing in SAP NetWeaver based ABAP system from nodejs, leveraging SAP Remote Function Call (RFC) protocol. For installation of the module into your nodejs framework see here.

Below I provide two example nodejs apps showcasing possibilities to passing values to BAPI’s parameters like single values, structures and tables.

I think that the node-rfc module is great option how to interface with SAP systems from heavily used nodejs app like in case of web based apps.

You can find both examples on my github:

demo_BAPI_params1.js      usage of table and variable parameter

//demo prg to showcase usage of table and variable parameters of SAP's BAPI while called from nodejs app via SAP/node-rfc nodule
"use strict";
var rfc = require('node-rfc');
var abapSystem = {
    user: 'sap_user',
    passwd: 'sap_user_pwd',
    ashost: 'sap.nodomain',
    sysnr: '01',
    client: '800'
};
var client = new rfc.Client(abapSystem);
var MAX_ROWS = 3;
var SELECTION_RANGE_str = {
               PARAMETER: "USERNAME",
               SIGN:      "I",
               OPTION:    "CP",
               LOW:       "A*"
        };     
var SELECTION_RANGE_tab = [SELECTION_RANGE_str];
       
client.connect(function(err) {
    if (err) {
        return console.error('could not connect to server', err);
    }  
    client.invoke('BAPI_USER_GETLIST', {
                       MAX_ROWS: MAX_ROWS,
                       SELECTION_RANGE: SELECTION_RANGE_tab
        },
        function(err, res) {
            if (err) {
                return console.error('Error invoking BAPI_USER_GETLIST:', err);
            }
               console.log('Result BAPI_USER_GETLIST:', res);
        });   
});

demo_BAPI_params2.js      usage of structure parameter of SAP's BAPI

//demo prg to showcase usage of structure parameter of SAP's BAPI while called from nodejs app via SAP/node-rfc nodule
"use strict";
var rfc = require('node-rfc');
var abapSystem = {
    user: 'sap_user',
    passwd: 'sap_user_pwd',
    ashost: 'sap.nodomain',
    sysnr: '01',
    client: '800'
};
var client = new rfc.Client(abapSystem);
 
client.connect(function(err) {
  if (err) {
    return console.error('could not connect to server', err);
  }
  var IMPORTSTRUCT = {
    RFCDATA1: 'some value of structure field RFCDATA1',
    RFCDATA2: 'some value of structure field RFCDATA2'
  };
  client.invoke('STFC_STRUCTURE',
    { IMPORTSTRUCT: IMPORTSTRUCT },
    function(err, res) {
      if (err) {
        return console.error('Error invoking STFC_STRUCTURE:', err);
      }
      console.log('Result STFC_STRUCTURE:', res);
  });
});

No comments: