SAP ByDesign SOAP based web services consumption in NodeJS

Here is a small example how to consume SAP BYD SOAP services in NodeJS.

I’m going to use this service https://help.sap.com/doc/saphelp_byd1711_en/2017.11/en-US/PUBLISHING/PSM_ISI_R_II_QUERY_JOURNAL_ENTRY_IN.html

This QueryJournalEntryIn service is for reading journal entries from BYD system.
Example SOAPUI request:

Doing the same thing with NodeJS, express middlware and node-soap npm package.
We are still using the WSDL query but instead of using the default queries provided by the WSDL we can define our own just like in SOAPUI.

/*
This is the array with the different document types
00023 -- vendor invoice
00025 --customer invoice
00030 -- customer return
00084 -- Manual entry for Accounts Payable
00085 - Manual entry for Accounts recivable
00088 - Manual Entry for Sales Documents
00094 - Supplier Credit Memo

*/

const soap = require('soap');
const wsdl = './wsdl/QueryJournalEntryIn.wsdl';

const neededTypes = ['00025', '00023', '00030', '00084', '00085', '00088', '00094'] //heres the list of different types we need to filter out
const hitLimit = 5; //maximum number of answers from BYD endpoint
const startDate = "2014-01-28T00:00:01Z"; //start date for query selection, format is very important!
const endDate = "2014-12-29T23:59:59Z"; //end date for query selection, format is very important!
const setOfBooks = "1000";

module.exports = app => {
    app.get('/api/getjournalentries', async(req, res) => {

            try {
                let journalEntriesResponseToClient = []; //array to store the answer to the client
                      
                for (let type of neededTypes) { //<------ looping through the array that contains the types we need         
                    const journalEntriesResponse = await getJournalEntries(type); //<--- calling the getJournalEntreis function that returns a promise
                    JSON.stringify(journalEntriesResponse)
                    //Looping through the respone we got from ByDesign
                    for (var i in journalEntriesResponse.JournalEntry) {                   
                        journalEntriesResponseToClient.push(journalEntriesResponse.JournalEntry[i])                   
                    }
                }    
                res.send(journalEntriesResponseToClient)            

        } catch (err) {
            res.status(422).send(err);
        }
    })
 }

 function getJournalEntries(type) {
        return new Promise(function(resolve, reject) {
            //do something with reject
    
            soap.createClient(wsdl, function(err, client) {
                if(err) throw new Error(err);
    
                var args = { 
                    JournalEntrySelectionByElements: {
                        SelectionByAccountingDocumentTypeCode: {
                            InclusionExclusionCode: "I",
                            IntervalBoundaryTypeCode: "1",
                            LowerBoundaryCode: type,
                        },
                        SelectionByOriginalEntryDocumentCreationDateTime: {
                            InclusionExclusionCode: "I",
                            IntervalBoundaryTypeCode: "3",
                            LowerBoundaryDateTime: startDate, //this is defined in the top of this file
                            UpperBoundaryDateTime: endDate //this is defined in the top of this file
                        },
                        SelectionBySetOfBooksID: {
                            InclusionExclusionCode: "I",
                            IntervalBoundaryTypeCode: "1",
                            LowerBoundaryIdentifier: setOfBooks //this is defined in the top of this file
                        }
                        
                    },
                    ProcessingConditions: {
                        QueryHitsMaximumNumberValue: hitLimit, //this is defined in the top of this file
                        QueryHitsUnlimitedIndicator: false
                    }	
                    
                };
                
    
                client.setSecurity(new soap.BasicAuthSecurity('_ZJOURNALENT', '*********'));
    
                client.service.FindByElements
    
                client.FindByElements(args, function(err, res) {
                    if(err) throw new Error(err.body);
    
                    if(!err) {
                        resolve(res)
                        //console.log(client.lastRequest) //<--Will show the last request for debugging		
                    }
                })
            });
        })
    }

 

In the example above i’m also making multiple queries to SOAP service since I only need few specific document types:

 

      
                for (let type of neededTypes) { //<------ looping through the array that contains the types we need         
                    const journalEntriesResponse = await getJournalEntries(type); //<--- calling the getJournalEntreis function that returns a promise
                    JSON.stringify(journalEntriesResponse)
                    //Looping through the respone we got from ByDesign
                    for (var i in journalEntriesResponse.JournalEntry) {                   
                        journalEntriesResponseToClient.push(journalEntriesResponse.JournalEntry[i])                   
                    }
                }    
                res.send(journalEntriesResponseToClient)

 

And now e get response back from ByDesign:

That’s it. If any questions, please leave a comment below.

Leave a comment

Your email address will not be published. Required fields are marked *