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
Julie NJulie N 

Apex to CSV: problem with Address field

Hello, 
I'm a newbie and working on apex classes in order to extract data into csv files (I don't want to use dataloader, reports or another thing, juste apex code). 
I have problem with the Address field : all the others fields are in one column when I generate the csv (like : "data1","data2","data3" etc). But I have this when the Address field is coming : (I've hidden the first colum which is ok until Address)

Address field : csv format problem

I'm not able to target the field :
- my  "if (fieldMap.keySet().contains('address')) " doesn't work and I don't know how to proceed
- if (fieldMap.keySet() == 'address')    doesn't work too (Tells me : Comparison arguments must be compatible types : Set <String>, String)
And I don't know how to separate the country, the countryCode etc...

My code : 
 
public class DataExtractUser {

    public static void extractUsers() {

        String csv = '';
        String query = 'SELECT ';

        // Getting User object fields
        Schema.DescribeSObjectResult objectDescribe  = User.SObjectType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = objectDescribe.fields.getMap();


        // Building csv's header and inserting required fields in the SOQL request 
        for( String fieldName : fieldMap.keySet() ) {
            // This if doesn't work actually
            if (fieldMap.keySet().contains('address')) {
                // Treatment
            } else {
                csv += '"' + fieldName + '",';
                query += fieldName + ',';
            }

        }

        // Deleting last comma
        query = query.substring(0, query.length()-1);
        query += '\n FROM User';
        csv = csv.substring(0, csv.length()-1);
        csv += '\n';

        List<User> users = Database.query(query);

        // Building csv
        for (User user : users) {
            for ( String fieldName : fieldMap.keySet() ) {
                csv += '"' + user.get(fieldName) + '",';
            }
            csv = csv.substring(0, csv.length());
            csv += '\n';
        }

        //Deleting last \n
        csv = csv.substring(0, csv.length()-1);

        // Generating csv file and recording in Chatter files
        try {
            ContentVersion file = new ContentVersion(
                    title = 'users.csv',
                    versionData = Blob.valueOf(csv),
                    pathOnClient = '/users.csv'
            );
            insert file;
        } catch (Exception e) {
            System.debug('error' + e);
        }
    }

}

Thanks for help, 
Julie.
Best Answer chosen by Julie N
Hemant_JainHemant_Jain
Try this:
if (fieldName == 'address') {
                System.debug('Skip Address');
                // Treatment
            }
This might be required at 2 places:

1. Below the comment: // This if doesn't work actually
2. Inside this for loop:
for (User user : users) {
            for ( String fieldName : fieldMap.keySet() ) {
                csv += '"' + user.get(fieldName) + '",';
            }
            csv = csv.substring(0, csv.length());
            csv += '\n';
        }
 

All Answers

Hemant_JainHemant_Jain
Check the answe in the below post, hope it helps

https://developer.salesforce.com/forums/ForumsMain?id=9060G000000I5CQQA0
Julie NJulie N
Sorry it's not helping me, I don't want to display in an visulaforce page neither a lightning page, I want a .csv file, so that the Address field like : "street","zipcode","country" etc. Not like what I actually have :
a column '    getCountry=FR   ' , an other column '  getCountryCode=null  '   etc.
User-added image
Hemant_JainHemant_Jain
Try this:
if (fieldName == 'address') {
                System.debug('Skip Address');
                // Treatment
            }
This might be required at 2 places:

1. Below the comment: // This if doesn't work actually
2. Inside this for loop:
for (User user : users) {
            for ( String fieldName : fieldMap.keySet() ) {
                csv += '"' + user.get(fieldName) + '",';
            }
            csv = csv.substring(0, csv.length());
            csv += '\n';
        }
 
This was selected as the best answer
Hemant_JainHemant_Jain
Did this work for you? Kindly mark the question as resolved if this worked.
Julie NJulie N
It works, thank you !