Send IDoc to SAP : Reference clientIDoc.py #340
-
Hi @bsrdjan , All, Can you please help with sample implementations of IDocs. Thank You!
<?xml version='1.0' encoding='UTF-8'?>
<Z_PM_IORDER01>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<DIRECT>2</DIRECT>
<IDOCTYP>IORDER01</IDOCTYP>
<SNDPRT>LS</SNDPRT>
<SNDPRN>MYDEVENV</SNDPRN>
<RCVPOR>SAPA01</RCVPOR>
<SNDPOR>MYDEVENV_S</SNDPOR>
<RCVPRT>LS</RCVPRT>
<RCVPRN>LOGA01C007</RCVPRN>
<CREDAT>20231012</CREDAT>
<CRETIM>20231012</CRETIM>
<CIMTYP>Z_PM_IORDER01</CIMTYP>
<MESTYP>IORDER</MESTYP>
<MESFCT>NEW</MESFCT>
<MESCOD>POC</MESCOD>
</EDI_DC40>
<E1ORHDR SEGMENT="1">
<AUART>ABC1</AUART>
<PRIOK>1</PRIOK>
<ILART>B01</ILART>
<E1OROPR SEGMENT="1">
<LTXA1>Test Desc</LTXA1>
<ZE1_PM_E1OROPR SEGMENT="1">
<USR00>SFDC_TEST</USR00>
</ZE1_PM_E1OROPR>
</E1OROPR>
<E1OROPR SEGMENT="1">
<LTXA1>Test Desc 1</LTXA1>
<ZE1_PM_E1OROPR SEGMENT="1">
<USR00>SFDC_TEST</USR00>
</ZE1_PM_E1OROPR>
</E1OROPR>
<E1OROPR SEGMENT="1">
<LTXA1>Test Desc 2</LTXA1>
<ZE1_PM_E1OROPR SEGMENT="1">
<USR00>SFDC_TEST</USR00>
</ZE1_PM_E1OROPR>
</E1OROPR>
<E1OROPR SEGMENT="1">
<LTXA1>Test Desc 3</LTXA1>
<ZE1_PM_E1OROPR SEGMENT="1">
<USR00>SFDC_TEST</USR00>
</ZE1_PM_E1OROPR>
</E1OROPR>
<ZE1ORHDR SEGMENT="1">
<ZZ_DMP_ORD/>
<RELKZ>X</RELKZ>
</ZE1ORHDR>
<E1ORTOB SEGMENT="1">
<EQUNR>123456</EQUNR>
</E1ORTOB>
</E1ORHDR>
</IDOC>
</Z_PM_IORDER01>`
import json
import sys
from pyrfc import Connection
def get_idoc_desc(idoc_id):
idoc_control = {
'TABNAM': 'EDI_DC40',
'DIRECT': '2',
'IDOCTYP' : 'IORDER01',
'SNDPRT' : 'LS',
'SNDPRN' : 'MYDEVENV',
'RCVPOR' : 'SAPA01',
'SNDPOR' : 'MYDEVENV_S',
'RCVPRT' : 'LS',
'RCVPRN' : 'LOGA01C007',
'CIMTYP' : 'Z_PM_IORDER01',
'MESTYP' : 'IORDER',
'MESFCT' : 'NEW',
'MESCOD' : 'POC'
}
idoc_data_dicts={
'AUART' : 'ABC1',
'PRIOK' : '1',
'ILART' : 'D01',
#implement nested structure??
}
new_idoc_data_dicts={
"SEGNAM": "E1ORHDR",
"SDATA" : json.dumps(idoc_data_dicts)
}
return {
"IDOC_CONTROL_REC_40": [idoc_control],
"IDOC_DATA_REC_40": [new_idoc_data_dicts],
}
def lambda_handler(event, context):
try:
print("Connecting to SAP")
conn = Connection(ashost='a02.domain.com', sysnr='01', client='001', user='MYUSER', passwd='MYPSWD')
print("Connected to SAP!!")
# Define the IDOC
idoc_id = 1
idoc = get_idoc_desc(idoc_id)
unit = conn.initialize_unit()
print('Sending an IDOC to SAP')
print(idoc)
result=conn.fill_and_submit_unit( unit,[("IDOC_INBOUND_ASYNCHRONOUS", idoc)])
print(f'IDOC Sent to SAP! and the response is : {str(result)}')
conn.close()
except Exception as e:
print("Exception block!")
print(f"An exception occurred: {str(e)}")
return ("Failed")
return ("Success!") ` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Let me say this: trying to parse or create IDocs on the RFC level, is extremely difficult. At least when trying to do it in a generic way. The code for one particular IDoc (like your IORDER01) might be doable, but still is far from "simple"... Just take a look at IDOC_INBOUND_ASYNCHRONOUS and its IDOC_DATA_REC_40 definition: it only has a 1000 CHAR field, into which the nested IDoc segment tree needs to be converted, and for this
The last point is complicated by two factors:
So if you want my advise: if you plan on processing more than 2-3 IDoc types, don't even try... If you have the chance to do this in Java, then you can use SAP's JCo IDoc Library. It does all the hard work for you! (And I'm not exaggerating when I say that around two years of net development time went into this library...) Another idea I just had, as it looks like you are receiving the IDoc data in SAP's standard IDoc-XML format: have you taken a look at SAP Business Connector yet? It can act as an HTTP(S) server, receive the IDoc-XML via HTTP POST, automatically convert it to an IDoc and send it into an SAP backend system. If this is your scenario, you could achieve it without having to code a single line. You would only need a bit of configuration like this:
(In addition to that, you also get a "monitoring component" for free, where you can monitor failed IDocs, e.g. because they had problems during HTTP or RFC communication. The opposite direction, SAP (IDoc) --> SAP BC --> HTTP receiver(IDoc-XML) is also supported out-of-the-box.) |
Beta Was this translation helpful? Give feedback.
Let me say this: trying to parse or create IDocs on the RFC level, is extremely difficult. At least when trying to do it in a generic way. The code for one particular IDoc (like your IORDER01) might be doable, but still is far from "simple"...
Just take a look at IDOC_INBOUND_ASYNCHRONOUS and its IDOC_DATA_REC_40 definition: it only has a 1000 CHAR field, into which the nested IDoc segment tree needs to be converted, and for this