+ Start a Discussion
Abhishek Pal 33Abhishek Pal 33 

How to write data in csv using apex?

Hi,

I have a field (Flag) in csv file , I want to set this field to true whenever new user is inserted in salesforce through apex code.

Below is the code:

public void getDataFromCsv(){
        Document documentTest=[select id, body from document where developername = 'New_Hire_Data'];
        List<User> userDetails= new List<User> ();
        string strCsvBody = '';
        string[] arrCsv;
        String[] inputvalues = new String[]{};
            List<String> roleList = new List<String>();
        List<Territory> createRole= new List<Territory>();
        User test = new User();
        UserRole createNewRole= new UserRole(); 
        
            strCsvBody = documentTest.body.toString();
            arrCsv = strCsvBody.split('\n');                
            System.debug('array value '+ arrCsv);
        
        for (Integer i=1;i<arrCsv.size();i++)   {
            
            inputvalues = arrCsv[i].split(',');
            System.debug('input :'+inputvalues);
            test.Alias= inputvalues[0];
            test.CommunityNickname=inputvalues[1];
            test.Email=inputvalues[2];
            test.FirstName=inputvalues[3];
            test.LastName=inputvalues[4];
            test.ProfileId=inputvalues[5];
            test.Username=inputvalues[6];
            test.EmailEncodingKey=inputvalues[7];
            test.LanguageLocaleKey=inputvalues[8];
            test.LocaleSidKey=inputvalues[9];
            test.TimeZoneSidKey=inputvalues[10];
            roleList.add(inputvalues[11]);
            System.debug('role :'+roleList);
            // test.UserRoleId=inputvalues[12];
            userDetails.add(test);
            System.debug('User details'+userDetails);
            insert userDetails;
        }