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
Gareth NguyenGareth Nguyen 

Future Method calls Future Method

Hi guys,

I am having difficulties finding way to solve the issue that one of my trigger actions triggers another action, it throws the AsyncException.

Here is my Trigger, it will trigger before an account is inserted into the object:
trigger createaccount on object(before insert) {
    if (Trigger.isBefore && Trigger.isInsert) {
        createaccountrec.onBeforeInsert(Trigger.new);
    }
}
The trigger calls the method that invokes another action that calls and gets data from API
private static void AccHandler(String accdomain) {
                Http http = new Http();
                HttpRequest request = new HttpRequest();
                request.setEndpoint('api request link');
                request.setHeader('Authorization info');
                request.setMethod('GET');
                HttpResponse response = http.send(request);
                Map<String, Object> res = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                Map<String, Object> var = (Map<String, Object>) res.get(part here);
                String companyName = (String) var.get(field here); 
                Account newAccount = new Account();
                newAccount.Name = companyName;
     	        insert newAccount;
            }
        }
    }
    
    public static void createaccountrec(list<object_name> recAccs) {
        for (object_name recAcc: recAccs) {
            if (!Test.isRunningTest()) {
				AccHandler(recAcc.field);
            }
        }
    }
How can I solve the Assync Exception issue here? I try to put @future(callout = true) but it doesn't work either. This code runs normally if I try to insert account manually, however it will throw the exception if any action, integration try to insert account.

Thank you so much.

 
RituSharmaRituSharma
One future method can't call another future method. It's a SF limitation and there is no way to overcome this. I am not clear about your business need so not sure how you can restructure your implementation, But would like to suggest you to look into the possibility of using queueable APEX.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm
Deal and DealsDeal and Deals
I am not pretty sure about this but its not possible to call another future method wit another future menthod. It's a SF limitation and there is no way to overcome this.
Here are some of Site links in which i faced the issues 
Capital Smart city Islamabad
_______________________
BLUE WORLD CITY (https://dealanddeals.pk/blue-world-city/" target="_blank)