Enterprise Resource Planning Blogs by SAP
Get insights and updates about cloud ERP and RISE with SAP, SAP S/4HANA and SAP S/4HANA Cloud, and more enterprise management capabilities with SAP blog posts.
Showing results for 
Search instead for 
Did you mean: 

Update 20/01/20

As  part of the strategy of SAP Cloud Platform. Several standard services like the ones used in this tutorial were deprecated. I adapted the code and update the tutorial so it stills relevant.

If you want to implement an application that makes use of a persistence layer (Postgres) and a Cache mechanism (Redis). Please check this other hands-on exercise, that leverage Azure services. This hands-on was offered as part of the Intelligent Enterprise Webinar Series.

The paradigm for SAP Business One solutions have completely changed (you probably notice that if you were not hiding in a cave). It started with small steps when we first adopted SAP HANA as our platform. The main drive was the push the add-ons' business logic from the legacy fat client to the server and, by the usage of Service Layer, leveraging standard protocols such as Odata we opened SAP Business One to countless possibilities. With Social Media integration, IoT and Voice Interfaces, just to name a few

In a more recent wave. We have digital transformation happening in the cloud and all the benefits that comes with it. Much further than a simplistic idea of porting your logic to the server. It gives your solution an amazing flexibility You can choose the technology, the services, when to implement new features and which customer will have access to it. And of course, hence you will be untangled from SAP Business One, you can create "multiple erp" solutions, like the SMB Marketplace or simply plug your B1 app to other system, like we did with the Amazon Echo and ByD.

With that mindset, I developed a tutorial repository to go trough some features of a cloud application. Such as deployment, scalability and persistence.

In this blog I am going to teach you how to get start and create your first SAP Business One loosely coupled solution. So get your git installed (shame on you if didn't have it) and let's do some code!


Get your free SAP Cloud Platform trial Account with Cloud Foundry

This is all you need to get started and It's quite easy to do it. Just follow this great tutorial and you will be all set

Do I need a SAP Business One System?

Not at all! The app supports the B1 APIs available in the SAP API Business Hub so we can play with B1 data in a sandbox without having a system.

You will need an API Key which you can get by clicking in the button in the details page

In case you want to use your own system. Make sure the B1 Service Layer port is exposed to the public internet, otherwise your loosely coupled app won't be able to fetch data from it. If you don't have a valid SSL certificate (which is the case for most dev environments). You can use the http port 50001.

Step 1 - Clone the Git Repository and deploy the app on Cloud Foundry

This is really basic, we first clone the GitHub repository executing
$ git clone https://github.com/B1SA/scp-b1.git

You should have a directory with all the files from the master branch in it. Navigate to this new directory and change it for the first step of our exercise with:
$ git checkout step_1

Time to send the app to the cloud!

If you followed the SAP Cloud Platform tutorial (the last step) mentioned before, you are already logged into the Cloud Foundry environment so in this case, create your cloud app is simple as:
$ cf push --random-route

--random-route will avoid name collisions with others that deploy this same app on SCP. You can choose your own app name by changing the manifest.yml file.

By the end of the process, your terminal should show something like this:

You can clearly see the route where your app is running and check it from your browser

Ok, but this is an empty HTML page. Where are the B1 Items?

Hold your horses, cowboy! We've just deployed the app, now we need to configure the environment. We could hard-code routes and credentials in the codebase. But this is not a Cloud development best practice. Instead, you should always use environment variables.

Set an environment variable for one app on cloud foundry is easy as:
$ cf set-env <appname> <env variable Name> <value>

The app require the following environment variables:
//Basic Variables
$ cf set-env scp-b1 B1_SERVER_ENV http://<your b1 server> or API HUb
$ cf set-env scp-b1 B1_SLPORT_ENV <Service Layer Port> or "" if API HUB
$ cf set-env scp-b1 B1_SLPATH_ENV <Service Layer path> or API HUB path
$ cf set-env scp-b1 B1_COMP_ENV <B1 Company DB>

//When using your own environment
$ cf set-env scp-b1 B1_USER_ENV <B1 User Name>
$ cf set-env scp-b1 B1_PASS_ENV <B1 User Password>

//When using the SAP API HUB
$ cf set-env scp-b1 APIKey <Your API Hub Key>
$ cf set-env scp-b1 APIHUB true

In my case, using the API HUB the variables would be like:
$ cf set-env scp-b1 B1_SERVER_ENV https://sandbox.api.sap.com
$ cf set-env scp-b1 B1_SLPORT_ENV ""
$ cf set-env scp-b1 B1_SLPATH_ENV /sapb1/b1s/v2/
$ cf set-env scp-b1 B1_COMP_ENV SBODEMOUS
$ cf set-env scp-b1 APIHUB true
$ cf set-env scp-b1 APIKey iamnotshowingyoumykey

time to restart the app and check it once again
$ cf restart scp-b1


Step 2 - Monitoring and Scaling

Let's assume your solution that lists B1 Items is a huge success. A lot of users are accessing it. First thing you do is to monitor the app's usage with:
$ cf app cfdemosummit18

Now it's time to scale our application to support more users. Let's do a scale out, that will create more instances of our application to support a high traffic, for example.
//Then we can create, lets say, 3 instances of the app with 
$ cf scale scp-b1 -i 3

Checking how the app looks like now  you will see the multiple instances:

The Cloud Foundry Environment variable CF_INSTANCE_INDEX returns the number of the instance which your app is running. You can see my implementation here.

If you access your app from the web browser and refresh the page fast enough, you can check that the web page is provided by different servers. The load balancing it's done automatically so you don't have to worry about that.


Step 3 - Cloud Foundry Services (Persistence)

The SAP Cloud Platform offers a great amount of services to support and enhance your application. Many of those are available in the Cloud Foundry environment. To check what services are assigned to your trial account
$ cf markeplace

There are two steps to have a service in your application, first you need to create it and then bind it to one or many apps. Like this:
$ cf create-service <Service> <Plan> <service_instance_name>
$ cf bind-service <app name> <service_instance_name>

In our case we want to create a PostgreSQL DB Service named itemdb. There are 2 plans available, we gonna use the V9.4-dev. Then we will bind it to the cfdemosummit18 app

You can check the services and bounds in your account by executing $ cf services. Once a service is bound, the connection parameters are availabe in the VCAP_SERVICES environment variable. You can see my implementation here.

To check the Environment Variables of your app:
$ cf env scp-b1

As in the previous step, checkout the step_3 branch and push it to the cloud. You will be able to interact with your new DB:

Step 4 - Create B1 Items from Cloud DB

Last step is just to close the cycle and synchronize the items created in the PostgreSQL db in the cloud to your B1 environment using Service Layer. It doesn't use any additional cloud feature but you can check the code here

That's it!

Hope you find this tutorial useful and get start developing cloud applications. You can also find a .Net version for the same app. Ithe Cloud is all about choice, so choose your runtime, tools and platforms and let us know if you have any questions. Just ping me on twitter @Ralphive