How to Execute an UiPath Robot from SAP eCATT

sap

#1

In different posts here I presented a few examples how to use SAP in the context of UiPath. In this post I will describe the way visa-versa. That means this example shows how to use an UiPath bot from SAP eCATT. The communication between the bot and SAP was realized trough memory mapped files (MMF), a very handy way.

We start with the UiPath flowchart. Very easy, three activities. In the first step we read via invoke code the transfered data from SAP from the MMF. In the second step we open notepad and in the third step we write the data into notepad.

UiPath_FlowChart

The invoke code step is also very easy. It reads the data from the MMF and sets the variable Text with this content.

vb

'-Begin-----------------------------------------------------------------

Dim MMF As System.IO.MemoryMappedFiles.MemoryMappedFile
Dim Stream As System.IO.Stream
Dim StreamReader As System.IO.StreamReader

MMF = System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting("UiPathRobot")
Stream = MMF.CreateViewStream
StreamReader = New System.IO.StreamReader(Stream)
Text = StreamReader.ReadToEnd().Replace(Chr(0), "")
StreamReader.Dispose
Stream.Dispose

'-End-------------------------------------------------------------------

To use this process inside SAP eCATT is it necessary to deploy them. After that we convert the NuGet package into an ABAP function module (FM), to be able to use it inside SAP. That means the NuGet package was embedded into an ABAP FM and with the execution of the FM the package is provided on the frontend server. You can use one of my tools BinFile2ABAP to do that.

All non SAP preparations a done now. In the next steps we create different development objects in the SAP environement.

DevelopmentObjects

Let us start with the PowerShell script to execute the UiPath bot. It is stored as include development object inside SAP. It creates an MMF, writes some data in it, executes the UiPath bot and destroys the MMF at the end.

#-Begin-----------------------------------------------------------------

#-Sub Main--------------------------------------------------------------
Function Main {

  #Set $Text
  If ([String]::IsNullOrEmpty($Text)) {
    $Text = "Hello World from SAP";
  }

  #Set $FileName
  If ([String]::IsNullOrEmpty($FileName)) {
    Return;
  }

  [System.IO.MemoryMappedFiles.MemoryMappedFile]$MMF = `
    [System.IO.MemoryMappedFiles.MemoryMappedFile]::CreateNew("UiPathRobot", 4MB);
  If($Null -eq $MMF) {
    Return;
  }

  $Stream = $MMF.CreateViewStream();
  $StreamWriter = [System.IO.StreamWriter]::new($Stream);
  $StreamWriter.Write($Text);
  $StreamWriter.Dispose();
  $Stream.Dispose();

  & "C:\Program Files (x86)\UiPath\app-19.1.0\UiRobot.exe" -file "$($FileName)";

  $MMF.Dispose();

}

#-Main------------------------------------------------------------------
Main;

#-End-------------------------------------------------------------------

Now comes the wrapper class for Sapien PowerShell Library, you can find all information about this approach in the SAP Community. Okay, now we implement the class to execute the UiPath bot. It contains only one method, execute_bot. In this method we load the PowerShell script, set the parameters and executes it. Last but not least we get the returning information from the script.

"-Begin-----------------------------------------------------------------
CLASS z_cl_posh_test_rpa DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    METHODS execute_bot
      IMPORTING
        VALUE(iv_filename) TYPE string
        VALUE(iv_text) TYPE string OPTIONAL
      RETURNING
        VALUE(rv_result) TYPE string.

  PROTECTED SECTION.

  PRIVATE SECTION.

ENDCLASS.


CLASS z_cl_posh_test_rpa IMPLEMENTATION.

  METHOD execute_bot."--------------------------------------------------

    DATA:
      lo_ps       TYPE REF TO z_cl_activexposhv3,
      lv_result   TYPE string,
      lv_pscode   TYPE string,
      lv_temp     TYPE string,
      lv_filename TYPE string
      .

    cl_gui_frontend_services=>get_sapgui_workdir(
      CHANGING
        sapworkdir = lv_filename
    ).
    lv_filename = lv_filename && '\' && iv_filename.

    CREATE OBJECT lo_ps.

    CHECK lo_ps->loadlib( ) = z_cl_activexposhv3=>mc_true.
    CHECK lo_ps->getispowershell_installed( ) = z_cl_activexposhv3=>mc_true.
    CHECK lo_ps->init( iv_load_profiles = z_cl_activexposhv3=>mc_false ) = 0.

    lo_ps->setoutputmode( lo_ps->mc_outputbuffer ).
    lo_ps->setoutputwidth( 132 ).
    lo_ps->clearoutput( ).

    lv_pscode = lo_ps->readinclasstring('Z_POSH_TEST_RPA_EXEBOT').

    CHECK iv_filename IS NOT INITIAL.
    lv_temp = '$FileName = "' && lv_filename && '";'.
    REPLACE '#Set $FileName' WITH lv_temp INTO lv_pscode.

    IF iv_text IS NOT INITIAL.
      lv_temp = '$Text = "' && iv_text && '";'.
      REPLACE '#Set $Text' WITH lv_temp INTO lv_pscode.
    ENDIF.

    lo_ps->execute( lv_pscode ).
    rv_result = lo_ps->getoutputstring( ).

    lo_ps->freelib( ).

  ENDMETHOD.

ENDCLASS.

"-End-------------------------------------------------------------------

Don’t forget to create the FM, which contains the NuGet package :wink:

Okay now we switch from the ABAP Development Workbench to the eCATT environment. Here we create a new script with the following interface…

…and the following code.

IV_TEXT = IP_TEXT.

ABAP.

  DATA:
    lo_rpa TYPE REF TO z_cl_posh_test_rpa.

  CALL FUNCTION 'Z_RPA_BLANKPROCESS103'.

  CREATE OBJECT lo_rpa.

  lv_success = lo_rpa->execute_bot(
    iv_filename = 'BlankProcess.1.0.2.nupkg'
    iv_text = iv_text
  ).

ENDABAP.

EP_SUCCESS = LV_SUCCESS.

Puh, that is all :slightly_smiling_face:

As you can see contains this integration scenario different languages and platforms. Many steps are necessary, but in the end we will be rewarded with a good result. You can find a video of the execution of an UiPath bot from SAP eCATT here.

Conclusion

My business use case for this example is the integration of UiPath processes inside SAP test automation. On this way is it possible to automate steps outside SAP, which normally can not be reached otherwise, from the test processes inside SAP. We all know that it is possible to use the reversed way, UiPath automates steps inside and outside SAP. But if you already have consolidated test processes in eCATT, changing the perspective can be very profitable. And, as we can see, it is possible.