[SalesForce] System.LimitException: Apex CPU time limit exceeded error in web service

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.

Best Answer

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.

Related Topic