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
SaifSaif 

How to export 10k+ records Id's in soql?

As I am trying Soql statement to export  10k+ records Id's in a single run
Example: Select Id,Name,Phone From Accounts where Id in ('Record 1','Record  2','Record 3'.......'Record N')
The soql statement is not accepting more than 1k records inquery how to overcome this limit exceeded error?
  
ApuroopApuroop
Hey Saif,

Try the below code in the Anonymous Apex:
Set<Id> allIds = new Set<Id>{'001f200001slpZnAAI', '001f200001xhVsSAAU', '001f200001xhW6oAAE', '001f200001xgJFOAA2'};
List<Account> allAccounts = [SELECT Id, Name, Phone FROM Account WHERE Id =:allIds];
System.debug('Size: '+allAccounts.size()); //Check if you're getting the right number

//Send the CSV file via E-mail

String myHeader = 'ID, NAME, PHONE \n';
String fnlHeader = myHeader;

for(Account acc : allAccounts){
    String recString = acc.Id + ','+ acc.Name +',' + acc.Phone + '\n';
    fnlHeader = fnlHeader + recString;
}

Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(fnlHeader);
string csvname= 'AccountExport_APZ.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'YOUR_EMAIL_ADDRESS'};
    String subject ='Accounts Export From Anonymous Apex!!!';
email.setSubject(subject);
email.setToAddresses( toAddresses );
email.setPlainTextBody('Accounts Export!!');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

Things to remember:
  1. Change the first line by adding your IDs. Like this: new Set<Id>{YOUR_MANY_IDS}
  2. Add your e-mail address to the toAddresses list.
Hope it helps. :)