API => GET Jobs not working

Hi everyone,

A couple of months ago I created a simple web service that uses UiPath’s API in order to return the status of the latest job that was run. It worked fine for weeks but now suddenly it’s not returning the latest job that was run, but it keeps returning a job that ran a few days ago. Does anyone know what might cause this?

I know there is an update on how to connect 3rd parties to the API, might that be causing this problem?

Also, I have followed the documentation on how to connect a 3rd party application as is shown here but I can’t get it to work. So if someone has successfully used this method I would really appreciate a step by step explanation on how to get it to work.

Thank you in advance!

Hi @thordis

Could you please share the API call that you are making, including the endpoint?

Hi @loginerror ,

Thank you for your response. Here is my initial code that worked well for a few weeks:

Authentication and retrieving the token:

let response = await fetch('https://account.uipath.com/oauth/token', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'X-UIPATH-TenantName': tenantName,
    },
    body: JSON.stringify({ 
        grant_type: "refresh_token",
        client_id: clientId,
        refresh_token: refreshToken
    })
});

The API call with the end point:

let response = await fetch('https://{{cloudUrl}}/{{cloudOrg}}/{{cloudTenant}}/odata/Jobs', {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + token,
        'X-UIPATH-TenantName': tenantName,
        'X-UIPATH-OrganizationUnitId': orgId,
    }
});

And here is the new code I tried out for OAuth 2 Authentication - it doesn’t work and does not return a token, so the method I’m using is probably wrong:

let response = await fetch('https://cloud.uipath.com/identity_/connect/token', {
    method: 'POST',
    body: JSON.stringify({
        'grant_type': 'client_credentials',
        'client_id': clientId,
        'client_secret': clientSecret,
        'scope': 'OR.Jobs',
    })
});

The order in which jobs are returned from odata/Jobs endpoint without any OData parameters isn’t technically defined. If you’d like to find out the status of the last job that was run, try ordering by StartTime desc or EndTime desc, for example /odata/Jobs?$top=1&$orderby=StartTime desc should return the last job that has started. Use StartTime if you’d like to include running jobs which do not have an EndTime property set. If you’re interested in completed jobs only, successful or failed, use EndTime.

Edit: fixed incorrect query string parameter name, $order$orderby

2 Likes

Hi @vasile.bujac

Thank you for your help. It looks like the response limit is 1000 items. Your code works but it keeps returning the oldest job that ran and not the most recent one. So the ‘desc’ ordering is not working. This is now my enpoint: ${URL}/Jobs?$top=1&$order=EndTime desc . Changing desc to asc makes no difference. Any idea on how I can get the most recent job? I don’t have much experience in this area. Thank you in advance.

I’m sorry, I had made a mistake in my original comment. The name of the query string parameter is $orderby, not $order.

It looks like the response limit is 1000 items

That is correct, but with $top=1 you should be getting a single item in the response. Can you try again and let us know if that worked for you?

Thank you @vasile.bujac, now it returns just one job, but it still returns the oldest one instead of the latest job. I tried ‘orderby’, but that didn’t return anything. I also tried ‘order_by’ and that returned the oldest job again.

The very same API is used by Orchestrator UI, it should work. Can you sign in to your Orchestrator tenant, go to the “Jobs” page, open up your browser’s developer tools (F12 usually), go to network tab, sort by Start/End time in descending order by pressing on the grid column and see what request is sent.

This is what I’m getting, you can see the query used for ordering is $orderby=EndTime desc

2 Likes

Thank you so much @vasile.bujac! Using your method I finally found a path that worked:

Jobs?$filter=((CreationTime%20ge%202021-06-03T15:00:47.525Z)%20and%20(ProcessType%20eq%20%27Process%27))&$top=1&$orderby=CreationTime%20desc

It needs some adjustments of course, but now it is returning the latest job that ran. Thank you so much for you help!