Friday, January 29, 2016

Dynamically Select Objects and Import csv files in visualforce

Hi,

We can create records from visual force page from csv file. Please check the below code
You can dynamically select the object from the dropdown and upload the file. the headers should be salesforce api field names in the csv file which you are trying to load. As there is a limitation on Visualforce view state. So please load the data using dataloader if the file has more records.

Visualforce page:


   
      
      
            
                
                    
                    
                        
                    
                
            
             
{!acc[hvalues]}
Apex Class:
public class DynamicFileUploader
{
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    public String selectedObject {get; set;}
    String[] filelines = new String[]{};
    public List<String> headervalues {get;set;}
    List<Sobject> recordstoupload;

    //get Object Name //
    public List<SelectOption> getObjectNames(){
        List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();     
        List<SelectOption> objNames = new List<SelectOption>();
        objNames.add(new SelectOption('','-- Select --'));
        for(Schema.SObjectType f : gd)
        {
           objNames.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getLabel()));
        }
        objNames.sort();
        return objNames;
    }
    
    public Pagereference ReadFile()
    {
        try{
                System.debug('\n Selected Object'+selectedObject);
                if(selectedObject == null)
                {
                    ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Object Should be selected');
                    ApexPages.addMessage(errormsg);
                    return null;
                }
                //Convert the uploaded file which is in BLOB format into a string
                nameFile =blobToString( contentFile,'ISO-8859-1');
                
                //Now sepatate every row of the excel file
                filelines = nameFile.split('\n');
                Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
                Schema.SObjectType objecttypeinfo=gd.get(selectedObject);
                
                //Iterate through every line and create a Account record for each row
                recordstoupload= new List<Sobject>();
                headerValues =new List<String>();
                headerValues = filelines[0].trim().split(',');
                for (Integer i=1;i<filelines.size();i++)
                {
                    String[] inputvalues = new String[]{};
                    inputvalues = filelines[i].split(',');
                    sObject sObj = Schema.getGlobalDescribe().get(selectedObject).newSObject() ;  
                    for(Integer j=0;j<headerValues.size();j++){
                        sObj.put(headerValues[j],inputvalues[j]) ;  
                    }
                    recordstoupload.add(sObj);
                }
         }
         catch(Exception e){
                 ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured reading the CSV file'+e.getMessage());
                ApexPages.addMessage(errormsg);
         }       
        //Finally, insert the collected records
        try{
            insert recordstoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured inserting the records'+e.getMessage());
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
   
    public List<Sobject> getuploadedAccounts()
    {
        if (recordstoupload!= NULL)
            if (recordstoupload.size() > 0)
                return recordstoupload;
            else
                return null;                    
        else
            return null;
    }  

    public static String blobToString(Blob input, String inCharset){
        String hex = EncodingUtil.convertToHex(input);
        System.assertEquals(0, hex.length() & 1);
        final Integer bytesCount = hex.length() >> 1;
        String[] bytes = new String[bytesCount];
        for(Integer i = 0; i < bytesCount; ++i)
            bytes[i] =  hex.mid(i << 1, 2);
        return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
    }         
}

5 comments:

  1. nice one,pls post more...thanku...

    ReplyDelete
  2. The VF Page code is not working!

    ReplyDelete
  3. Create a VisualForce Component and add allowDML true.
    Then your visualforce page will run.

    ReplyDelete
  4. The VF Page code is not working!
    the error is
    Component <apex:pageMessages> definition does not contain <apex:componentBody> so it cannot be used with any child tags.: Markup

    ReplyDelete
  5. Hi, I'm not able to see visual force code here.
    Can you please upload or add again vf pages.

    ReplyDelete