DELPHI 调用SAP—RFC 示例
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.
Example 2:
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.
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.
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
unit logon1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, OleCtrls, StdCtrls, SAPFunctionsOCX_TLB, Grids, SAPLogonCtrl_TLB;
type TForm1 = class(TForm) SAPFunctions1: TSAPFunctions; Button2: TButton; Grid: TStringGrid; Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Label2: TLabel; SAPLogonControl1: TSAPLogonControl; procedure Button2Click(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } end;
var Form1 : TForm1 ; Table,Funct, Connection : VARIANT ;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject); var txt : string ; r : integer ;
begin 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' ;
if Connection.LogOn(0,true) = true then (* parameter "true" = SilentLogOn *)
begin
(* assign the existing connection to the *) (* component SAPFunctions1 *) SAPFunctions1.Connection := Connection;
Funct := SAPFunctions1.add('RFC_READ_TABLE'); Funct.exports('QUERY_TABLE').value := 'CSKT'; if not Funct.call then showMessage(Funct.exception) else begin Table := Funct.tables.item('DATA'); grid.rowCount := Table.rowcount + 1; grid.cells[0,0] := 'Client'; grid.cells[1,0] := 'CostCent-No'; grid.cells[2,0] := 'CostCent-Des.'; for r := 1 to grid.rowCount -1 do begin txt := Table.value(r,1); grid.cells[0,r] := copy(txt,0,3); grid.cells[1,r] := copy(txt,9,10); grid.cells[2,r] := copy(txt,27,20); end; grid.visible := True; end; end; end; end.
Example 5:
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.
Example 6:
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;
interface
uses
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 *) Funct := sapFunctions1.add('BAPI_PO_CREATE');
(*** define tables, use structures of the dictionary ***)
(* table for the purcaseorder header *) Header := funct.exports('PO_HEADER');
(* table of the purcaseorder items *) POItems := funct.tables.item('PO_ITEMS');
(* table of the schedules *) Schedules := funct.tables.item('PO_ITEM_SCHEDULES');
(*** filling the PO_Header-table ***)
(* purcasing document type *) Header.Value[2] := 'NB ' ;
(* purcasing document category *) Header.Value[3] := 'F' ;
(* purcasing organisation *) Header.Value[5] := '600' ;
(* purcasing group *) Header.Value[6] := '610' ;
(* vendor account number, on numeric values don't *) (* forget the leading zeroes!!! *) Header.Value[8] := '0099000123';
(*** filling the PO_Items-table ***)
(* add new row to the table *) ItemsRow := POItems.rows.add;
(* item number of purcasing document *) ItemsRow.Value[2]:='00010';
(* material-number, on numeric values don't forget *) (* the leading zeros !!! *) ItemsRow.Value[5]:='000000000000001161';
(* storage location *) ItemsRow.Value[11]:='100';
(* plant *) ItemsRow.Value[17]:='0001';
(* netprice in purcasing document, *) (* in document currency *) ItemsRow.Value[21]:='10,00';
(*** filling the PO_Items_Schedules-table ***)
(* add new row to the table *) SchedulesRow := Schedules.rows.add;
(* item number of purcasing document *) SchedulesRow.Value[1]:='00010';
(* category of delivery date *) SchedulesRow.Value[3]:='1';
(* item delivery date *) SchedulesRow.Value[4]:='30.05.2000';
(* scheduled quantity *) SchedulesRow.Value[6]:='10';
(*** call function ***)
if not funct.call then
(* on error show message *) showMessage(funct.exception)
else begin
(* show number of the purcaseorder *) MLDText:= funct.imports('PURCHASEORDER'); MessageDlg('purcaseorder '+MLDText+' created.', MTInformation,[mbOK],0); end; end;
end.
Example 7:
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.
Example 8:
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.
|
请发表评论