I have a webservice exposed in SFDC. SFDC will receive account records from external system. Based on the accounts received, its parent and child will be updated. It works fine. However when 1000 or more records are sent, it fails with the below error System.LimitException: Apex CPU time limit exceeded

Though my code is very large, i have followed the best practice. Also it worked perfectly fine for 750 records.

Is it a limitation or can something be done on SFDC side to overcome the error.

  • This depends on your code and implementation. Could you explain what you're doing on share the essential structure of your code flow ? Oct 28 '14 at 9:06
  • The basic structure of my code is below: The request will have the details of account, its parent and child. First the accounts will be validated, then based the values in the request , the parent accounts will be queried and updated, then the account will be linked to the parent. secondly the main accounts children will be updated.Finally the error records will be inserted in the error log object. Finally suceeded records will be updated as successful. Oct 28 '14 at 9:26

I'm surprised you're hitting this with 1000 records, are you 100% sure there's no conditions caused by the larger data set (dupes or similar) that's causing an infinite loop in the code?

If there are no such bugs and you simply are hitting the limit then the first option would be to ensure the sending system sends a manageable amount of records each time, staying within a comfortable limit. If that's not possible then you'll need to restructure your solution on the Salesforce side, perhaps insert the information into holding records in a custom object, and then use a batch job to pick those up and turn them into proper accounts. If the accounts are basic but it's a lot of processing afterwards that needs to be done then maybe that's the bit to separate out, it'll depend entirely on what your code is doing.

To sum it up, there's no way to bypass this limit and you have to work within it, it's all part of being on a multi-tenancy system, but to take 10 seconds to process 1000 records definitely suggests that there's an infinite (or massively nested/recursive) loop going on somewhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.