An Outbound Example

 

This example illustrates an application program that waits for R/3 to call and receives one IDoc from it. The type of IDoc to be processed is MATMAS02. If you want to run this example, create a MATMAS02 IDoc in your R/3 system with the following segments:

...E2MARAM

E2MAKTM

E2MARCM

E2MARCM

E2MARCM

E2MARMM

E2MBEWM

E2MBEWM

E2MVKEM

E2MLANM

E2MLANM

 

In this program, the contents of selected fields of each segment are extracted, cast to appropriate data types, and then written to a file. The use of various member functions for segment iteration such NextSibling , GetChild and NextRepetition are illustrated.

#include "idoctidm.h"

#include "idoclink.h"

#include <ios>

#include <iostream>

#include <fstream>

#include <vector>

#include <string>

 

void main(int argc, char ** argv)

{

char filename[64];

fstream file;

CRfcConnection Connection;

string strField, strE2maram;

// open a file named idoc001.out

file.open("idoc001.out", ios_base::out | ios_base::trunc);

if(!file.good())

{

cout << "Failure when opening file: " << filename << endl

<< flush;

exit(1);

}

RFC_USER_INFO UserInfo; //All user login info

RFC_CONNECT_INFO ConnectInfo; //R/3 system connection info

//Define user logon info

ConnectInfo.rstrDestination = "ID3";

ConnectInfo.rstrHostName = "orlando1.pal.sap-ag.de";

ConnectInfo.nSystemNo = 61;

ConnectInfo.rstrSystemName = "ID3";

UserInfo.rstrClient ="001";

UserInfo.rstrUserName = "KHSIEH";

UserInfo.rstrPassword = "sapsap";

UserInfo.rstrLanguage = "E";

//Define system connection info

ConnectInfo.rfcMode = RFC_MODE_R3ONLY ;

//Fill user info and connection info into the

//connection object so that they will be used

//when starting logon

Connection.SetUserInfo(UserInfo);

Connection.SetConnectInfo(ConnectInfo);

CIDocTID TidMgr("plants.tid","plants.lck","don");

CIDocLink Link1(OUTBOUND_LINK,&Connection,FALSE,&TidMgr);

// Connect to R/3 system to accept RFC calls

try

{

Connection.Accept(argv);

}

catch (RFC_ERROR_INFO Err)

{

cout << Err.message << endl << flush;

return;

}

try

{

Link1.GetIDocsFromR3();

int nIDocCount = Link1.GetIDocCount();

CIDoc& IDoc1 = Link1.GetIDoc(0);

CIDocIterator it1(IDoc1);

CIDocIterator it2(IDoc1);

CIDocIterator it3(IDoc1);

CIDocIterator it4(IDoc1);

CIDocIterator it5(IDoc1);

 

it1.GoTop();

// Position it2 on the first child of "TOP", which is E2MARAM

it2 = it1.GetChild(0);

file << it2.GetName() << ":" << endl

<< " matnr - " << (CSTR)it2.Field("MATNR") << endl

<< " ersda - " << (CSTR)it2.Field("ERSDA") << endl

<< " blanz - " << (CSTR)it2.Field("BLANZ") << endl

<< " brgew - " << (RFC_FLOAT)it2.Field("BRGEW") << endl

<< " ntgew - " << (RFC_FLOAT)it2.Field("NTGEW") << endl

<< " volum - " << (RFC_FLOAT)it2.Field("VOLUM") << endl

<< " wesch - " << (RFC_FLOAT)it2.Field("WESCH") << endl

<< " gewto - " << (RFC_FLOAT)it2.Field("GEWTO") << endl

<< " fuelg - " << (CSTR)it2.Field("FUELG") << endl

<< " mhdrz - " << (RFC_INT)it2.Field("MHDRZ") << endl

<< " mhdlp - " << (RFC_INT2)it2.Field("MHDLP") << endl;

file << "Maram ChildCount = " << it2.GetChildCount() << endl;

// Position it3 on E2MAKTM, the first child of E2MARAM.

it3 = it2.GetChild(0);

file << it3.GetName() << ":" << endl

<< " spras - " << (CSTR)it3.Field("SPRAS") << endl

<< " maktx - " << (CSTR)it3.Field("MAKTX") << endl;

// Position it3 on the second child, or child with index 1, of

// E2MARAM, which is E2MARCM

it3 = it2.GetChild(1);

file << it3.GetName() << ":" << endl

<< " werks - " << (RFC_INT)it3.Field("WERKS") << endl

<< " plifz - " << (CSTR)it3.Field("PLIFZ") << endl

<< " ausss - " << (RFC_FLOAT)it3.Field("AUSSS") << endl

<< " ueeto - " << (CSTR)it3.Field("UEETO") << endl

<< " ladgr - " << (CSTR)it3.Field("LADGR") << endl

<< " prfrq - " << (RFC_INT2)it3.Field("PRFRQ") << endl

<< " shzet - " << (CSTR)it3.Field("SHZET") << endl;

// Re-position it3 on E2MAKTM

it3 = it2.GetChild(0);

file << it3.GetName() << "'s siblings:" << endl;

while (it3.NextSibling())

file << " " << it3.GetName() << endl;

// Re-position on the child with index 5 of E2MARAM, which,

// in this example case, is E2MBEWM. Loop to get all

// subsequent siblings.

it3 = it2.GetChild(5);

file << it3.GetName() << "'s siblings:" << endl;

while (it3.NextSibling())

file << " " << it3.GetName() << endl;

// Re-position it3 on E2MAKTM, loop to get more repetitions of

// E2MAKTM

it3 = it2.GetChild(0);

file << it3.GetName() << "'s repetitions:" << endl;

while (it3.NextRepetition())

file << " " << it3.GetName() << endl;

// Position it3 on second of 3 occurrences of E2MARCM, and

// loop to get to all subsequent repetitions. In this example

// case, there should be only one more.

it3 = it2.GetChild(2);

file << it3.GetName() << "'s repetitions:" << endl;

while (it3.NextRepetition())

{

file << " " << it3.GetName() << endl;

}

}

catch (IDOC_ERROR_INFO Err)

{

DumpIDocErrInfo(Err);

}

file << flush;

file.close();

}