+ Start a Discussion
Biswajit Nath 9Biswajit Nath 9 

List is not getting populated in controller

Hi, I have a controller and a VF page where the vf page takes in a csv file and pass it to the controller. The controller follows a particular logic to create user. I am encountering no error but the list named as 'colleaguelist' is not getting populated.

CONTROLLER:
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<User> userlist{get;set;}
public List<Colleague_c__c> colleaguelist;
public List<String> Collid{get;set;}

    public importDataFromCSVController(){
        csvFileLines = new String[]{};
        userlist = New List<User>();
        colleaguelist = New List<Colleague_c__c>();
        collid = New List<String>();
    }
  
  public void importCSVFile(){
       
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n');
           System.debug('####csv' + csvFileLines);
           
           for(Integer i=1;i<csvFileLines.size();i++){
               String[] csvRecordData =csvFileLines[i].split(',');
               collid.add(csvRecordData[0]);
               
           }
           System.debug('####collid' + collid);
           colleaguelist = [Select Id, Name, IsActive__c, CompanyName__c, Email__c, EmployeeNumber__c from Colleague_c__c where EmployeeNumber__c in:collid];
           System.debug('####colllist' + colleaguelist);   
           for(Integer i=1;i<csvFileLines.size();i++){
           system.debug('###for1 entered');
               string[] csvRecordData = csvFileLines[i].split(',');
            for(Colleague_c__c coll: colleaguelist){
            system.debug('###for2 entered');
            system.debug('###csvRecordData[0]' + csvRecordData[0]);
            system.debug('###coll.EmployeeNumber__c' + coll.EmployeeNumber__c);
            system.debug('###coll.IsActive__c' + coll.IsActive__c);
                    if(csvRecordData[0] == coll.EmployeeNumber__c && coll.IsActive__c == true){
                       system.debug('###if entered');
                       User use = new User() ;
                       use.LastName = coll.Name ;            
                       use.Email = coll.Email__c;
                       use.Username = coll.Email__c;
                       use.CommunityNickname = coll.Name;
                       use.ProfileId = '00e28000001D7DM';
                       use.UserRoleId = '00E28000001lvac';
                       use.EmployeeNumber__c = coll.EmployeeNumber__c;
                       userlist.add(use);  
           }
           }
           
        
           }
           system.debug('####userlist' + userlist);
           insert userlist;
           System.debug('####List inserted');
       }
        
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
         
        }
  } 
  }

VF Page:
<apex:page controller="importDataFromCSVController">
<apex:form >
<apex:pagemessages />
<apex:pageBlock >
<apex:pageBlockSection columns="4">
<apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/>
<apex:commandButton value="Import user" action="{!importCSVFile}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Kindly help. Many thanks in advance.
v varaprasadv varaprasad
Hi Biswjit,

your code is working fine. I have tested following code here list is displaying records.Check once EmployeeNumber__c  number is existing in your records or not.

 
public class importDataFromCSVController {
    public Blob csvFileBody{get;set;}
    public string csvAsString{get;set;}
    public String[] csvFileLines{get;set;}
    public List<User> userlist{get;set;}
    public List<account> colleaguelist;
    public List<String> Collid{get;set;}
    
    public importDataFromCSVController(){
        csvFileLines = new String[]{};
            userlist = New List<User>();
        colleaguelist = New List<account>();
        collid = New List<String>();
    }
    
    public void importCSVFile(){
        
       
            csvAsString = csvFileBody.toString();
            csvFileLines = csvAsString.split('\n');
            System.debug('####csv' + csvFileLines);
            
            for(Integer i=1;i<csvFileLines.size();i++){
                String[] csvRecordData =csvFileLines[i].split(',');
                collid.add(csvRecordData[0]);
                
            }
            System.debug('####collid' + collid);
            colleaguelist = [Select Id, Name, accountnumber  from account where accountnumber in:collid];
            System.debug('####colllist' + colleaguelist);   
           
            
        } 
    }

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com




 
Amit Chaudhary 8Amit Chaudhary 8
Are you getting value in "collid" ?


System.debug('####collid' + collid);
           colleaguelist = [Select Id, Name, IsActive__c, CompanyName__c, Email__c, EmployeeNumber__c from Colleague_c__c where EmployeeNumber__c in:collid];

Make sure you have maching record in your system
Biswajit Nath 9Biswajit Nath 9
Hi, Now colleaguelist  is getting populated but user is not getting created though I have correct colleague data created. Please help. I need this code working badly. Many thanks in advance.