function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Martina Dimitrieva 2Martina Dimitrieva 2 

How to check how many future callouts are left in my org

Hi, guys,

I would like to check how many future callouts are left in my organization. I know that Salesforce solution to check how many asynchronous apex executions are available is by making a request to the Rest API limits, but that's not helping me. 
My situation is this:
I'm checking before every future callout will I hit limitations. I am doing that by making a query to the AsyncApexJob object where I count all the objects of this type which are with a creation date equal to Datetime.now().addHours(-24) and where JobType is equal to Future.
Something like this: SELECT COUNT() FROM AsyncApexJob WHERE CreatedDate >= :Datetime.now().addHours(-24) and JobType = "Future"; 
But AsyncApexJob object can have more than 200 000 records and I am hitting another limit if I am following Salesforce recommendation to check before every callout. 

Also, the Limit class is not helping me too, because I need organization-wide limits, which is 250 000 callouts or users*200, the limit class only gives me the limits in the current apex context.

So do you have any idea how can I resolve this problem?

Best Regards, 
 
Raj VakatiRaj Vakati
1. You can use Limits Class to get the information. Please refer this link 

https://salesforce.stackexchange.com/questions/3380/how-can-i-determine-how-many-future-calls-have-been-executed-in-24-hours

2 , You can use REST Resouce Limits RI /vXX.X/limits/

User-added image
 
Raj VakatiRaj Vakati
select count() from AsyncApexJob 
            where CreatedDate >= :Datetime.now().addHours(-24) 
            and JobType = 'Future'
 
boolean callStackLimitExceeded = Limits.getFutureCalls() >= Limits.getLimitFutureCalls());

 
Martina Dimitrieva 2Martina Dimitrieva 2
Hi, guys,
That's not helping me. Let me explain why.
I am using this method:
public static Integer GetNumFutureCallsInLast24Hours() {
    return [select count() from AsyncApexJob 
            where CreatedDate >= :Datetime.now().addHours(-24) 
            and JobType = 'Future'];
to check how many future callouts are left for 24 hours, but the problem is that I am doing this check before every future callout so I can be sure that there are left future callouts. So if I use REST API I need this callout to be async and the only way to do it is with a future callout, but before I do that, I have to make sure that there are left future callout, otherwise, if there are 0 future callouts left I will hit the limit. But when I execute this method GetNumFutureCallsInLast24Hours(), SF is giving me an error because the AsyncApexJob table contains more than 200 000 records or more. 

I need another approach to this problem.