SAP - RFC返回指向数据的指针而不是数据本身

时间:2015-01-30 17:52:49

标签: sap

我正在运行RFC以从SAP获取数据。当我运行这个RFC时,似乎我得到了一个指向数据的指针,而不是数据本身。

<ET_RETURN>BAPIRET2_</ET_RETURN>

看来这个BAPIRET2_是一些数据集名称或指针。我想要这个BAPIRET2_数据集中的数据。

有没有办法获取数据本身?

编辑:RFC是:

FUNCTION z_esrv_offer_getlist. 
*"---------------------------------------------------------------------- 
*"*"Local Interface: 
*"  IMPORTING 
*"     VALUE(IV_USERID) TYPE  UNAME OPTIONAL 
*"     VALUE(IV_REQUESTID) TYPE  ZESRVREQGUID OPTIONAL 
*"     VALUE(IV_OFFER_NUM) TYPE  TKONN OPTIONAL 
*"     VALUE(IV_STATUS_CHANGE_FROM_DATETIME) TYPE  ZDATETIME OPTIONAL 
*"     VALUE(IV_STATUS_CHANGE_TO_DATETIME) TYPE  ZDATETIME OPTIONAL 
*"     VALUE(IV_PURCH_SALES) TYPE  ZPUR_SALE OPTIONAL 
*"     VALUE(IV_TYPE) TYPE  BSARK OPTIONAL 
*"     VALUE(IV_IDOC) TYPE  XFELD DEFAULT SPACE 
*"     VALUE(IT_PLANTS) TYPE  CCVX_WERKS_TAB OPTIONAL 
*"     VALUE(IT_PARTNERS) TYPE  ZESRVBPLIST_TAB OPTIONAL 
*"     VALUE(IT_OFFER_STATUS_CODES) TYPE  ZESRV_BTBSTA_TAB OPTIONAL 
*"  CHANGING 
*"     VALUE(ET_OFFERS) TYPE  ZESRVOFFER_TAB 
*"     VALUE(ET_CHANGE_HIST) TYPE  ZESRVOFFER_HIST_TAB 
*"     VALUE(ET_RETURN) TYPE  BAPIRET2_TAB 
*"---------------------------------------------------------------------- 

  DATA: l_error     TYPE symsgv, 
        lv_return   TYPE bapiret2. 

  FIELD-SYMBOLS: <error>     TYPE bapiret2. 

* Initialize. 
  PERFORM initialize_setup. 
  PERFORM initialize_for_getlist. 
  gv_userid = iv_userid. 
  gv_req_id = iv_requestid. 

  PERFORM get_function_name CHANGING gv_funcname. 


* Unload selection criteria into global variables 

  gv_offer = iv_offer_num. 
  gv_frm_chngdtm = iv_status_change_from_datetime. 
  gv_to_chngdtm = iv_status_change_to_datetime. 
  gv_ps = iv_purch_sales. 
  gv_type = iv_type. "(F)irm / (S)oft 
  gv_idoc = iv_idoc. "X if for idoc creation 

* partners 
  IF it_partners[] IS NOT INITIAL. 
    r_partners-sign = 'I'. 
    r_partners-option = 'EQ'. 
    CLEAR r_partners-high. 
    LOOP AT it_partners INTO gs_partners. 
      APPEND gs_partners TO gt_logprtnrs. 
      r_partners-low = gs_partners-bu_partner. 
      APPEND r_partners TO gt_partner_rng. 
    ENDLOOP. 
  ENDIF. 
* plants 
  IF it_plants[] IS NOT INITIAL. 
    r_plants-sign = 'I'. 
    r_plants-option = 'EQ'. 
    CLEAR r_plants-high. 
    LOOP AT it_plants INTO gn_plant. 
      r_plants-low = gn_plant-werks. 
      APPEND r_plants TO gt_plant_rng. 
      APPEND gn_plant TO gt_plants. 
    ENDLOOP. 
  ENDIF. 
* status codes 
  IF it_offer_status_codes[] IS NOT INITIAL. 
    r_status-sign = 'I'. 
    r_status-option = 'EQ'. 
    CLEAR r_status-high. 
    LOOP AT it_offer_status_codes INTO gn_status. 
      r_status-low = gn_status-btbsta. 
      APPEND r_status TO gt_status_rng. 
      APPEND gn_status TO gt_status_codes. 
    ENDLOOP. 
  ENDIF. 

* make sure there is at least one criteria filled 
  IF iv_offer_num IS INITIAL AND 
     iv_status_change_from_datetime IS INITIAL AND 
     iv_status_change_to_datetime IS INITIAL AND 
     iv_type IS INITIAL AND 
     it_plants[] IS INITIAL AND 
     it_partners[] IS INITIAL AND 
     it_offer_status_codes[] IS INITIAL. 

    PERFORM get_message_for_bapi USING gc_warning gc_esrv '057' 
                               space space space space 
                                CHANGING lv_return. 
    APPEND lv_return TO et_return. 
    APPEND lv_return TO gt_error. 
*    IF iv_userid IS INITIAL AND iv_requestid = 0. 
*    ELSE. 
    PERFORM bapi_logging USING gv_funcname. 
*    ENDIF. 
    EXIT. 
  ENDIF. 

* at least one selection criteria has been entered, so validate selections 
  PERFORM validate_selections. 
  READ TABLE gt_error INTO lv_return WITH KEY type = 'W'. 
  IF sy-subrc = 0. 
*     errors found in selection criteria, stop 
    et_return[] = gt_error[]. 
    EXIT. 
  ELSE. 
*     no errors found, select data 
    PERFORM get_offers. 
    IF gt_offers[] IS NOT INITIAL. 
      PERFORM get_history. 
      et_change_hist[] = gt_chng_hist[]. 
      et_offers[] = gt_offers[]. 
    ENDIF. 
    DESCRIBE TABLE et_offers LINES gv_recordsfound. 
    IF gv_recordsfound = 0. 
*     no offers found 
      PERFORM get_message_for_bapi USING gc_warning gc_esrv '061' 
                                 space space space space 
                                  CHANGING lv_return. 
      APPEND lv_return TO et_return. 
      APPEND lv_return TO gt_error. 
*      IF iv_userid IS INITIAL AND iv_requestid = 0. 
*      ELSE. 
      PERFORM bapi_logging USING gv_funcname. 
*      ENDIF. 
      EXIT. 
    ELSE. 
      READ TABLE et_return INTO lv_return WITH KEY type = 'W'. 
      IF sy-subrc <> 0. 
*       success, history returned 
        PERFORM get_message_for_bapi USING gc_success gc_esrv '002' 
                                   space space space space 
                                    CHANGING lv_return. 
        APPEND lv_return TO gt_error. 
        APPEND lv_return TO et_return. 
*        IF iv_userid IS INITIAL AND iv_requestid = 0. 
*        ELSE. 
        PERFORM bapi_logging USING gv_funcname. 
*        ENDIF. 
      ENDIF. 
    ENDIF. 
  ENDIF. 

ENDFUNCTION. 

1 个答案:

答案 0 :(得分:1)

SAP JCo将使用自己的对象类型返回您的数据。您必须浏览这些对象才能获取数据。下面是一个Java方法的示例,该方法连接到SAP以便使用SAP JCo 3获取发票详细信息。您应该注意两种返回类型:

1)&#34;返回&#34;是一种结构类型。在java中,这有点像java中的POJO。或者您可以将其视为单个数据记录。 Google&#34; SAP结构&#34;了解更多详情。

2)&#34; INVOICE_SUMMARIES&#34;是一种表格类型。注意正在读表的for循环。

根据RFC的导出参数是表(它们看起来是什么)还是结构,您需要调整检索数据的方式。希望这个例子足以让你开始。

private List<InvoiceSummary> getInvoices( String userId, String customerNumber, String invoiceNumber ) throws InvoiceRetrievalException, JCoException {

ArrayList<InvoiceSummary> invoices = new ArrayList<InvoiceSummary>();
try {
    //Verify SAP connection.
    DAOFactory.registerDestination();
    JCoDestination dest = JCoDestinationManager.getDestination( DAOFactory.getDestinationName() );
    JCoRepository repository = dest.getRepository();

    JCoFunctionTemplate ft = repository.getFunctionTemplate( "Z_BAPI_GET_INVOICES" );
    JCoFunction jFunction = ft.getFunction();

    //Set parameters.
    jFunction.getImportParameterList().setValue( "USER_ID", userId );
    jFunction.getImportParameterList().setValue( "CUSTOMER_NUMBER", customerNumber );
    jFunction.getImportParameterList().setValue( "INVOICE_NUMBER", invoiceNumber );

    //Execute the function and read the results.
    jFunction.execute( dest );

    //Check the SAP return object for errors.
    JCoStructure out = jFunction.getExportParameterList().getStructure( "RETURN" );
    if ( "E".equals( out.getString( "TYPE" ) ) ) {
        throw new InvoiceRetrievalException( out.getString( "MESSAGE" ) );
    }

    //Parse the returned invoices into InvoiceSummary objects.
    JCoTable invoiceTable = jFunction.getTableParameterList().getTable( "INVOICE_SUMMARIES" );
    for ( int i = 0; i < invoiceTable.getNumRows(); i++ ) {
        InvoiceSummary invoice = new InvoiceSummary();
        invoiceTable.setRow( i );

        invoice.setInvoiceNumber( invoiceTable.getString( "INVOICE_NUMBER" ) );
        invoice.setReleaseNumber( invoiceTable.getString( "RELEASE_NUMBER" ) );
        invoice.setItemNumber( invoiceTable.getString( "ITEM_NUMBER" ) );
        invoice.setInvoiceDate( invoiceTable.getString( "INVOICE_DATE" ) );
        invoice.setProductDescription( invoiceTable.getString( "DESCRIPTION" ) );
        invoice.setDueDate( invoiceTable.getString( "DUE_DATE" ) );
        invoice.setCustomerNumber( invoiceTable.getString( "CUSTOMER_NUMBER" ) );

        invoices.add( invoice );
    }
} catch ( InvoiceRetrievalException e ) {
    logger.error( "An error occurred while attempting to retrieve an invoice."
            + " userId = " + userId
            + " invoiceNumber = " + invoiceNumber, e );
    throw e;
} catch ( JCoException e ) {
    logger.error( "An error occurred while calling Z_BAPI_GET_INVOICES."
                + " userId = " + userId
                + " invoiceNumber = " + invoiceNumber, e );
        throw e;
}
return invoices;
}