4

With the winter '15 release over the weekend some mission critical code for us is now getting the infamous "Apex CPU time limit exceeded". It worked fine for the past 8 months until this release. (as an aside, I'm not sure what they changed on the CPU limits or CPU power because the code hasn't changed)

The apex code consists of mathematically intensive financial calculations and needs to get moved to a different platform (or JavaScript). However, until that happens we need a stop gap. So, one thought we had was to move the code to asynch execution using the future tag. We've put in the future tag in a sandbox yet the code is still executing synchronously (can't see the class in the jobs queue and the whole system hangs until the error - unlike other asynch code where it just refreshes the user's screen)

Here are some code snippets. Any suggestions of how to fix this? there are no callouts in the code, just a bunch of math functions and a couple loops. The webservice method MathStuffAmort below is being called from a javascript button.

global class MathStuffClass
{  
 Static Contact conUpdate ;
 Static List<WrapperClass> wrapperClassList = new List<WrapperClass>();

 Public Static Decimal PMT(Decimal intrate,Integer nper,Decimal pv)
 {
    return (intrate * pv * ((1 + intrate).pow(nper))) / (1 - ((1 + intrate).pow(nper)));
 }

 Public Static Date NextMonth (Date MonthDate)
 {
     return MonthDate.addMonths(1);
 }

 // Some other financial functions here

 @future
 WebService Static Void MathStuffAmort(String ID,String str1)
  {
    Contact con = new Contact();
    // some other variables declared here
   //ONE query is run for one ID - so the query is not the issue

   //a for loop with a bunch of math - this is where the problem is
   // end loop

   //ONE update query with one updated record that contains the new info

    }

To re-state the question (for clarity), why isn't this code executing asynchronously?

Thanks!

7
  • I've never tried future and WebService together. Have you tried separating them i.e. make the WebService method call a separate future method? That ensures you are using each separately as documented rather than the undocumented (AFAIK) combination of the two.
    – Keith C
    Oct 20, 2014 at 17:36
  • I'm up for anything at this point - how would you suggest we do that? would you make the webservice call an @future as well? Oct 20, 2014 at 17:42
  • See code in answer - yes.
    – Keith C
    Oct 20, 2014 at 17:45
  • If Keith's suggestion works, then you should post feedback to the Apex documentation page on @future to indicate it can't be used directly on a static WebService method
    – cropredy
    Oct 20, 2014 at 18:04
  • @crop1645 sure - how/where do I do that? Lots of documentation resources out there so not sure which one you had in mind... Oct 20, 2014 at 22:18

1 Answer 1

7

From the comment thread, separating the future and WebService annotations may help so they each are being used as documented:

WebService static void MathStuffAmort(String ID,String str1) {
    MathStuffAmortInTheFuture(ID, str1);
}

@future
private static void MathStuffAmortInTheFuture(String ID,String str1) {
    // Existing code
}
2
  • Awesome, this worked! Thanks. Other than not thinking of this myself, what I don't understand is why this type of syntax limitation would exist in Salesforce. Got any ideas? Oct 20, 2014 at 22:15
  • 1
    @StartingAgain Perhaps because the WebService remoting was implemented before the future annotation and was never updated to take notice of the future annotation. At a minimum, the compiler flagging that WebService and future as a combination are not supported would help avoid the uncertainty.
    – Keith C
    Oct 20, 2014 at 22:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .