• Julie N
  • NEWBIE
  • 20 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 4
    Replies
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.
I cannot complete my Trail head challenge for Create Reports with the Report Builder it states

Challenge Not yet complete... here's what's wrong: 
Please check that you have a filter for Amounts greater than 25,000.

It will accept USD even though i have done the following?

Create a report to show opportunities that are greater than the amount specified below. Note: Because this challenge includes a USD currency filter, you may need to update your personal preferences. Go to My Settings | Personal | Language & Time Zone and change the Locale to English (United States) and the Language to English and click Save.
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.
create an Apex class that inserts a new account named after an incoming parameter. If the account is successfully inserted, the method should return the account record. If a DML exception occurs, the method should return null?
The Apex class must be called 'AccountHandler' and be in the public scope.
The Apex class must have a public static method called 'insertNewAccount'.
The 'insertNewAccount' method must accept an incoming string as a parameter, name the account after the parameter, insert it into the system and then return the account record.
The 'insertNewAccount' method must also accept an empty string, catch the failed DML and return null.