BAPI / RFC with Delphi |
||||||||||||||||||||||||||||
Examples
|
||||||||||||||||||||||||||||
top of page |
Logon to the R3-system with the componente TSAPLogOnControl
In this example the form TForm1 contains the following components:
Component | Function |
SAPLogOnControl1 | SAP ActiveX-Component to logon to the system |
Button1 | Button to start the procedure |
unit s_logon;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, OleCtrls, SAPLogonCtrl_TLB, StdCtrls,Grids ;
type
TForm1 = class(TForm)
SAPLogonControl1: TSAPLogonControl;
Panel1: TPanel;
StaticText1: TStaticText;
Button1: TButton;
procedure SAPLogonControl1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
Connection :variant;
implementation
{$R *.DFM}
procedure TForm1.SAPLogonControl1Click(Sender: TObject);
begin
(* define connection *)
Connection:= SAPLogOnControl1.NewConnection;
(* start LogOn *)
if Connection.LogOn(0,false) = true then
begin
showmessage('Logon O.K.');
Button1.Enabled:= true;
end
else
begin
ShowMessage('Error on Logon :-(((');
SAPLogonControl1.Enabled:=true;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
(* cut connection *)
Connection.LogOff;
ShowMessage('System LogOff...');
SAPLogonControl1.Enabled:=true;
Button1.enabled :=false;
end;
end.
top of page |
Logon to the R3-system with the Component TSAPLogOnControl
and SilentLogOn
In this example the form TForm1 contains the following components:
Component | Function |
SAPLogOnControl1 | SAP ActiveX-Component to logon to the system |
Button1 | Button to cut the connection |
unit s_logon;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, OleCtrls, SAPLogonCtrl_TLB, StdCtrls,Grids ;
type
TForm1 = class(TForm)
SAPLogonControl1: TSAPLogonControl;
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
StaticText1: TStaticText;
Button1: TButton;
procedure SAPLogonControl1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
Connection :variant;
implementation
{$R *.DFM}
procedure TForm1.SAPLogonControl1Click(Sender: TObject);
begin
(* define connection and it's parameters *)
Connection := SAPLogoncontrol1.newConnection;
(* In some GUI-versions the username *)
(* must be written in uppercase !!! *)
Connection.User := AnsiUpperCase(Edit1.text);
Connection.System
:= 'IDS';
Connection.Client
:= '800';
Connection.ApplicationServer := 'SAPIDES';
Connection.SystemNumber := '00';
Connection.Password :=
Edit2.text;
Connection.Language := 'DE' ;
SAPLogonControl1.Enabled := false;
if Connection.LogOn(0,true) = true then
(* parameter "true" : SilentLogOn *)
begin
ShowMessage('Logon O.K.');
Button1.Enabled:= true;
end
else
begin
ShowMessage('Error on logon :-(((');
SAPLogonControl1.Enabled:=true;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
(* cut connection *)
Connection.LogOff;
ShowMessage('System LogOff...');
SAPLogonControl1.Enabled:=true;
Button1.Enabled :=false;
end;
end.
top of page |
Example 3:
Read all costcenters with the function RFC_READ_TABLE
In this example the form TForm1 contains the following components:
Component | function |
SAPFunctions1 | SAP ActiveX-component to connect RFC/BAPI |
Grid | Stringgrid to show the data's in the form |
Button1 | Button to start the procedure |
unit logon1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ExtCtrls, OleCtrls, StdCtrls, SAPFunctionsOCX_TLB, Grids;
type
TForm1 = class(TForm)
SAPFunctions1: TSAPFunctions;
Button1: TButton;
Grid: TStringGrid;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1 : TForm1 ;
Table,Funct : VARIANT ;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var txt : string ;
r : integer ;
begin
(* define function *)
Funct := SAPFunctions1.add('RFC_READ_TABLE');
(* tell the function what table should be read *)
Funct.exports('QUERY_TABLE').value := 'CSKT';
(* call the function *)
if not Funct.call then
(* on error show message *)
showMessage(Funct.exception)
else begin
(* select table with the data's *)
Table := Funct.tables.item('DATA');
(* addjust the StringGrid *)
grid.rowCount := Table.rowcount + 1;
grid.cells[0,0] := 'Client';
grid.cells[1,0] := 'Kostenstelle';
grid.cells[2,0] := 'Bezeichnung';
for r := 1 to grid.rowCount -1 do begin
(* select first dataset *)
txt := Table.value(r,1);
(* Because the RCF-function returns only one
*)
(* string whitch contains all data's, the
*)
(* string must be cut to different parts
*)
grid.cells[0,r] := copy(txt,0,3); (* Client
*)
grid.cells[1,r] := copy(txt,9,10); (* CostCent-number *)
grid.cells[2,r] := copy(txt,27,20); (* CostCent-description*)
end;
grid.visible := True;
end;
end;
end.
top of page |
Example 4:
Read all costcenters with the function RFC_READ_TABLE and logon with SilentLogOn
In this example the form TForm1 contains the following components:
Component | Function |
SAPFunctions1 | SAP ActiveX-component to connect RFC/BAPI |
SAPLogoncontrol1 | SAP ActiveX-Component to logon to the system |
Grid | Stringgrid to show the data's in the form |
Button1 | Button to start the procedure |
top of page |
Show detail of a material with the BAPI BAPI_MATERIAL_GET_DETAIL
In this example the form TForm1 contains the following components:
Component |
Function |
SAPFunctions1 | SAP ActiveX-component to connect RFC/BAPI |
Panel1/Panel2 | Elements to show the data's |
Button1 | Button to start the procedure |
unit material;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, ExtCtrls, OleCtrls, SAPFunctionsOCX_TLB;
type
TForm1 = class(TForm)
SAPFunctions1: TSAPFunctions;
Panel1: TPanel;
Panel2: TPanel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
funct, tab, connection: variant;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
(* define function *)
funct := sapFunctions1.add('BAPI_MATERIAL_GET_DETAIL');
(* hand over material-number
*)
(* On numeric values don't forget the leading zeros!!!
*)
funct.exports('MATERIAL').value := '000000000000017550';
(* call the function *)
if not funct.call then
(* on error show message *)
showMessage(funct.exception)
else
begin
(* select table with the returned data's *)
tab := funct.imports.item('MATERIAL_GENERAL_DATA');
(* The variable TAB now contains the returned data's. The
*)
(* structure is like the structure BAPIMATDOA in the
*)
(* dictionary. We will show the 1. and 3. value of this
*)
(* structure
*)
Panel1.caption := tab.value(1);
Panel2.caption := tab.value(3);
end;
end;
end.
top of page |
Create a purcaseorder with the BAPI BAPI_PO_CREATE
In this example the form TForm1 contains the following components:
Component | Function |
SAPFunctions1 | SAP ActiveX-component to connect RFC/BAPI |
Button1 | Button to start the procedure |
Panel1 | not relevant! |
unit PO_Create;
interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, OleCtrls, SAPFunctionsOCX_TLB, ExtCtrls;
type
TForm1 = class(TForm)
SAPFunctions1: TSAPFunctions;
Button1: TButton;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
Funct,
Header,
POItems,
Schedules,
ItemsRow,
SchedulesRow: Variant;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var MLDText : String;
begin
Button1.Enabled:=false;
Panel1.Caption := 'RFC ist running, please wait.....';
(* define function *)
if not funct.call then
(* on error show message *) else
begin
end.
top of page |
Show the details of material with the BusinessObject BUS1001
In this example the form TForm1 contains the following components:
Component | Function |
SAPBAPIControl1 | SAP ActiveX-Component to connect to BAPI |
Button1 | Button to start the procedure |
Panel1 | Element to display the material-description |
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, SAPBAPIControlLib_TLB, ExtCtrls;
type
TForm1 = class(TForm)
SAPBAPIControl1: TSAPBAPIControl;
Button1: TButton;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
Connection,MAT : Variant;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
Connection:= SAPBapiControl1.Connection;
if Connection.Logon then
begin
ShowMessage('LogOn');
(* Call the object with the needed parameters *)
MAT:=
sapbapicontrol1.GetSAPObject('BUS1001','000000000000017550');
(* Display material-description *)
Panel1.Caption:=MAT.MATERIALDESCRIPTION;
end;
end;
end.
top of page |
Create an purchaseorder with the BusinessObject BUS2012
In this example the form TForm1 contains the following components:
Component | Function |
SAPLogonControl1 | SAP ActiveX-Component to logon to the system |
SAPBAPIControl1 | SAP ActiveX-Component to connect to BAPI |
Button1 | Button to start the procedure |
Button2 | Button to logon |
Panel1-3 | Elements to display messages |
unit best;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, SAPBAPIControlLib_TLB, ExtCtrls, Grids,
SAPLogonCtrl_TLB;
type
TForm1 = class(TForm)
SAPBAPIControl1: TSAPBAPIControl;
Button1: TButton;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Button2: TButton;
SAPLogonControl1: TSAPLogonControl;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
Connection,Mat,Header,Ret,Schedul,Item : Variant;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
(* select BusinessObject *)
Mat:= SAPBapiControl1.GetSAPObject('BUS2012');
(* define structures *)
Header := SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','PoHeader');
Schedul:= SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','PoItemSchedules');
Item := SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','PoItems');
Ret := SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','Return');
(* purchaseorder header data's *)
Header.value ('DOC_TYPE') := 'NB';
Header.value ('DOC_CAT') := 'F';
Header.value ('PURCH_ORG'):= '10';
Header.value ('PUR_GROUP'):= '10';
Header.value ('VENDOR') := '0010000999';
(* data for position 00010 *)
Item.Rows.Add;
Item.Value (1,'PO_ITEM') := '00010';
Item.Value (1,'PUR_MAT') := '000000000000000017';
Item.Value (1,'STORE_LOC') := '100';
Item.Value (1,'PLANT') := '1000';
Item.Value (1,'NET_PRICE') := '10,00';
(* schedules for position 00010 *)
Schedul.Rows.Add;
Schedul.Value (1,'PO_ITEM') := '00010';
Schedul.Value (1,'DEL_DATCAT') := '1';
Schedul.Value (1,'DELIV_DATE') := '20.09.2000';
Schedul.Value (1,'QUANTITY') := '10';
(* data for position 00020 *)
Item.Rows.Add;
Item.value (2,'PO_ITEM') := '00020';
Item.value (2,'PUR_MAT') := '000000000000001161';
Item.value (2,'STORE_LOC') := '100';
Item.value (2,'PLANT') := '1000';
Item.value (2,'NET_PRICE') := '10,00';
(* schedules for position 00020 *)
Schedul.Rows.Add;
Schedul.Value (2,'PO_ITEM') := '00020';
Schedul.Value (2,'DEL_DATCAT') := '1';
Schedul.Value (2,'DELIV_DATE') := '20.09.2000';
Schedul.Value (2,'QUANTITY') := '10';
(* call the method CreateFromData *)
Mat.CreateFromData (PoHeader
:= Header,
SkipItemsWithError
:= ' ',
PoItems
:= Item,
PoItemSchedules
:= Schedul,
Return
:= Ret);
(* Errors are saved in the structure Ret *)
if Ret.RowCount > 0 then
begin
Panel1.Caption:= Ret.Value (1,'TYPE');
Panel2.Caption:= Ret.Value (1,'MESSAGE');
end
(* If the method was calles without errors, *)
(* display the number of the purchaseorder *)
else Panel2.Caption:= Mat.PurchaseOrder;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
(* Logon to the system *)
Connection
:= SAPLogoncontrol1.newConnection;
Connection.User
:=
Ansiuppercase(Edit1.text);
Connection.System
:= 'IDS';
Connection.Client
:= '800';
Connection.ApplicationServer := 'SAPIDES';
Connection.SystemNumber := '00';
Connection.Password :=
Edit2.text;
Connection.Language :=
'DE' ;
SAPLogonControl1.Enabled := false;
if Connection.LogOn(0,true) = True then
begin
ShowMessage('Logon O.K.');
Button1.Enabled:= true;
(* assign the existing connection to the *)
(* component SAPBapiControl1
*)
SapBapiControl1.Connection:=Connection;
end
else
begin
ShowMessage('Error on logon :-(((');
end;
end;
end.
top of page |