0

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

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • 3
    If you want to try and solve that, you can put a remote breakpoint on the SAP side and if there's a system failure, that can often be caused by a short dump, are you familiar with transaction ST22? What may also help us understand a bit better is to provide us a view of your spreadsheet. It also looks like you're adding the BAPI multiple times, from memory, if you're going to do that, you should remove it first but clearing the structures may be a better option than adding it each time. It's been yeeeeeeeeeears since I used that stuff but it rings bells. – Skin Feb 15 '22 at 21:27
  • Hello Skin, thank you for looking at it. I am not very familiar SAP nor VBA, this was a code I found online and managed to edit a little based on information I found on google. Would you know how I can add a remote breakpoint on the SAP side and also clearing the structure? Thank you – Mathew Jose Feb 16 '22 at 06:41
  • 3
    Please log into your SAP ERP system, go to `ST22`, download the short dump which occurred at the time of your `SYSTEM_FAILURE` and attach it here. Thanks. – Sandra Rossi Feb 16 '22 at 07:12
  • Sandra / Skin post your comment as answer. It is the solution ;) The problem explanation is in either st22 or sm21.... – phil soady Feb 22 '22 at 11:21
  • 1
    @philsoady, fancy seeing you on Stackoverflow. We’re old colleagues at SAP. – Skin Feb 27 '22 at 10:16
  • @MathewJose, sorry I never got back to you but it’s very hard to help without doing over Teams/Zoom or something. Trying to help though, in SAP, transaction SE37 will get you to the code behind the function module, from there, Google “SAP function module remote debug breakpoint”, that will help you. Regarding clearing the structures, there’s a method for the tables which is either “Clear” or “RemoveAll” or something. Like I said, it’s been years. – Skin Feb 27 '22 at 10:17

0 Answers0