Skill Level: Beginner
Time Needed: 20 minutes
I recently assisted someone on the forums when they were having trouble creating records in their SAP ECC instance while using Gateway. I thought I would share some very simple code showing what it takes to write an entry into a service via JSON from iOS (Objective C). The reason the title of the document is "Simple Sample" - there are no frills. Whenever I develop a new app, I start small, get the basic functionality working, and proof out the process before getting too creative.
*** If you do not have your own gateway server, you might be able to connect this to the SAP Gateway System which can be found here. Create an account and utilize this service *** DISCLAIMER: Untested as the signup form does not work
The small application sample below uses this document as a starting point. It describes setting up CRUD (Create, Read, Update and Delete) operations for the Bank account list in your back-end, based on the Bank BAPI. For our example, we will be using the Read and Create functionality of the service. Our example will use a open source repository called AFNetworking. The download can be found here, is ARC compatible and is very easy to add and reference in your project. I will skip the basics of creating a iOS project and adding the reference to AFNetworking (but for reference I am using a "Single View" app template in XCode).
Read Operation from your iOS App
Reading from a OData service using JSON can be done in as little as 7 lines of code in objective C (When using the AFNetworking classes). Here is my example code which can be used to read from your service. In my case, I am returning a list of Banks. Below is the code and the output.
Create Operation from your iOS App
Creating a new record adds a small requirement, in that a CSRF (Cross Site Request Forgery) Token needs to be added to the POST request header. If you notice above, I have added this "fetch" header to my GET request, thus returning the key we need along with our data. (2 birds, 1 stone). Here is the create request. Pending it is successful, it will return the record which have been added to your back-end system. Below is the code and output.
Here is a link to the GitHub project to avoid you writing all of this out. Once again - download and follow this useful guide from Gateway team to create the Bank service in your Gateway system, download the project from GitHub, update your gateway server name and details and this will be a useful starting point for creating your next enterprise app.
Disclaimer: Keep in mind the code purposely simplifies the code needed. If you feeling more adventurous:
- Ensure your gateway server is using HTTPS.
- Instead of using a NSString *dataset, use NSDictionary object/Class to the Request using JSONRepresentation
- Add your username and password to the header request, Base64 encoding them.
- Instead of writing the JSON response out to a UITextView, add some UITextBoxes and parse the values out.
Can anyone spot the field I forgot to post?