Technical Articles
Developing a RPA Bot to create sales order in SAP System
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.
Very Nice Kratika! Thanks for sharing.
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 Avinash Y
For scrolling, you have to add a subpage then capture the application page after scrolling down.
Hello Avinash Y,
Is your issue resolved?
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/
Kratika! , yes the issue got resolved
Thanks
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:
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.
Could you please help me.
Thank you in advance.
Regards,
Bengu
Hello Kratika! ,
Thank you for your quick reply. I tried as you mentioned but it is not working. Here is the screenshot.
Best Regards,
Bengu
Hello Bengu Alan,
Delete that whole page and capture the page again. Follow the below steps-
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 Captured screen
Regards,
Bharathiyar S.
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
updated pic




hi Kratika!
above issues are resolved after i rebuild the workflow【add workflow logong】 and can executed successfully , but i have another issue to extract plant number。you can see that i used GUI status bar but it doesn't work, can you pls tell me how do it? thanks
Hello Rongxian Lin,
You have to make variables in context for saving message id, message number and message line which you are getting from SAP GUI Status bar functionalities. And in the where to store result property , give the path of context that you have created.
hi Kratika!
i updated “SENDER EMAIL ADDRESS, SUBJECT OF YOUR MAIL and PROJECT FOLDER PATH in the step: saveAttachments, but
it doesn't work, the log only shows ctx.outlook.end, can you pls help me to correct it? thanks
update pic

Hello Rongxian Lin,,
Is your attachment is saving in the folder path that you have provided?
Can you please share the custom code of saveAttachment, so that i can find out the error.
hi Kratika! ,
above issue is resolved, i changed the from mail address to other people【 seems that from mail can't be myself】and added %% on subject【even though my mail subject is plant, i have to add %%, don't know why】。
i have another question:when i changed the workflow and try to rebuild, studio will hint :merge tool can't be set , i check the workflow setting and KDiff3 has been selected, can you pls help to correct it? thanks
Hello Rongxian Lin,,
From your uploaded picture i can see that no merge tool has been selected. You have to download kdiff3 merge tool and give the path of exe file in merge tool area.
i have downloaded kdiff3 and selected, but it doesn't work. you can see that i added set wb01 and enter in the workflow but not generated .js code after i clicked rebuild。。
Hello Rongxian Lin,,
Did you installed the tool in your machine. For installing please follow this-
https://help.sap.com/viewer/6b9c8e86a0be43539b670de962834562/Cloud/en-US/156a3232aa834ac1bdd9d5e8ad29aae5.html
And then follow this-
https://help.sap.com/viewer/6b9c8e86a0be43539b670de962834562/Cloud/en-US/5c630b4233f5481f8ef316914badb684.html
After this restart your desktop studio.
hi Kratika! ,
ikdiff3 is installed on my computer and i followed above steps but still can't rebuild, so i have to create a new project and this issue is resolved tempoary. but this issue is always happens, i don't know why. now i have another question about subpage
after i create a organization in the page1, then i need to click change button to change BP role, so i tried to add a subpage on the page1 but failed, desktop studio has a hint:An unexpected condition occurred on the server (exception from HRESULT:0x80010105(RPC_E_SERVERFAULT)), can you pls tell me to how capture the subpage? thanks
hi Kratika!,
the last page is red even though the criteria is unique, can you pls help me how to set criteria? thanks
Hello Rongxian Lin,,
Don't choose criteria type=GuiMainWindow in any page because most of the pages are of type GUIMainWindow. Choose criteria like text and deepness.
hi Kratika!
you can see that all pages are green, but when i run this project, log shows timeout in the second page, can you pls help me to correct it? thanks
Hello Former Member
Please provide screen shots of how you declare the first and the second page. If you run the debugger click pages to see if the pages are well recognized. Provide also a screenshot of this.
second page:
first page:
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