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
chyun87chyun87 

Looking for a workaround to perform DML operations on a Read-only visualforce page

Dear SF experts,
I have a visualforce page in read-only mode as the collection size in my controller exceeds the maximum size of 1,000. 
However, on the same page I also need to save inputfields but DML operations are not allowed on a read-only page.
On save, the following error page is shown:

Visualforce ErrorHelp for this Page
System.LimitException: Too many DML statements: 1
Error is in expression '{!save}' in component <apex:commandButton>

Are there any workarounds to kill 2 birds with 1 stone? ie allow a collection size of more than 1000 records AND have the page perform DML operations.
I'm aware I could use pagination to query 1000 records iteratively but I would like to avoid this option as I need a search feature on the page which is only useful if ALL records are loaded at once.

Would I be able to save records if I used a popup window + javascript remoting while the parent window is read-only?

Any inputs would be much appreciated!
Regards,
Best Answer chosen by chyun87
KaranrajKaranraj
You can overcome 1000 collection limit using nested list, but remember we have 10,000 record DML operation limit.
List<List<Account>> accounts = new List<List<Acccount>();
List<Account> tempList = new List<Account>();
for(Account a : [select Id, Name from Account]){
    tempList.add(a);
    if(tempList.size() == 1000){
        accounts.add(tempList);
        tempList = new List<Account>();
    }
}
accounts.add(tempList);

 

All Answers

KaranrajKaranraj
You can overcome 1000 collection limit using nested list, but remember we have 10,000 record DML operation limit.
List<List<Account>> accounts = new List<List<Acccount>();
List<Account> tempList = new List<Account>();
for(Account a : [select Id, Name from Account]){
    tempList.add(a);
    if(tempList.size() == 1000){
        accounts.add(tempList);
        tempList = new List<Account>();
    }
}
accounts.add(tempList);

 
This was selected as the best answer
chyun87chyun87
Thanks Karanraj! A nested list resolved my issue. Cheers!