1 year ago

#183081

test-img

Mathew Jose

SYSTEM_FAILURE when updating PO through excel VBA

I am trying to update the date and quantity for a Purchase Order in SAP ERP (ME22N). The first line in the excel sheet gets updated, however, it does not work for the lines after that.

I get the error "SYSTEM_FAILURE" - Function call failed (RFC). Can someone help me to solve this?

Sub RunScript(currentline As Integer)
    Dim i As Integer
    Dim DocNo As String
    Dim newqty As String
    Dim MM As String
    Dim UOM As String
    Dim Plant As String
    Dim newdate As String
    Dim lineitem As String
    
    
    On Error GoTo myerr:
    'Get the document number from the sheet
    DocNo = Cells(currentline, 1).Value
    lineitem = Cells(currentline, 4).Value
    MM = Cells(currentline, 5).Value
    newqty = Cells(currentline, 6).Value
    UOM = Cells(currentline, 7).Value
    newdate = Cells(currentline, 8).Value
    Plant = Cells(currentline, 9).Value
    
    lineitem = Right("000000" & lineitem, 5)
    MM = Right("00000000000000" & MM, 18)
    
    'Setting the line status to processing
    Cells(currentline, 2).Value = 3
    
    'Logon was successful
    AddLog "Logon", "Successfully logged into SAP", vbBlack
    'Create an object to call the RFC FM
    Functions.Connection = objConnection
    
    AddLog "RFC", "Calling change for document " + DocNo, vbBlack
    'Actual BAPI is added here
    Set Func = Functions.Add("BAPI_PO_CHANGE")
    'Importing parameters are set
    Func.Exports("PURCHASEORDER").Value = DocNo
    Func.Exports("NO_MESSAGING").Value = "X"
    Func.Exports("NO_MESSAGE_REQ").Value = "X"
   
    
    Set tPOITEM = Func.Tables("POITEM")
    Set tPOITEMX = Func.Tables("POITEMX")
    tPOITEM.AppendRow
    tPOITEM.Value(1, 1) = lineitem
    tPOITEM.Value(1, 18) = newqty
    tPOITEM.Value(1, 4) = MM
    tPOITEM.Value(1, 20) = UOM
    tPOITEM.Value(1, 12) = Plant
    tPOITEMX.AppendRow
    tPOITEMX.Value(1, 1) = lineitem
    tPOITEMX.Value(1, 19) = "X"
    tPOITEMX.Value(1, 5) = MM
    tPOITEMX.Value(1, 21) = UOM
    tPOITEMX.Value(1, 13) = Plant
    
    
    Set tPOSCHEDULE = Func.Tables("POSCHEDULE")
    Set tPOSCHEDULEX = Func.Tables("POSCHEDULEX")
    tPOSCHEDULE.AppendRow
    tPOSCHEDULE.Value(1, 1) = lineitem
    tPOSCHEDULE.Value(1, 4) = newdate
    tPOSCHEDULEX.AppendRow
    tPOSCHEDULEX.Value(1, 1) = lineitem
    tPOSCHEDULEX.Value(1, 6) = "X"

    'Tables are defined to read get the messages returned
    Set tRETURN = Func.Tables("RETURN")
    
    'Executing the FM
    AddLog "RFC", "Executing order change", vbBlack
    If Func.Call = False Then
        AddLog "SAP Error", Func.Exception, vbRed
        AddLog "RFC", "Function call failed", vbRed
        Cells(currentline, 2).Value = 2
    Else
        AddLog "RFC", "Change executed", vbBlack
        DumpReturn tRETURN
        
        AddLog "RFC", "Calling commit", vbBlack
        Set Commit = Functions.Add("BAPI_TRANSACTION_COMMIT")
        'Setting importing parameters and return table
        Commit.Exports("WAIT").Value = "X"
        Set tRETURN = Commit.Tables("RETURN")
        
        If Commit.Call = False Then
            AddLog "SAP Error", Func.Exception, vbRed
            AddLog "RFC", "Commit call failed", vbRed
            Cells(currentline, 2).Value = 2
        Else
            AddLog "RFC", "Commit executed", vbBlack
            DumpReturn tRETURN
            Cells(currentline, 2).Value = 1
        End If
    End If
    Exit Sub
myerr:
    Cells(currentline, 2).Value = 2
        
End Sub

'''''''''''''''''''''''''''''''''''''''''
Sub StartScript()
Dim currentline As Integer
    Dim SilentLogon As Boolean

    Set shScript = Worksheets("Script")
    ResetLog
    
    Set LogonControl = CreateObject("SAP.LogonControl.1")
    Set Functions = CreateObject("SAP.Functions")
    Set TableFactory = CreateObject("SAP.TableFactory.1")
    Set objConnection = LogonControl.NewConnection
    SilentLogon = False
    
    'Use the below block to hardcode system connection and connect automatically
    'objConnection.Client = ""
    'objConnection.ApplicationServer = ""
    'objConnection.Language = ""
    'objConnection.User = ""
    'objConnection.Password = ""
    'objConnection.System = ""
    'objConnection.SystemID = ""
    'objConnection.SystemNumber = ""
    'objConnection.UseSAPLogonIni = False
    'SilentLogon = True
    'End of autologon block
    
    'Set the hourglass pointer
    'Application.Cursor = xlWait
    
    AddLog "Logon", "Logging into SAP...", vbBlack
    ' Logging into SAP
    If objConnection.Logon(0, SilentLogon) Then

        ' We start looking for order numbers from line 10 in the sheet
        currentline = 10
        While Cells(currentline, 1).Value <> ""
            ' Only process this line if the status is "to be processed"
            If Cells(currentline, 2).Value = 0 Then
                ' Run the actual GUI script
                RunScript currentline
            End If
            ' move to the next line
            currentline = currentline + 1
        Wend
    
        ' Update the current date and time
        Range("Timestamp").Value = Now()
    
    Else
        'Execute this block if logon fails
        Dim msg As String
        msg = "Failed to login to SAP. Verify credentials or access."
        AddLog "Logon", msg, vbRed
        MsgBox msg
    End If
    
    
Cleanup:
    'Close the connection to SAP
    objConnection.Logoff
    AddLog "Logout", "Logged out of SAP", vbBlack
    'Set the pointer back to default cursor
    Application.Cursor = xlDefault
    MsgBox "Script completed"
End Sub

Excel sheet screenshot

excel

vba

abap

saprfc

bapi

0 Answers

Your Answer

Accepted video resources