I'm working on csv files for specific standard object. In this i'm querying the fields dynamically in batch class and it was created a .csv file in my document folder but when i was open my csv file it shows like this "system.Address[getCity=xxx] " can any one help me in this. And my code is:
string[] Lines;
String sFolder = 'DataBackup';
String query;
transient Database.UpsertResult upsertResults;
DataBackup1__c backup = [select id,LastBackupDate1__c from DataBackup1__c];
Datetime lastDate = backup.LastBackupDate1__c;
List<String> fieldNames = new List<String>();
public Database.QueryLocator start(Database.BatchableContext bc) {
lines = new String[0];
Schema.DescribeSObjectResult result = Account.getSObjectType().getDescribe();
Map<String,Schema.SObjectField> fields = result.fields.getMap();
for(Schema.sObjectField f : fields.values()){
String s = String.valueOf(f);
fieldNames.add(s);
}
String l = String.join(fieldNames,',');
String oName = result.getName();
if ( lastDate == null ){
query= 'SELECT ' + l + ' from ' + oName;
}
else{
System.debug('lastdate::'+lastDate);
query = 'SELECT ' + l + ' FROM ' + oName + ' WHERE LastModifiedDate >= '+ lastDate.formatGmt('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'') ;
system.debug('query:::'+query);
}
return Database.getQueryLocator(query);
}
public void execute (Database.BatchableContext bc, Account[] scope) {
for(Account record : scope){
String line = '';
String a = String.join(fieldNames, '","');
String b = '"'+a+'"';
//header row
if(lines.size() == 0 ) {
line=b;
lines.add(line);
}
else {
// build your csv line here
for(String name : fieldNames){
String values = String.valueOf(record.get(name));
String x = ' '+ values +' ';
line = line+x;
}
lines.add(line);
system.debug('Line No >>> ' + line);
}
}
}
public void finish(Database.BatchableContext bc) {
try{
Integer i = 0;
Id sFolderId = [Select Id From Folder where Name = :sFolder].id;
Document dn = new Document();
String finalResults = String.join(lines, '\n');
lines = null;
Blob csvBlob = Blob.valueOf(finalResults);
dn.Name = 'Account'+DateTime.now();
dn.FolderId = sFolderId;
dn.Body = csvBlob;
dn.ContentType = 'text/csv';
dn.Type = 'csv';
insert dn;
i++;
}catch(Exception e)
{
system.debug(e);
}
//ContactCSV c = new ContactCSV();
//Database.executeBatch(c);
backup.LastBackupDate1__c = Datetime.now();
update backup;
}
And at last i'm updating the DataBackup__c in custom settings.