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

I am having error while import contacts from csv file

Apex code :


public with sharing class FileUploader
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    List<TwilioImportObject__c> accstoupload;
    public Pagereference ReadFile()
        filelines = nameFile.split('\n');
        accstoupload = new List<TwilioImportObject__c>();
        for (Integer i=1;i<filelines.size();i++)
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
            TwilioImportObject__c a = new   TwilioImportObject__c();
            a.Name__c = inputvalues[0];
            a.Mobile_Number__c = inputvalues[1];       
            a.Workshop_Number__c= inputvalues[2];
        insert accstoupload;
        catch (Exception e)
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
        return null;
    public List<TwilioImportObject__c> getuploadedAccounts()
        if (accstoupload!= NULL)
            if (accstoupload.size() > 0)
                return accstoupload;
                return null;                    
            return null;



Visualforce code :


<apex:page sidebar="false" controller="FileUploader">
   <apex:form >
      <apex:sectionHeader title="Upload data from CSV file"/>
      <apex:pagemessages />
      <apex:pageBlock >
              <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> <apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;"/>
            <!--  <br/> <br/> <font color="red"> <b>Note: Please use the standard template to upload Accounts. <a href="{!URLFOR($Resource.AccountUploadTemplate)}" target="_blank"> Click here </a> to download the template. </b> </font>-->

      <apex:pageblocktable value="{!uploadedAccounts}" var="acc" rendered="{!NOT(ISNULL(uploadedAccounts))}">
          <apex:column headerValue=" Name">
              <apex:outputField value="{!acc.Name__c}"/>
          <apex:column headerValue="Mobile number">
              <apex:outputField value="{!acc.Mobile_Number__c}"/>
          <apex:column headerValue="Workshop No">
              <apex:outputField value="{!acc.Workshop_Number__c}"/>


contents of csv file


rajesh 9850432340 1001

girish 8451254565  1002

akash 8478589698 1003


while clicking on upload button its giving error


System.StringException: BLOB is not a valid UTF-8 string

Error is in expression '{!ReadFile}' in component <apex:commandButton> in page uploadaccounts

Class.FileUploader.ReadFile: line 10, column 1


please help me in correcting the code . i hope everything is clear






Go for this link, doing the same that you are trying

Naveen KNNaveen KN
Check the below link