Installation
npm install node-sapb1
SAPb1 Class
Methods |
---|
resource(name) Gets a Resource object using the specified name. |
session() Gets the SAP B1 Session data. |
query(query, join, success, error) Querys SAP B1 Service Layer with oData query lanaguage. |
Resource Class
Methods |
---|
create(data, success, error) Posts data to the resource to be created. |
update(id, data, success, error) Posts data to the resource to be updated using the specified id. |
delete(id, success, error) Deletes an entity from the resource using the specified id. |
action(id, action, success, error) Executes an action on the resource using the specified id. |
queryBuilder() Returns a new instance of Query. |
Query Class
Methods |
---|
select(fields) The entity fields to return. Returns the current Query instance. |
where(filter) Adds a Filter to the filter collection. This method performs an AND operation. Returns the current Query instance. |
orWhere(filter) Adds a Filter to the filter collection. This method performs an OR operation. Returns the current Query instance. |
limit(top, skip) Limits the results. Returns the current Query instance. |
inlineCount() Includes the count of matched entities in the result. Returns the current Query instance. |
orderBy(field, direction) Orders the results by the specified field and order direction. Returns the current Query instance. |
count(success, error) Returns a count of entities. Returns the current Query instance. |
findAll(success, error) Returns all matches. |
find(id, success, error) Returns a single match using the specified id. |
Filters
Name |
---|
Filter(field, value) Abstract Filter class. |
Equal(field, value) This filter checks if the specified field is equal to the specified value. |
NotEqual(field, value) This filter checks if the specified field is NOT equal to the specified value. |
LessThan(field, value) This filter checks if the specified field is less than the specified value. |
LessThanEqual(field, value) This filter checks if the specified field is less than or equal to the specified value. |
MoreThan(field, value) This filter checks if the specified field is more than the specified value. |
MoreThanEqual(field, value) This filter checks if the specified field is more than or equal to the specified value. |
StartsWith(field, value) This filter checks if the specified field starts with the specified value. |
EndsWith(field, value) This filter checks if the specified field ends with the specified value. |
Contains(field, value) This filter checks if the specified field contains the specified value. |
InArray(field, collection) This filter checks if the specified field contains one or more of the values in the specified collection. |
NotInArray(field, collection) This filter checks if the specified field does NOT contain one or more of the values in the specified collection. |
Usage
Create an object to store your SAP Business One Service Layer configuration details. If you are using an https connection, you can attach the certificate by specifying the ca property of the configuration object as shown below.
Listing 1
var config = {
host : 'http(s): ip or hostname',
port : 50000,
version : 2,
username : 'SAP usernmae',
password : 'SAP password',
company : 'Company name',
ca : fs.readFileSync('/path/to/certificate.crt')
}
Login to the Service Layer to obtain a session as shown in listing 2 below.
Listing 2
const SAPb1 = require('node-sapb1');
SAPb1.createSession(config, sap => {
// Success
}, (error, type) => {
// Error
// type = 1, Connection errors
// type = 2, SAP response errors.
});
If the connection is successful, the success callback function is called and passed a new instance of SAPb1 as the callback argument. The SAPb1 object provides a resource(name) method which returns a new instance of Resource with the specified name. Using this Resource object you can perform CRUD actions.
Querying A Service
The following code sample demonstrates how to query a Sales Order service and return a JSON object with multiple entries using the queryBuilder() method. The queryBuilder() method returns a new instance of Query.
Listing 3
SAPb1.createSession(config , sap => {
sap.resource("Orders")
.queryBuilder()
.select("DocNum, DocEntry, CardCode, DocumentLines")
.orderBy("DocNum", "asc")
.findAll(data => {
console.log(data);
}, (error, type) => {
console.log(error, type);
});
}, (error, type) =>{
console.log(error, type);
});
To return a single entry, use the find(id, success, error) method as shown below.
Listing 4
SAPb1.createSession(config , sap => {
sap.resource("Orders")
.queryBuilder()
.select("DocNum, DocEntry, CardCode, DocumentLines")
.orderBy("DocNum", "asc")
.find(1474, data => {
console.log(data);
}, (error, type) => {
console.log(error, type);
});
}, (error, type) =>{
console.log(error, type);
});
Depending on the service, id may be a numeric value or a string.
Creating A Service
The following code sample shows how to create a new Sales Order using the create() method of the Resource object.
Listing 5
SAPb1.createSession(config , sap => {
sap.resource("Orders").create({
CardCode: "BP Card Code",
DocDueDate: "Doc due date",
DocumentLines: [
{
ItemCode: "Item Code",
Quantity: "200",
}
]
}, (data) => {
console.log(data);
}, (error, type) => {
console.log(error, type);
});
}, (error, type) =>{
console.log(error, type);
});
You must provide any User Defined Fields that are required in the create object. If successful, the newly created Sales Order will be returned as a JSON object.
Updating A Service
The following code sample demonstrates how to update a service using the update() method of the Resource object.
Listing 6
SAPb1.createSession(config , sap => {
sap.resource('Orders').update(19165, {
Comments: "This is a comment",
}, (data) => {
console.log(data);
}, (error, type) => {
console.log(error, type);
});
}, (error, type) =>{
console.log(error, type);
});
Note that the first argument to the update() method is the id of the entity to update. In the case of a Sales Order the id is the DocEntry field. If the update is successful and empty JSON object is returned.
-
php-sapb1-v2 Library Documentation v2
This library has been updated and includes bug fixes from V1.
07 November 2024 - 44 views -
Stock Transfer Part 2
In the previous article I exaplined how to transfer stock between warehouses and bin enabled warehouses. In this article, I discuss how to transfer batch enabled items.
07 August 2020 - 4357 views -
Stock Transfer Part 1
The SAP B1 Service Layer provides an API to move stock between warehouses. This article explains the construction of the JSON payload data that is posted to the StockTransfers API.
12 December 2019 - 6890 views -
php-sapb1 Library Documentation v1
A simple and easy to use PHP library for SAP Business One Service Layer API.
16 August 2019 - 8850 views -
PHP Service Layer Example Part 2
This article is part 2 of PHP Service Layer Example. In the previous article, I explained how to initiate an authentication request to get a Service Layer session id and route id. In this article we use these details to execute a request to retrieve a list of business partners.
25 January 2018 - 10814 views -
PHP Service Layer Example Part 1
This article explains how to get connected to the Service Layer using PHP.
09 January 2018 - 14541 views -
Introduction To SAP Business One Service Layer
An introduction to the SAP Business One Service Layer.
08 December 2017 - 10766 views