Skip to Content
Technical Articles

Developing a RPA Bot to create sales order in SAP System

In this blog post I would like to share how you can create sales order in SAP System using SAP Intelligent Robotic Process Automation, aka, SAP iRPA. But,before we start building our bots, we need to get our system ready for this. Following this great blog post from Vijay Sharma I have my set up ready and running.

What we are trying to do?

Create a sales order in SAP System based on the purchase order and that purchase order will be received from mail attachment (excel) by customer. RPA Bot will follow below steps-

  • Fetch the purchase order mail from outlook and download the attachment and save it in the project folder
  • Read the mail, extract the data in some variables
  • Open the SAP GUI Application, select the system and then logon to system with credentials
  • Enter T Code in SAP Easy Access for creating sales order
  • Enter all the required data, which will need to create sales order

Let’s Begin

Now our system is ready to build bot. Open the desktop studio and follow the below steps-

Step-1 Create new project

  • Goto File > Create New Project or press “Ctrl + N

  • Fill in the mandatory details in the pop-up, I have maintained below details for my project and then click on save.

Step-2 Add Excel and Outlook library script

  • Open the script tab from context menu and right click on script area then select Include library script.

  • Check on excel and outlook integration and save it.

Step-3 Create New Application and add pages to it

  • Click on applications from context menu and then Right click on Application panel and click on Add New Application

  • Select UIAutomation in the technology and it will display list of all the Windows application running under Applications, select SAP GUI Logon as shown below and click on Save and Capture Page.

  • Capture page popup will open, select the page from the selected application. and then save and capture it.

  • Selected page will be displayed inside your application as shown below

  • Right click on some data under Capture Data section and click on “Add to Criteria”. Criteria helps your bot to identify page. As soon as you add the criteria, page will turn into Green as shown below

  • Now let’s capture a new page for logon screen, before capturing the page please make sure you open next screen on SAP GUI Application
  • To capture new right-click on your application and select Capture a New Page

  • For capturing all next pages we will choose technology as SAP GUI.
  • Likewise we will capture all the pages which are required to create a sales order. Add the criteria on the pages for the required data.

Step-3 Create Workflow

  • Select workflow under context menu. Right click on Global and click on New Workflow.

  • Enter the workflow name and click on save, I will name it as SAPLogon

Step-4 Add Activities in your Workflow

  • The first activity that you need to add in your workflow is a Custom activity so you can use the functions in the Outlook library. You can give a name and a description for this activity, so you can distinguish better its purpose. I will name mine “saveAttachments”.

  • Before you can continue with the rest of the functionalities you need to build your project. This way the script for the workflow will be generated and we can enhance it. Open the script generated by your workflow ( it will have the same name ). Search for your custom activity and step by their name. You can write the rest of the logic in this step. Update SENDER EMAIL ADDRESS, SUBJECT OF YOUR MAIL and PROJECT FOLDER PATH in the below code
GLOBAL.step({ saveAttachments: function(ev, sc, st) {
	var rootData = sc.data;
	ctx.workflow('SAPLogon', '4830c49e-2646-41aa-a683-7624c75e8c9c') ;
	// saveAttachments
	 ctx.outlook.init();
         var mails = [];
        var i, j, path, filename, attachments, noAttachments;

        // Resets the working mails list.
        ctx.outlook.mail.resetMailCollection();

        // Search the body of email for a keyword. Other criteria are also available.
        ctx.outlook.mail.searchByCriteria({
            fromEmail: "SENDER EMAIL ADDRESS",
            subject: "SUBJECT OF YOUR MAIL",
            hasAttachment: 1,
            dontThrowExceptionIfNoMailFound: true
        });

        // Get the list of mail information for the mails that fit the criteria.
        mails = ctx.outlook.mail.getFilteredTable();

        // Build the working mails list by retrieving each mail.

        if (mails.length) {
            for (i = 0; i < mails.length; i++) {
                ctx.outlook.mail.retrieveMail({
                    EntryID: mails[i]['EntryID'],
                    StoreID: mails[i]['StoreID']
                });
            }

            // Display some info about each email.
            ctx.log("---------------------------------------------------------");
            for (i = 0; i < ctx.outlook.mail.getCollectionLength(); i++) {
                ctx.log("Mail no: " + i);
                ctx.log("From: " + mails[i]['Sender']);
                ctx.log("Subject: " + ctx.outlook.mail.getSubject(i));
                ctx.log("---------------------------------------------------------");

                // Here we start the procedure of downloading the attachments in the email (if they exist).	
               // Get the number of attachments for each email.
                noAttachments = ctx.outlook.mail.getAttachmentsCount(i);
                // Get the name of the attachments.
                attachments = ctx.outlook.mail.getAttachmentsName(i);

                // Download the attachments if they exist.
                if (noAttachments > 0) {
                    // Save each attachment separately.
                    for (j = 0; j < noAttachments; j++) {
                        filename = attachments[j];
                        path = "PROJECT FOLDER PATH" + filename;
                        ctx.outlook.mail.attachmentSave(i, path, {
                            AttachmentName: filename
                        });
                        ctx.log('File downloaded: ' + filename);
                    }
                }
            }
        }

        // Ends “Microsoft Outlook” application.
        ctx.outlook.end();
	
	sc.endStep(); // Sequence
	return;
}});
  • In the context menu, right click on the folder SAPLogon760Data that will be automatically create when you capture application pages data, click on create item and then create three variables in which mail attachment values will be stored.

  • Drag and drop the sequence activity.

  • Now double click the sequence and we will add our our steps from excel library. The first step is to initialize the excel and open the sheet. Give the project folder path which you had given earlier for downloading attachment, to open the excel sheet

  • Next steps are for getting values from downloaded file. Drag and drop Get one value activity three times to getting three values. Add the variable by selecting context path. And then define the row and column for respective item. Do not worry about the code it will generate automatically when you will save it.

  • Now we want to start our SAP GUI application so for that we drag and drop Start Application under Activities to our workflow

  • Now go to Pages beside Activities, it will display all the pages you have created in your application as below

  • Drag and drop first page to the workflow and connect it to Start application

  • Double-click on your page inside the workflow to edit the Activities. You will be navigated to the page activities section as shown below. Drag and drop Click activity under Item and attach it to system which was highlighted when we created page in our application and click on Save or press Ctrl + S

  • Similarly you will have to add all the pages. Drag and drop all the activities which are required to automate the process and attach these activities to the highlighted part.

  • In this scenario you will have to use some activities like Double click, click, set and keystroke for page. Keystroke will be Enter in our case. So for this, first select the page and write key sequence as e.SAPScripting.key._Enter_

Step-5 Execute the bot

  • To execute our bot we first need to build and then run our project. Once Build is success, you will get message as below

  • Now click on Play icon or press F5 to debug the script. You will get the below messages in Desktop Debugger

  • Now right-click on Desktop Agent and click your workflow

Conclusion

This blog post should help you to understand the use of the ‘Outlook Library’, how to recursively save email attachments from selected emails, read excel sheet from specified folder, add application and capture application pages and add all these pages in the workflow.

42 Comments
You must be Logged on to comment or reply to a post.
  • Hi Kratika!

    Thanks for the blog.i have a doubt and i am doing sales order creation.

    Here i have a SAP screen where i can see only few fields ,in order to fill the other fields i need to scroll down in the same page. how do achieve this scenario ? You can find the scroll bars  in the right side and below one to scroll down and right .

    Can you help me on this scroll bar issue ?

     

     

    Thanks

    /
    • /
      • Hello Kratika,

        I am also facing same kind of issue, where I need to capture the same page twice in the workflow.

        For example, in Sales Order creation, the initial screen of VA01 is the identical with the finished screen after the Sales Order is created. The new sales order number is displayed at the bottom.

        If I want to capture that text in the bottom, I need to capture the after process screen.

        But, as it is identical with the initial screen, I am unable to assign criteria to distinguish between the pages.

        The same would happen in case of scrolling also as it would be the same page and the captured properties also would be the same.

        Could you please help to address this issue?

        • Hello Subu Mandal,

          First capture the original page, capture the subpage and then finally capture the area to be worked upon but not whole page. In capture data select different criteria like deepness.

          • Hello Kratika!,

            I tried as you suggested.

            Captured the main page:

             

            And then captured the subpage statusbar from which I need information:

             

            But even after using all the captured data, it is not setting the criteria as unique! I included deepness level as well, but it is not helping out!

            Moreover, I found I am unable to select the area where the text is written!

            When checked, I found that the same is happening with the User Area as well. Irrespective of selecting all the captured data as page criteria, it is unable to recognize the subpage.

            However, in user area, we can select the elements and add it to the subpage:

            Could you please suggest how to get it resolved?

          • Hello Subu Mandal,

            You are unable to select the area where the text is written because you had selected the technology UIAutomation at the time of capturing subpage. Select SAPGUI technology, then you can select the text.

             

          • Hello Kratika!,

            I have captured the pages using SAPGUI connector only; I think you missed the right pane of my screen shots.

            However, I am also unable to give a unique criteria to the captured subpage, even if I selected all the captured properties of that page.

          • Hello Subu Mandal,

            For your case, you do not require to capture sub page. For handling status bar message SAP Intelligent RPA has provided very good functionality, you can use this in your scenario, in my sales creation order scenario, it was working fine.

            You can follow this blog for handling status bar message . All the steps have nicely explained https://blogs.sap.com/2020/05/15/sap-intelligent-rpa-exception-handling-sapgui-connector/

            /
  • Hello Kratika,

     

    what if there is modal popup message in SAP GUI when adding item, for example, in KDMAT which does not exist?

    How to:

    1. Wait for the page to be updated (since it could be variable time).
    2. Check if there is the modal windows on screen or not.
    3. Choose different processing: if there is modal window, – press Enter on modal window.
    • Hello Dmitry Kalmykov,

      There is an activity in the Flow area called “Switches output”. With this activity you can react on different screens. From this, You can check if there is the modal windows on screen or not, you can also choose different processing: if there is modal window.

       

      /
  • Hi Kratika! ,

     

    Thank you for your post. I have a question about defining same page using sub-pages with the criteria deepness.

    • I choose the first page as main page, after fill some text field here I press F8 and pagedown I get the second page and give Deepness=0

     

    • For the second page I define subpage but not all page only GuiUserArea, choose deepness=1 but I can’t make unique the subpage.

    Could you please help me.

     

    Thank you in advance.

    Regards,

    Bengu

    • Thanks for reading the blogpost.
      In the subpage give only two criteria-
      1) Type = GuiUserArea
      2) Deepness = 1
      Remove the text criteria because it will not give uniqueness to your pages.
      • /
        • Hello Bengu Alan,

          Delete that whole page and capture the page again. Follow the below steps-

          1. Capture a new page (right now don’t give criteria to this page, keep it red, will do it later)
          2. select the item and give the criteria
          3. add the subpage and give the criteria -> type=GuiUserArea and deepness=1
          4. Now give the criteria to original page-> type=GuiMainWindow and deepness=0
          • Hello Kratika! ,

             

            Thank you 🙂 Now it is unique !!

             

            I want to learn something else about this subpage. After I defined, I click the button as you can see in the ss. But I got an error. Is this about subpage method or something else?

             

            Thank you a lot.

            Regards,

            Bengu

             

  • Hi,

    I am not able to capture the VA01 screen properly, when i try to select the order type to create a variable it is not allowing me to select the field. is this because i am using trial version of desktop studio? Please help.

     

    VA01%20Capture%20screen

    VA01 Captured screen

     

    Regards,

    Bharathiyar S.

    /
    VA01%20Capture%20screen
    • Hi Bharathiyar S

      Could you please check the technology used to capture this SAP screen?

      It should be SAPGUI.

      Also, from your screen shot, your captured page is lacking the unique criteria (that’s why it is red).

      Please set a unique criteria, and use SAPGUI as technology while capturing this page, and try again.

       

      Thanks,

      SM

  • hi Kratika!,

     

    i’m facing some issues when  using wb01 to create a plant, can you pls help to correct them? thanks

    1: you can see that i set ‘wb01’ in the workflow and the log also shows “wb01″ but it’s typed ” 0WB01″ rather than “WB01″  in system, can you pls help me to find the root cause? thanks

    2: i designed the workflow: system will popup a message box after i click ” save”, then  click “×” system will  hint ” plant xxx is created, but  when i run the workflow, system didn’t click “×”  but plant is created successfully,can you pls help me to find the root cause? thanks

  • HI,

    In my case I’m creating sales invoice (capture page for sap logon,ID password, Enter t-code and last create billing doc) but page for create billing document it is not setting the criteria as unique! I used all criteria. and because of that it is showing timeout error.

     

    /
  • Hi kratika,

     

    How can we run this bot in background ? Without any foreground sap window which we used in create sales order bot?

    I scheduled package in irpa application. But it is showing all sap screen process in foreground.

     

    Need solutions

  • Hi,

    I am trying to create the VA01, but my 2nd line-item not populated in the table control.

    I have created the switch for header data and items. It’s working fine for line item 1st

     

    Now if the 1st line runs via header brach. and 2nd line oItem.

    then I have called the item table control

    2nd line get the timeout