function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deepak Sharma 12Deepak Sharma 12 

Issue in importing data from CSV in visualforce page and handling line break in field value

Hi Guys
I want to import data from CSV in visual force where i am handling the line break. But after spending hours found no solution for this. Please anybody can tell me how to handle line break and insert data in salesforce custom object.

Thanks in advance.
AMIT KAMBOJAMIT KAMBOJ
Hi Deepak

Refer to the code for page and class. You will get the idea.
Please mark this answer as solution if this solves the issue.
 
<apex:page controller="RecordMerge">
<apex:form > 

<apex:pageblock >
<apex:pageMessages id="info" ></apex:pageMessages>
  <apex:pageblocksection title="Merge Accounts" >
  <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> 
  <apex:commandButton action="{!ReadFile}" value="Merge Accounts" id="theAccountButton"/> 
  </apex:pageblocksection>
  
  <apex:pageblocksection title="Merge Contacts" >
  <apex:inputFile value="{!ContactcontentFile}" filename="{!nameFile}" /> 
  <apex:commandButton action="{!ReadContactFile}" value="Merge Contacts" id="theContactButton"/> 
  </apex:pageblocksection>
  
  
  
  
  </apex:pageblock>
 </apex:form>
</apex:page>
 
public class RecordMerge 
{
 public string nameFile{get;set;}
 public Blob contentFile{get;set;}
 public string ContactnameFile{get;set;}
 public Blob ContactcontentFile{get;set;}
 public boolean AfterMerge{get;set;}
 public RecordMerge()
 {
 
 }
 public list<string> TotalIds(string namefile){
 
   List<String> lines = new List<String>();    
    lines = nameFile.split('\n');  
    list<string> newlist = new list<string>();   
    
     for(String line : lines)         
        {
                  List<String> accounts = line.split(',');
                   newlist.add(accounts[0].trim());
                   newlist.add(accounts[1].trim());
                  
    }
    return newlist;
 
 }
public Pagereference ReadFile()
{
   list<string> IdsList = new list<string>();
   List<String> lines = new List<String>();   
    map<string,account> accmap = new map<string,account>(); 
    
   if(!test.isrunningtest())
      nameFile=contentFile.toString();
    else{
           
          nameFile = 'field1,field2,field3';
        }  
    
    
    IdsList =TotalIds(nameFile);
    
       
    lines = nameFile.split('\n');  
    
   
  list<account> acclist = new list<account>([select id,name from account where id IN:IdsList]);                
    for(account acc:acclist){
      accmap.put(acc.id,acc);
    }
        for(String line : lines)         
        {
                  List<String> accounts = line.split(',');
                  
                
                 account masterAcct;
                  account mergeAcct;
                  system.debug('accmap-------'+accmap);
                  if(accmap.containskey(accounts[0].trim())) 
                  {
                     masterAcct = accmap.get(accounts[0].trim());
                     system.debug('masteracct-------'+masterAcct);
                  } 
                 if(accmap.containskey(accounts[1].trim())) 
                  {
                     mergeAcct = accmap.get(accounts[1].trim());
                     system.debug('mergeacct-------'+mergeAcct);
                  }     
                
                 try {
                     if(masterAcct!=null && mergeAcct!=null)  
                       merge masterAcct mergeAcct;
                 } catch (DmlException e) {
                // Process exception here 
                
                 }

        }
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'File Succesfully Merged'));  
     return null;
  }
  
public Pagereference ReadContactFile()
{
    list<string> IdsList = new list<string>();
    List<String> lines = new List<String>();
    map<string,contact> conmap = new map<string,contact>();
    
   
    if(!test.isrunningtest())
       ContactnameFile=ContactcontentFile.toString();
    else{
           
          ContactnameFile = 'field1,field2,field3';
        }  
    
    System.debug(nameFile);    
    lines = ContactnameFile.split('\n');
    
    IdsList =TotalIds(ContactnameFile);
    
    list<contact> conlist = new list<contact>([select id,name from contact where id IN:IdsList]);                
    for(contact acc:conlist){
      conmap.put(acc.id,acc);
    }
        for(String line : lines)         
        {
                  List<String> contacts = line.split(',');
                 
                  
                   contact masterCont;
                   contact mergeCont;
                
                  if(conmap.containskey(contacts[0].trim())) 
                  {
                     masterCont = conmap.get(contacts[0].trim());
                     
                  } 
                 if(conmap.containskey(contacts[1].trim())) 
                  {
                     mergeCont = conmap.get(contacts[1].trim());
                     
                  }            
                  
                 try {
                    if(masterCont!=null && mergeCont!=null)   
                    merge masterCont mergeCont;
                 } catch (DmlException e) {
                // Process exception here 
                   //ErrorUtil.Handler(e,'Sync','test', ' POC', 'POC', 'APEX Class', 'Log', 'Merge POC Error');   
                 }

        }
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'File Succesfully Merged'));       
     return null;
  }  
  
  static testMethod void myUnitTest() {

RecordMerge GSRM = new RecordMerge();

GSRM.ReadContactFile();
GSRM.ReadFile();


}
  
}