1

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.

1
  • thanks for your answer but i need to modify the dynamically getting values form sObject how can i? is there any possible way... please help me – sfdc developer Feb 28 '18 at 10:10
0

My Answer is the same as Tushar Sharma. You're code returns both the compound address field and the individual parts of the address field (city, state, postal code, etc.).

To proof out that this is your issue, you can run this code in your anonymous console:

Account a = [SELECT Id, BillingAddress FROM Account LIMIT 1];
system.debug(string.valueOf(a.get('BillingAddress')));

My advice is to exclude the address field altogether as you are still going to get the individual pieces of the address anyway. Something like the below:

for(Schema.sObjectField f : fields.values()){
    if (f.getDescribe().getType() != Schema.DisplayType.Address) {
        String s = String.valueOf(f);
        fieldNames.add(s);
    }
}

Also, you say DataBackup1__c is a custom setting so you shouldn't use a query to access it (that's kind of the point of custom settings). Try

Map<String, DataBackup1__c> backups = DataBackup1__c.getAll();

or

DataBackup1__c backup = DataBackup1__c.getInstance('<the name of the specific record you want>');

to get it. It's a small change but should save you a query.

2
  • your answer help me a lot but when come to latitude and longitude it shows same system.getlatitude[xxxx]. – sfdc developer Feb 28 '18 at 11:15
  • The answer is basically the same, use the code I provided for testing in execute anonymous window and change the field to latitude and longitude to see what outputs and lookup the Schema.DisplayType enum to see if long/lat are fields that you can check for to exclude. – gNerb Feb 28 '18 at 15:55
1

You are getting issue here because you are trying to fetch data from Compound address field. You can't get Data from compound field using this way. You need yo modify your code to handle that

for(Account acc : [SELECT Name, BillingAddress FROM Account])
{
    Address addr = (Address) acc.BillingAddress;
    if(addr != null)
    {
        System.debug('# City '+addr.getCity());
        System.debug('# Country '+addr.getCountry());
    }
}

As you are doing dynamic describe so you need to compare field API name to check if it is compound field and then use this to get data or you can also ignore this field as well. As this is a standard field so name will be same in all orgs.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.