Skip to Content
Technical Articles

Working with SAP IRPA and S/4 On-Premise System

My Struggling days:

Few Months back, I have completed couple of Open SAP Course in SAP IRPA and wanted to try out myself. Tenant registration in SAP Agent is successful for the first time. I have downloaded SAP On-premise packages from SAP Store. All the time jobs wer successful, but records failed. [ I have tried to run these packages few times and every time same result – Fail ]

Few days later, I do not know what happened suddenly my account with “Europe Rot” region got stopped working. So, I have created another account and with this account I couldn’t establish tenant registration with Desktop Agent. It took more than 5 weeks to find a solution on this.

After much search in SAP SCN and research I got my issue resolved.  https://answers.sap.com/questions/13075127/authentication-access-is-not-permitted-desktop-age.html?childToView=13084898#answer-13084898

Now, My Desktop Agent tenant registration issue got resolved. But still my Package which I have downloaded from SAP Store for SAP On-Premise system still fails when executed from Bot. When I have searched in SAP SCN on this. I got to know that, one of reasons for failure is SAP Package and SAP Agent Version mismatch.

So, I have downloaded SAP IRPA Package [ Maintain Planned Independent Requirements (4FL) ] onto my Desktop. Import the same package in Desktop Studio and click on export. Now, I have used this Exported package  by uploading back to IRPA Cloud Foundry.

 

Arriving at Major Error:

Even with this Exported package all my records are failing to post entries into S/4 HANA. So, I have tried to debug from SAP and found an error while Ajax Post or Read call is made.

Error: status: 12175 – A security error occurred

With this error status I tired searching SAP SCN again then I have found a clue fro below link and SPA Note

https://answers.sap.com/questions/13036165/odata-service-is-showing-error-a-security-error-is.html

2826745 – How to call a web service when ctx.ajax fails ?

 

Answer – We need use CURL instead of Ajax Calls.

 

How to Use Curl?

Download Curl from the link https://curl.haxx.se/windows/

Unzip the file. We don’t need to install any file here. We just need to extract folder and place it somewhere C:\ or C:\Program Files or C:\ProgramData or C:\Program Files (x86).

I have placed the extracted folder in C:\Program Files

Make sure, Bin folder has curl.exe file

Now, set curl folder in the environmental variables / path

[ Path where curl.exe file is available ]

Now, Open Windows command prompt and try curl –version, it should its version. Now, we have Curl available from windows command prompt.

 

Lets’ go to Desktop Studio.

In the Get/Head method replace ajax call with Curl Call

Prepare a Curl Command for Get Call.

Generally it will be like – curl url  =>

curl https://google.com 
curl https://www.sap.com/index.html

To pass down user credentials we can use multiple ways, one of the ways is mentioning parameter -u and passing user and password like

curl -u user:password url

To store header response, we can use -D and mention file name

curl -D c:\\headerfile.txt url

Here headerfile.txt doesn’t need to exist, it will be created automatically, and header response parameters are stored when the curl command is processed

To store cookies, we can use -c and mention file name

curl  -b c:\\cookiefile.txt url

Here cookiefile.txt doesn’t need to exist, it will be created automatically, and cookies are stored when the curl command is processed

-v or/and -i are passed to curl statement to get full response -v for verbose ; -i for include protocol header response in the output; -H is used to pass headers

 

1) Finally, our curl command will look like

var cmd1 = 'curl -v -i -D C:\\ProgramData\\SAP\\headers.txt -c C:\\ProgramData\\SAP\\cookie.txt ' +
           '-H "Content-Type:application/json" -H "Accept: application/json" -H "x-csrf-token: fetch" '+ 
           '-u ' + params.credentials.username+":"+params.credentials.password +' ' +params.headUrl; 

Use ct.exec command to pass curl command

try {
	ctx.exec(cmd1, 30000, function(obj){
	    ctx.log(obj);	
	    //response.xcsrfToken = 'xcrsftoken';
	    //response.cookie = 'cookie';
	    success(response); // Copy pasted from Ajax call
	});
	}
	catch (ex){
          ctx.log('error : ' + ex.message);
	  response = ex;
          failure();  //Copy pasted method from Ajax call
   }

Note – Obj.output will have both respose header and body. If we need to get body result we need to extract from obj.output

 

2)Extract crsf token from header file

As we have mentioned  csrf-token: fetch in the header statement we will crsf token in the header response and stored in the file mentioned after -D path

ctx.exec  is as windows shell script which executes WScript.Shell.Exec. This is different from Command prompt.

So, to execute as command prompt we need to use

‘%comspec% /c’

Now, to extract crsf token from header file and create separate token file.

var cmd1 = '%comspec% /c findstr "x-csrf-token" C:\\ProgramData\\SAP\\headers.txt > C:\\ProgramData\\SAP\\token.txt';

Now execute command using ctx.exec to create token file from header file, similar as mentioned in 1st step / screenshot

 

3)Create json file

We will have json data in data which is available in the post method(earlier used by ajax calls)

In ctx we have json.stingfy and serialize, which should convert json object into a string. But I do not what is happening those two statements are not giving me desired results.

As per documentation

var txt = ctx.serialize( { name:'Ford', firstname:'John' }, false, false);
// result is : "{\"name\":\"Ford\",\"firstname\":\"John\"}"

var txt = ctx.json.stringify( { name:'Ford', firstname:'John' }, false, false);
// result is : "{\"name\":\"Ford\",\"firstname\":\"John\"}"

 

Anyways, we have an alternative to json stringy as we can store json file as a file and can use in post url

Prepare Command as mentioned below

var cmd2 = '%comspec% /c echo ' + data + ' > C:\\ProgramData\\SAP\\loadjson.json'

Now ,execute cmd2 using ctx.exec as mentioned in the screenshot

 

4)Post URL

var cmd3 = 'curl -i –v -D C:\\ProgramData\\SAP\\headers.txt -b C:\\ProgramData\\SAP\\cookie.txt' +
	    ' -H "Content-Type: application/json; charset=utf-8" -H "Accept: application/json" -H @C:\\ProgramData\\SAP\\token.txt' +
	    ' -u '+params.credentials.username+":"+params.credentials.password +
	    ' -X POST -d @C:\\ProgramData\\SAP\\loadjson.json ' + params.postUrl;

Here we have mentioned -D and file name =>Headers from post response will be save into this file -This will help us when we want to check about response headers

To pass token file, we have used

-H @C:\\Tokenfile.txt,

which we have created in step 2

 

To pass data, we have used

-d @C:\\loadjson.json

which we created in Step 3

 

Now, we can use ctx.exec to run the command

try {
         ctx.exec(cmd3, 30000, function(res1){
  	  response = res1.output;
   	  success(response);
        });
      }
    catch (ex){
     ctx.log('error : ' + ex.message);
			failure();
      }	

Response will be available at res1.output. It will have both header and body response.

 

Note:

I have mentioned on how to get body response either from Get or POst call. I will try to work on this thing some other blog.

To test any curl command we can directly try in windows command prompt.

2 Comments
You must be Logged on to comment or reply to a post.
  • Hi Siva – great blog around the CURL part. If I understand it correctly, you were activating the standard bot and that it did not work because standard code uses AJAX and ideally it should be CURL. Is this what it means ? Then isn’t this a bug in the standard code?

    Thanks & Regards

    SR.

    • I don’t know if we can call it as bug. Ajax should genrally. I do not know why Ajax is not working. I haven’t worked with basis team on this. may be there is some settings or configutation or somework around should be availble to work on ajax.

      But ajx is not working for me, as per sap note availble i tried with curl and it worked.

      thanks-

      Siva