You need to sign in to do that
Don't have an account?
Julie 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)
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 :
Thanks for help,
Julie.
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)
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.
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
https://developer.salesforce.com/forums/ForumsMain?id=9060G000000I5CQQA0
a column ' getCountry=FR ' , an other column ' getCountryCode=null ' etc.
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';
}