SAP Intelligent RPA – How to manage errors and timeout with the desktop studio

Advanced developers want to catch and manage errors. It’s pleasant to run a bot that know what to do in case of error. It can retry, go to the other iteration, just stop without error, send an email etc.

With desktop studio project it’s possible but there is some little things to know. In this blog post we will see these one.

The onError and onTimeout callbacks are technical callback, once the machine reach these callback it’s too late, you have to stop your automation.

In thses callbacks you always have to stop the scenario with sc.endScenario(), try to continue the workflow will cause trouble in the state machine.

But you can :

  • Close applications
  • Close instance of excel
  • write the error in a custom error log

Warning:

Do not write asynchronous code or long synchronous code in these blocks, you can have inconsistent behavior between the agent and the factory.

If you want to manage your error in order to have a scenario that never fail and you want to try again some action or continue the scenario, you have to surround the step code with a try catch and go to the step that you need.

Like this:

GLOBAL.scenario({ scMain: function(ev, sc){ var rootData = sc.data; sc.setMode(e.scenario.mode.clearIfRunning); sc.setScenarioTimeout(600000); // Default timeout for global scenario. sc.onError(function(sc, st, ex) \{ sc.endScenario(); }); // Default error handler. sc.onTimeout(30000, function(sc, st) { sc.endScenario(); }); // Default timeout handler for each step. sc.step(GLOBAL.steps.Custom, GLOBAL.steps.Start_scSubautomatio); sc.step(GLOBAL.steps.Custom, GLOBAL.steps.errorManagement); sc.step(GLOBAL.steps.Start_scSubautomatio, null);
}}, ctx.dataManagers.rootData).setId('1c6c65b9-d21b-4867-a8da-5efc4cb0af2a') ; // ----------------------------------------------------------------
// Step: Custom
// ----------------------------------------------------------------
GLOBAL.step({ Custom: function(ev, sc, st) { var rootData = sc.data; ctx.workflow('scMain', '64848f39-2e54-4a25-a3aa-a09e9a3f5467') ; try { // Some code that raise an error sc.endStep(); // Start_scSubautomatio } catch (ex) { sc.endStep("errorManagementStep"); } return;
}});

In order to manage timeout you can use ctx.wait, if you add a sc.endStep in this block the endStep instruction of the step will not be executed.

It’s an excellent way to manage the timeout. The delay of the ctx.wait have to be lower than the timeout of the step (by default 30sec)

GLOBAL.scenario({ scMain: function(ev, sc) { var rootData = sc.data; sc.setMode(e.scenario.mode.clearIfRunning); sc.setScenarioTimeout(600000); // Default timeout for global scenario. sc.onError(function(sc, st, ex) { sc.endScenario(); }); // Default error handler. sc.onTimeout(30000, function(sc, st) { sc.endScenario(); }); // Default timeout handler for each step. sc.step(GLOBAL.steps.Custom, GLOBAL.steps.Start_scSubautomatio); sc.step(GLOBAL.steps.Custom, GLOBAL.steps.timeoutManagement); sc.step(GLOBAL.steps.Start_scSubautomatio, null); sc.step(GLOBAL.steps.timeoutManagement, null);
}}, ctx.dataManagers.rootData).setId('1c6c65b9-d21b-4867-a8da-5efc4cb0af2a') ; // ----------------------------------------------------------------
// Step: Custom
// ----------------------------------------------------------------
GLOBAL.step({ Custom: function(ev, sc, st) { var rootData = sc.data; ctx.workflow('scMain', '64848f39-2e54-4a25-a3aa-a09e9a3f5467') ; ctx.wait(function(ev){ sc.endStep("timeoutManagement"); }, 29000); // Some code that wait more than the timeout (30sec by default) sc.endStep(); // Start_scSubautomatio return;
}});

4 –  Use subautomations

All the last solutions are a little dirty, you have to add this code on every step, it’s difficult to maintain.

Actually the best way to manage these errors is to use subautomations and use the sc.code result. When you use a subautomation you can retrieve the result code of the subautomation in order to try again or do other actions. In the subautomation no need to add ctx.wait or try catch, onError and onTimeout callback will be executed in the subautomation and the result code will be affected.

Here a sample:

GLOBAL.step({ Start_scSubautomatio: function(ev, sc, st) { var rootData = sc.data; ctx.workflow('scMain', 'd03b9346-aba8-4b79-99f3-81abffb439ef') ; // Starts a specified scenario. The data to use and manipulate via the Scenario can also be specified. The "Wait end" option will launch a scenario and wait until the scenario terminates successfully. Once the scenario terminates, execution continues with the next action. GLOBAL.scenarios.scSubautomation.start(undefined).onEnd( function(scData) { if(scData.code == e.error.KO) { sc.endStep("errorManagement"); return; } else if (scData.code == e.error.TimeOut) { sc.endStep("timeoutManagement"); return; } sc.endStep(); // end Scenario return; });
}});

With this article you know how to manage your errors. Keep in mind that the onError and onTimeout callbacks (scenario or step callback) mustn’t be used to retry or continue your scenario.

Now you have the assets to design your code in the right way.

Links to useful resources


For more information on SAP Process Automation: