Delphi + Enterprise Connectors + RAD Server = SAP native client for iOS and Android

Enterprise Connectors allows you to connect from Delphi and C++ Builder to 80+ enterprise data sources: https://www.embarcadero.com/products/enterprise-connectors.

Between these data sources, we have SAP, one of the most used ERPs in the world.

Screen Shot 2017-11-17 at 17.25.30.png

To demonstrate how easy is to connect SAP using the Enterprise Connectors with FireDAC, I decided to build a native mobile client for Android and iOS, using Delphi and FMX on the client side, and RAD Server/FireDAC on the server side.

Solution overview

In summary, the idea is to allow a mobile application to display data directly from SAP in real time, for example, showing a summary of the sales in a period.

To accomplish this task we’ll have a FireDAC SAP connection in our RAD Server implementation, which will allows us to query the Sales Document entity via NetWeaver: http://www.se80.co.uk/saptables/v/vbak/vbak.htm

The cool thing from the Enterprise Connectors is that, you basically can issue standard SQL ANSI queries against any supported data source, easy as 1,2,3.

Server Implementation

Our server application is actually a RAD Server application used to publish the REST APIs that we’ll need. Before starting, I have installed the SAP driver from the Enterprise Connectors collection that are available in the GetIt as a trial: https://community.embarcadero.com/blogs/entry/enterprise-connectors-are-now-live

Our server has just one method, responsible for grabbing the data from the VBAK entity, which is serialized as JSON using the TFDSchemaAdapter:

procedure TSAPEMSServerResource1.Get(const AContext: TEndpointContext; const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
 fMem: TMemoryStream;
begin
 fMem := TMemoryStream.Create;
 try
 VbakView.Open;
 FDSchemaAdapter1.SaveToStream(fMem, TFDStorageFormat.sfJSON);
 AResponse.Body.SetStream(fMem, 'application/json', True);
 except
 on E: Exception do
 begin
 fMem.Free;
 raise;
 end;
 end;
end;

Mobile Client Implementation

Our client application has a ListView using the Dynamic Appearance style to display the sales data, and a TChart displaying the sales total by department.

Screen Shot 2017-11-17 at 18.00.09

procedure TMainForm.FormCreate(Sender: TObject);
begin
 chartResult.Series[0].Clear;
end;

procedure TMainForm.SpeedButton1Click(Sender: TObject);
var
 i: integer;
 MyThread: TThread;
begin
 EMSFireDACClient1.GetData;
 FDMemTable1.Open;

FDConnection1.Open;
 FDQuery1.Open;

MyThread := TThread.CreateAnonymousThread(
 procedure
 begin
 while not FDQuery1.Eof do
 begin
 TThread.Synchronize(MyThread,
 procedure
 begin
 chartResult.Series[0].Add(FDQuery1.FieldByName('TOTAL').AsFloat,
 FDQuery1.FieldByName('VKORG').AsString);
 end);
 FDQuery1.Next;
 end;
 end);
 MyThread.Start;
end;

One thing to notice in the code above, I’m using the FireDAC’s LocalSQL feature to execute one SQL query that summarize the data and fills the graph in the client side, avoiding additional calls to the server (in this case NetWeaver) and increasing the client performance.

Advertisements

Delphi + Enterprise Connectors + DataSnap = Salesforce native client for iOS and Android

Enterprise Connectors allows you to connect from Delphi and C++ Builder to 80+ enterprise data sources: https://www.embarcadero.com/products/enterprise-connectors.

Between these datasources, we have Salesforce, probably the #1 CRM in the world.

To demonstrate how easy is to use the Enterprise Connectors with FireDAC, I decided to build a native Salesforce mobile client for Android and iOS, using Delphi and Firemonkey on the client side, and DataSnap/FireDAC on the server side.

Solution overview

In summary, the idea is to allow the mobile application to display data from Salesforce cases, a kind of support case management that is part of their CRM solution.

To accomplish this task we’ll have an FireDAC Salesforce connection in the DataSnap server, which will allows us to validate the user credentials, as well to query the cases entries directly from the Salesforce in the clouds.

The cool thing from the Enterprise Connectors is that, you basically can issue standard SQL ANSI queries against any supported data source, easy as 1,2,3.

Server Implementation

Our server application is an standard Datasnap REST server application. Before starting, I have installed the Salesforce driver from the Enterprise Connectors collection that are available in the GetIt as a trial: https://community.embarcadero.com/blogs/entry/enterprise-connectors-are-now-live

 

As you can see in the image, it’s a standard FireDAC implementation, the only difference from connecting a regular database are the parameters. In this case, for Salesforce, you can create an developer account in https://developer.salesforce.com.

Our server has only two methods, one to authenticate the user, and another one to grab some Salesforce data:

function TServerMethods1.DoUserLogin(fUserName, fPassword: string): boolean;
begin
 FDCnn.Params.UserName := fUserName;
 FDCnn.Params.Password := fPassword;
 try
 // open the connection to validate the user/pass
 FDCnn.Open;
 Result := True;
 // close the connection after the validation
 FDCnn.Close;
 except
 on E: Exception do
 raise Exception.Create('UserLogin: ' + E.Message);
 end;
end;

function TServerMethods1.GetSFCases(fUserName, fPassword: string)
 : TFDJSONDataSets;
begin
 // active the connection
 FDCnn.Params.UserName := fUserName;
 FDCnn.Params.Password := fPassword;
 FDCnn.Open;

// serialize the dataset
 CaseTable.Close;
 Result := TFDJSONDataSets.Create;
 TFDJSONDataSetsWriter.ListAdd(Result, CaseTable);
end;

Again, it’s a standard DataSnap/FireDAC server, serializing and returning the JSON data to the client, in this case an multi device application with FMX.

Mobile Client Implementation

Our client application has a login form, a ListView using the Dynamic Appearance style to display the cases data, and an third tab that shows all the selected case details.

 

The data is de-serialized back to a TFDMemTable using the FireDAC reflection classes, as you can see in the excerpts below:

function TMainDM.UserLogin: boolean;
begin
 Result := False;
 DSRestCnn.Host := fServerIP;
 if ServerMethods1Client.DoUserLogin(fUserName, fPassword) then
 begin
 SaveLoginData;
 Result := True;
 end;
end;

procedure TMainDM.LoadSFCases;
var
 LDataSetList: TFDJSONDataSets;
begin
 DSRestCnn.Host := FServerIP;
 LDataSetList := ServerMethods1Client.GetSFCases(fUserName, fPassword);

CaseMemTable.Close;
 CaseMemTable.AppendData(TFDJSONDataSetsReader.GetListValue(LDataSetList, 0));
 CaseMemTable.Open;
end;

There are some other details in the client application, like saving the login information, the general app workflow, look&feel and etc., that you can review downloading the samples in the link by the end of this post.

 

All the source code from this sample is located here, in my personal GitHub: https://github.com/flrizzato/CDATA

And stay tuned, next week I’ll show how you can easily connect an real SAP/R3 implementation directly from an Delphi or C++ Builder, and display/modify some data from the ERP, with almost no code!

ps: in order to run the sample, you’ll need to create an Salesforce developer account and add the login information and security token in the FireDAC connection before trying to run.