C# SAP .NET Connector调用BAPI返回数据与SAP GUI不一致
SAP RFC调用BAPI返回结果与SAP GUI不一致问题
场景说明
本人参考站点 https://www.veonconsulting.com/integrating-sap-using-nco/ 给出的开发指引完成代码编写,目前需调用BAPI_PRODORD_GET_DETAIL函数获取生产订单详情数据。
已在SAP GUI环境下测试传入参数:<bapi_prodord_get_detail><number>1000262</number><struct><order_objects><header>X</header><operations>X</operations><components>X</components></order_objects></struct></bapi_prodord_get_detail>
可正常获取订单抬头、组件、工序三类完整数据,但通过现有开发代码执行调用时,未得到与SAP GUI一致的返回结果,希望协助排查定位问题。
核心实现代码
public bool TestRfcReadTableConnection() { bool state = false; string rfcRequest = "<RFC_READ_TABLE><QUERY_TABLE>MARD</QUERY_TABLE><DELIMITER>*" + "</DELIMITER><ROWSKIPS>0</ROWSKIPS><ROWCOUNT>0</ROWCOUNT><TABLE><OPTIONS><ROW>" + "<TEXT>MATNR IN (</TEXT></ROW><ROW><TEXT>'testConnection'</TEXT></ROW><ROW>" + "<TEXT>)</TEXT></ROW></OPTIONS></TABLE></RFC_READ_TABLE>"; Utils.RfcClient client = new Utils.RfcClient(); try { XElement response = client.PullRequestToSAPrfc(rfcRequest); state = true; } catch (RfcLogonException ex) { Console.Write("Logon Failed"); } catch (RfcInvalidStateException ex) { Console.Write("RFC Failed"); } catch (RfcBaseException ex) { Console.WriteLine("communication error" + ex.Message); } catch (Exception ex) { Console.Write("Connection error"); } finally { //client.disconnectDestination(); } return state; } public bool TestProdOrderBapiConnection() { bool state = false; string rfcRequest = "<bapi_prodord_get_detail><number>1000262</number><struct>" + "<order_objects><header>X</header><operations>X</operations><components>X" + "</components></order_objects></struct></bapi_prodord_get_detail>"; Utils.RfcClient client = new Utils.RfcClient(); try { XElement response = client.PullRequestToSAPrfc(rfcRequest); state = true; } catch (RfcLogonException ex) { Console.Write("Logon Failed"); } catch (RfcInvalidStateException ex) { Console.Write("RFC Failed"); } catch (RfcBaseException ex) { Console.WriteLine("communication error" + ex.Message); } catch (Exception ex) { Console.Write("Connection error"); } finally { //client.disconnectDestination(); } return state; } public XElement PullRequestToSAPrfc(string XMLRequest) { IRfcFunction requestFn; requestFn = PrepareRfcFunctionFromXML(XElement.Parse(XMLRequest)); RfcSessionManager.BeginContext(_ECCsystem); requestFn.Invoke(_ECCsystem); RfcSessionManager.EndContext(_ECCsystem); XElement XMLResponse = PrepareXMLFromrfc(requestFn); return XMLResponse; } public IRfcFunction PrepareRfcFunctionFromXML(XElement xmlFunction) { RfcRepository repo = _ECCsystem.Repository; IRfcFunction RfcFunction = repo.CreateFunction(xmlFunction.Name.ToString()); foreach (XElement xelement in xmlFunction.Elements()) { if (xelement.Name.ToString().Equals("TABLE")) { if (NotProcessSpecialTable(xelement)) continue; IRfcTable options = RfcFunction.GetTable(xelement.Descendants().First().Name.ToString()); foreach (XElement row in xelement.Elements().First().Elements()) { options.Append(); foreach (XElement rowElement in row.Elements()) { string elementName = rowElement.Name.ToString(); RfcElementMetadata elementMeta = options.GetElementMetadata(elementName); var elementValue = getValueAsMetadata(ref elementMeta, rowElement.Value); if (elementValue is string && string.IsNullOrEmpty((string)elementValue)) { continue; } options.SetValue(elementName, elementValue); } } } else if (xelement.Name.ToString().Equals("STRUCT")) { IRfcStructure options = RfcFunction.GetStructure(xelement.Descendants().First().Name.ToString()); foreach (XElement structElement in xelement.Elements().First().Elements()) { string elementName = structElement.Name.ToString(); RfcElementMetadata elementMeta = options.GetElementMetadata(elementName); var elementValue = getValueAsMetadata(ref elementMeta, structElement.Value); if (elementValue is string && string.IsNullOrEmpty((string)elementValue)) { continue; } options.SetValue(elementName, elementValue); } } else { string elementName = xelement.Name.ToString(); RfcElementMetadata elementMeta = RfcFunction.GetElementMetadata(elementName); var elementValue = getValueAsMetadata(ref elementMeta, xelement.Value); if (elementValue is string && string.IsNullOrEmpty((string)elementValue)) { continue; } RfcFunction.SetValue(elementName, elementValue); } } return RfcFunction; } public XElement PrepareXMLFromrfc(IRfcFunction rfcFunction) { var XMLRoot = new XElement(rfcFunction.Metadata.Name); for (int functionIndex = 0; functionIndex < rfcFunction.ElementCount; functionIndex++) { var functionMatadata = rfcFunction.GetElementMetadata(functionIndex); if (functionMatadata.DataType == RfcDataType.TABLE) { var rfcTable = rfcFunction.GetTable(functionMatadata.Name); var XMLTable = new XElement(functionMatadata.Name); foreach (IRfcStructure rfcStracture in rfcTable) { XElement XMLRow = new XElement("ROW"); for (int i = 0; i < rfcStracture.ElementCount; i++) { RfcElementMetadata rfcElementMetadata = rfcStracture.GetElementMetadata(i); if (rfcElementMetadata.DataType == RfcDataType.BCD) { XMLRow.Add(new XElement(rfcElementMetadata.Name, rfcStracture.GetString(rfcElementMetadata.Name))); } else { XMLRow.Add(new XElement(rfcElementMetadata.Name, rfcStracture.GetString(rfcElementMetadata.Name))); } } XMLTable.Add(XMLRow); } XMLRoot.Add(XMLTable); } else if (functionMatadata.DataType == RfcDataType.STRUCTURE) { var rfcStructure = rfcFunction.GetStructure(functionMatadata.Name); XElement XMLRow = new XElement(functionMatadata.Name); for (int elementIndex = 0; elementIndex < rfcStructure.ElementCount; elementIndex++) { RfcElementMetadata eleMeta = rfcStructure.GetElementMetadata(elementIndex); XMLRow.Add(new XElement(eleMeta.Name, rfcStructure.GetString(eleMeta.Name))); } XMLRoot.Add(XMLRow); } else { RfcElementMetadata rfcElement = rfcFunction.GetElementMetadata(functionIndex); XMLRoot.Add(new XElement(rfcElement.Name, rfcFunction.GetString(rfcElement.Name))); } } return XMLRoot; } // 数据类型转换处理函数 private object getValueAsMetadata(ref RfcElementMetadata elementMeta, string value) { switch (elementMeta.DataType) { case RfcDataType.BCD: return value; case RfcDataType.NUM: if (value.Contains(".")) { int elementValue; int.TryParse(value, out elementValue); return elementValue; } else { return Convert.ToInt32(value); } case RfcDataType.INT1: return Convert.ToInt32(value); case RfcDataType.INT2: return Convert.ToInt32(value); case RfcDataType.INT4: return Convert.ToInt32(value); case RfcDataType.INT8: return Convert.ToInt64(value); case RfcDataType.CHAR: return value; case RfcDataType.DATE: return DateTime.ParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture); default: return string.Empty; } }
内容的提问来源于stack exchange,提问作者Mysterious288
相关产品推荐
相关产品推荐

