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
Øyvind Borgersen 10Øyvind Borgersen 10 

Create and split csv file

Hi, 

I've created the following code to create a csv file with all records. However, due to limitations in the visualforce page I've created for uploading the file, I need to have the csv file split with 1000 records in each file. 

How do I modify the code below to make such split?

public class createVideogramFile {
@Auraenabled
public static void sendNewFile() {
//Create variables
String currentuser = UserInfo.getUserId();
String emailAddress = UserInfo.getUserEmail();
ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;
List <String> videogramRecType = new List <String> ();
List <String> videogramActive = new List <String> ();
//List <String> distId = new List <String> ();
//Add static value to variable
videogramRecType.add('Videogram');
videogramActive.add('Trykt merke');
videogramActive.add('Trykt merke + VOD');
videogramActive.add('VOD');
//distId.add('1867');
System.debug( 'Email address: ' + emailAddress);
System.debug('Videogramactive ' + videogramActive);
//System.debug('distId ' + distId);
//Logic to run if current user is a community user
//Check if there is a contactrecord link on user profile
If(contactId != null) {
ID AccID = [Select AccountID from Contact where id =: contactid].AccountId;
system.debug('ContactID ' + contactID);
system.debug('AccId ' + AccID);
//Create list with videogramcases based on current users account
List < Case > caslist = [SELECT id,
Registration_number__c,
original_title__c
FROM Case
WHERE accountid =: AccID
AND case.RecordType.DeveloperName IN:videogramRecType
//and case.Distributor_ID__c IN:distid
AND case.Active_with__c IN:videogramActive];
system.debug('List of cases IF ' + caslist);
//Logic for email creating
//Add blank column to prevent read of integer in last field
string header = 'Record Id, Registration Number, Originial Title, Number of sales, \n';
string finalstr = header;
for (Case cs: caslist) {
//Create string with @ for later replacement
string recordString = '"' + cs.id + '"@"' + cs.Registration_number__c + '"@"' + cs.original_title__c + '"@"' + '' + '"@"' + '' + '"@"' + '"\n';
//Replace all commas in text field to avoid split errors in upload
string newstring = recordString.replaceAll(',', '').replaceAll('@',',');
finalstr = finalstr + newstring;
//string recordString = '"' + cs.id + '","' + cs.Registration_number__c + '","' + cs.original_title__c + '","' + '' + '","' + '' + '","' + '"\n';
//finalstr = finalstr + recordString;
//csvFieldString.replaceAll(',', '').replaceAll('+',',')
}
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname = 'Videogram.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new list <string>();
toAddresses.add(emailAddress);
String subject = 'Videogram CSV';
email.setsenderdisplayname ('Medietilsynet');
email.setSubject(subject);
email.setToAddresses(toAddresses);
email.setPlainTextBody('Videogram CSV ');
email.setFileAttachments(new Messaging.EmailFileAttachment[] {
csvAttc
});
Messaging.SendEmailResult[] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}