C# 傳入 IRfcTable 呼叫 SAP RFC

接續上一篇文章

https://robertlan.com/programming/sap-net-connector-3-0%E9%80%A3%E6%8E%A5rfc/

所提到的調用SAP RFC的方法

可以得知,呼叫 RFC 和一般寫程式的概念大同小異, 但如果今天必須傳入多筆資料,就必須透過 IRfcTable 當作輸入的參數傳入 SAP RFC 中。

要傳入 IRfcTable 到 SAP RFC 中,有分為以下幾個步驟: 

1.初始 SAP 連接介面 
2.設定 SAP 連接參數 
3.初始 SAP RFC 來源 
4.建立 IRfcTable 結構 
5.設定 IRfcTalbe 參數 
6.呼叫 SAP RFC 返回值 
7.取得 SAP RFC 返回值 

using Newtonsoft.Json;
using SAP.Middleware.Connector;
using SapRFCService.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SapRFCService.Models
{
    public class Products
    {       
        public static string GetAllProductsByID(string ProductsList)
        {
            string[] ProductArray = ProductsList.Split(',');
            // 1.初始 SAP 連接介面
            IRfcTable IRetTable = null;
            List<Z_MM_QUBE_MATNR> Z_MM_QUBE_MATNR_List = new List<Z_MM_QUBE_MATNR>();
            //2.設定 SAP 連接參數
            RfcConfigParameters parms = RFC.GetSettingParms();
            RfcDestination rfcDest = RfcDestinationManager.GetDestination(parms);

            //3.初始 SAP RFC 來源
            RfcRepository rfcRep = rfcDest.Repository;
            IRfcFunction IReader = rfcRep.CreateFunction("Z_MM_QUBE_MATNR");

            //4.建立 IRfcTable 結構
            IReader.Invoke(rfcDest);
            IRfcTable Itab = IReader.GetTable("IT_MATNR");
           
            //5.設定 IRfcTalbe 參數
            for(int i = 0; i < ProductArray.Length; i++)
            {
                Itab.Append();
                Itab[i].SetValue("MATNR", ProductArray[i].ToString());
            }
            
            //6.呼叫 SAP RFC 返回值
            IReader.SetValue("IT_MATNR", Itab);
            IReader.Invoke(rfcDest);

            //7.取得 SAP RFC 返回值
            IRetTable = IReader.GetTable("ET_MATNR");
            Z_MM_QUBE_MATNR_List = IRetTable.AsQueryable().Select(x => new Z_MM_QUBE_MATNR
            {
                MATNR = x.GetString("MATNR") ?? "",
                MAKTX = x.GetString("MAKTX") ?? "",
                ZZCT2 = x.GetString("ZZCT2") ?? "",
                LIFNR = x.GetString("LIFNR") ?? "",
                ZZMATU = x.GetString("ZZMATU") ?? "",
                ZFVOLUME = x.GetString("ZFVOLUME") ?? "",
                ZZPAGE = x.GetString("ZZPAGE") ?? "",
                WGBEZ = x.GetString("WGBEZ") ?? "",
                ZFPRESERVE_D = x.GetString("ZFPRESERVE_D") ?? "",
                ZZSE = x.GetString("ZZSE") ?? "",
                NETPR = x.GetString("NETPR") ?? "",
                WAERS = x.GetString("WAERS") ?? "",
                DMBTR = x.GetString("DMBTR") ?? "",
                ERDAT = x.GetString("ERDAT") ?? "",
                UDATE = x.GetString("UDATE") ?? ""
            }
             ).ToList();

            AddTempTable(Z_MM_QUBE_MATNR_List);

            string result = JsonConvert.SerializeObject(Z_MM_QUBE_MATNR_List);
            return result;
        }
       
    public class Z_MM_QUBE_MATNR
    {
        public string MATNR = "";
        public string MAKTX = "";
        public string ZZCT2 = "";
        public string LIFNR = "";
        public string ZZMATU = "";
        public string ZFVOLUME = "";
        public string ZZPAGE = "";
        public string WGBEZ = "";
        public string ZFPRESERVE_D = "";
        public string ZZSE = "";
        public string NETPR = "";
        public string WAERS = "";
        public string DMBTR = "";
        public string ERDAT = "";
        public string UDATE = "";

    }
}

執行結果