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
Baz DensonBaz Denson 

Future Method cannot be called from future or batch method.

I have 2 future calls. The first creates a record in our accounting software via call out.

I then use another future method to create an invoice in the accounting software and pass it the customer ID returned from the first future method.

I have created 2 seperate apex classes. Both are invocable processes. When a status changes on the opportunity I use process builder to fire the first call to create the customer record. Which creates a customer record related to the account.

When a customer record is created I use process builder to fire the second invocable process to create the invoice.

I am getting the error 'Future Method cannot be called from future or batch method.' 

I can't understand why if I can call the first future method from process builder, and it has finished (as a customer record is created) then why can't I call the second future method from process builder also?




 
Kaustubh LabheKaustubh Labhe
Hi,

Future methods are asynchronous. How will your logic know which method was called first? 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm

 
Baz DensonBaz Denson
The first one is called via a process builder and creates a 'customer' record.

It is the creation of the customer record which is the criteria for running the second call.
 
Kaustubh LabheKaustubh Labhe
Hi, 

Thats Ok and why are you using the future annotation in the first place?
refer to this blog article by Bob on using Invokable Methods: http://bobbuzzard.blogspot.co.uk/2015/02/lightning-process-builder-and-invocable.html

Thankls :) 
Baz DensonBaz Denson
I am using future methods becuase they are callouts to web services.

They are invoked by process builder which then does a callout to the webservice. The first one returns a customer number and stores it in a custom object related to the account.

When the custom object record is created that causes the second process builder to invoke the second call to the webservice to create the invoice.

It is when the second call is made that I get the error.
 
Error element myRule_1_A1 (FlowActionCall).
An Apex error occurred: System.AsyncException: Future method cannot be called from a future or batch method: CreateNominalInvoice.calloutmethod(String, String, String) 
This report lists the elements that the flow interview executed. The report is a beta feature.
We welcome your feedback on IdeaExchange.
Flow Details
Flow Name: Create_Nominal_Invoice
Type: Workflow
Version: 2
Status: Active
Flow Interview Details
Interview Label: Create_Nominal_Invoice-2_InterviewLabel
Current User: Barry Denson (0050O000007nKGn)
Start time: 16/01/2018 13:44
Duration: 0 seconds
How the Interview Started
Barry Denson (0050O000007nKGn) started the flow interview.
Some of this flow's variables were set when the interview started.
myVariable_old = null
myVariable_current = a0L0O00000fxswDUAQ
ASSIGNMENT: myVariable_waitStartTimeAssignment
{!myVariable_waitStartTimeVariable} Equals {!Flow.CurrentDateTime}
Result
{!myVariable_waitStartTimeVariable} = "16/01/2018 13:44"
DECISION: myDecision
Executed this outcome: myRule_1
Outcome conditions: and
1. {!myVariable_current.Xero_Contact_ID__c} (6d759aa3-3a65-4257-9882-e009ca754ab9) Is null false
Logic: All conditions must be true (AND)
CREATENOMINALINVOICE (APEX): myRule_1_A1
Inputs:
AccountID = {!myVariable_current.Account__c} (0010O00001tkFgWQAU)
PracticeName = {!myVariable_current.Name} (Google Inc)
XeroContactID = {!myVariable_current.Xero_Contact_ID__c} (6d759aa3-3a65-4257-9882-e009ca754ab9)
Error Occurred: An Apex error occurred: System.AsyncException: Future method cannot be called from a future or batch method: CreateNominalInvoice.calloutmethod(String, String, String)

as you can see from the error report, the Account Id, Account Name and Xero Contact (Customer) Id are all set so the first call must have completed as the Xero_Contact__c custom object record has been created.
 
Kaustubh LabheKaustubh Labhe
Hi Barry, 

Thats what I am saying, you are trying to do a synchronous process there using future methods. If you read the problem stated in the idea here
https://success.salesforce.com/ideaView?id=08730000000BpR4AAK, that is what you are trying to do. 
Baz DensonBaz Denson
I split the two future calls into seperate processes so thatI could ensure the first was finished before I started the second one.

1. I know that you can invoke a future method with process builder as it is done in call 1.
2. I know the first process is completed because it has created the customer record.

Therefore I can't see what is stopping the second process builder from invoking a new future method.
Baz DensonBaz Denson
how would you suggest I do it?
 
Kaustubh LabheKaustubh Labhe
Hey Barry,

I maybe wrong and there might be better ways to do it, however I will tell you how I did it in the past.

So let the first process flow remain as is. its working fine and inserting the required customer records. 
Lightning - For creating invoices, why not create a lightning component and embed it in a quickAction (I assume you are using Lightning). Follow this post on how to build a webservice in a component (https://developer.salesforce.com/forums/?id=9060G000000XiheQAC). Now add this quickaction on the customer object record and click on it manually to generate an invoice for that customer with its relevant opp and account info. 
Classic - Create a javascript button and call a webservice class. Again put this button on the customer object record and click on it to generate a invoice record. 

Thanks 
Baz DensonBaz Denson
Hi Kaustubh

I am using Lightning but I have only ever created 1 lightning component and I'm not familiar with quick actions.

This whole process is an entirely automated one which takes the customer from filling in a sign up form on the website to having contracts electronically signed and payment authority set up without any human touchpoints other than the customer.

I need to find a way of getting the invoice created automatically.
Baz DensonBaz Denson
I have tried removing the future annotation from both calls so they run synchronously but they dont seem to run at all and I don't even get any log entries
 
PDA GroupPDA Group
Hi Bza,

Have you got the solution for this question?
Please help me if you got it.

Thanks!