How To Start A Job Using A Custom Button?

How to start a job using a custom button?

Approach for an Attended Robot

This requirement can be achieved using a bat script with the help of UiRobot.exe execute​​​​​​. Assume that you have a process named "Execute a bat file" in the "Shared" folder.image.png

Steps that need to be followed after the Robot is connected to Orchestrator in the respective machine where the script needs to run by a click button.

1. Create a .bat script (e.g. RunProcess.bat) with this content:

  • @ECHO OFF
  • "C:\Program Files\UiPath\Studio\UiRobot.exe" execute --process-name "Execute a bat file" --folder "Shared"

  1. Right-click on the .bat script and create a Shortcut to the Desktop

  1. Rename the Shortcut to something else if it is needed

  1. Right-click on the shortcut and choose Properties. To run the bat script invisible (without opening a cmd.exe window), in the Run choose Minimized

6.jpg

  1. To choose a different icon for the shortcut, right-click on the shortcut and choose Properties -> Change Icon... -> Browse... -> choose the desired .ico file

5.jpg5.jpg

Results:

  • Double-click on the RunProcess shortcut button.

  • Process run successfully

Reference: Arguments Description

Approach for an Unattended Robot

This requirement can be achieved using a Confidential External Application for Orchestrator API with OR.Jobs scope. This needs to be set by an Administrator user who will have access to External applications.

  1. Access Admin -> External Applications

  1. Add Application

  1. Add Scopes -> Orchestrator API Access -> Application Scope(s): OR.Jobs

  1. Copy App ID and App Secret and store them somewhere as you will need them in the next steps.

  1. Access Orchestrator -> folder -> Automations -> right-click on the page -> Inspect -> Network -> start a job in order to trach the Rest API call from back-end. This will be needed to extract the needed details to build a .ps1 script.

  1. From the available calls, find the one that ends with UiPath.Server.Configuration.OData.StartJobs and get its details.

Find the FolderId value below,

  1. Create a .ps1 (e.g. start_job.ps1) file with the below details (adapt it based on your details).

# Get the access_token

$ClientID = '4c00a473-8726-4806-ace3-0e5815503d0a'

$ClientSecret = 'JEni6NzOCZpL*dC!'

$Scope = 'OR.Jobs'

$header = @{'Content-Type' = "application/x-www-form-urlencoded"}

$body = @{client_id = $ClientID ; client_secret = $ClientSecret; scope = $Scope; grant_type = 'client_credentials'}

$Token = Invoke-RestMethod -Method POST 'https://cloud.uipath.com/identity_/connect/token' -Headers $header -Body $body

$auth = 'Bearer ' + $Token.access_token

$auth | ConvertTo-Json

#Start Job

$StartJobHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"

$FolderId = 2549819

$StartJobHeader.Add("X-UIPATH-OrganizationUnitId", $FolderId)

$StartJobHeader.Add("Accept", "application/json, text/plain, */*")

$StartJobHeader.Add("Content-Type", "application/json")

$StartJobHeader.Add("Authorization", $auth)

$StartJobBody = "{

`n `"startInfo`": {

`n `"ReleaseKey`": `"2a426444-faa7-4a5f-a6ca-cee65cdf2333`",

`n `"JobsCount`": 1,

`n `"JobPriority`": null,

`n `"SpecificPriorityValue`": null,

`n `"Strategy`": `"ModernJobsCount`",

`n `"ResumeOnSameContext`": false,

`n `"RuntimeType`": `"Unattended`",

`n `"RunAsMe`": false,

`n `"InputArguments`": `"{}`",

`n `"MachineSessionIds`": [

`n 16883146

`n ],

`n `"RobotIds`": [

`n 881563

`n ]

`n }

`n}"

$StartJobResponse = Invoke-RestMethod 'https://cloud.uipath.com/YOUR_ORGANIZATION_NAME/YOUR_TENANT_NAME/orchestrator_/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs' -Method 'POST' -Headers $StartJobHeader -Body $StartJobBody

$StartJobResponse | ConvertTo-Json

3.jpg

  1. Create a .bat (e.g. start_job.bat) file with the below details (adapt it based on the .ps1 script local path).

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Users\my_username\Desktop\Introduction\Tutorials\PowerShell commands\Start job from button\start_job.ps1'"

  1. Right-click on the shortcut and choose Properties. In order to run the bat script invisible (without opening a cmd.exe window), in the Run choose Minimized

Results:

Double-click on the Start Job shortcut button.

The process will start and it will execute your workflows.