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

"Too many save points" error in test code



While I'm testing my custom controllers, I've encountered some problems with save points.


My test code calls several methods to create test data, and some of the methods contains code like bellow:




public void save()
    Savepoint sp = Database.setSavepoint();
    try {
        // delete and insert operations comes here

    } catch (Exception e) {
        throw e;



After preparing the data, I tried to test one of my method which also contains save point, and the test fails because of the following error:


System.LimitException: Too many save points: 6


I thought I had to reduce the number of save points by explicitly discarding them or committing the data, but I couldn't find how to do so.


Could anybody help me on how to solve this problem?


This is problematic only in test code so far as I don't need to call those methods in series in other places.


Thanks and best regards,




It seems you have triggers too in your org which might be getting invoked. These might be having some save points. In salesforce the limits on SOQL query , dmL statements are contextual. So you need to check what all other code is being invoked and try to reduce the savepoints on the whole.


Hope this helps.




Thank you for your response.


In this case, I don't think there's any trigger fired during the test sequence.


I just sequentially call methods which use save points, but they are not nested, so I was wondering if there's any way to discard the save points which are no longer needed (i.e. transaction succeed and doesn't need to rollback) , then I could reduce the number of save points to remain within the limits.


Best regards,