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
@rajeshdulhai@rajeshdulhai 

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()
    {
        nameFile=contentFile.toString();
        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];
            accstoupload.add(a);
        }
        try{
        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');
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
    
    public List<TwilioImportObject__c> getuploadedAccounts()
    {
        if (accstoupload!= NULL)
            if (accstoupload.size() > 0)
                return accstoupload;
            else
                return null;                    
        else
            return null;
    }            
}

 

 

Visualforce code :

 

<apex:page sidebar="false" controller="FileUploader">
   <apex:form >
      <apex:sectionHeader title="Upload data from CSV file"/>
      <apex:pagemessages />
      <apex:pageBlock >
             <center>
              <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>-->
             </center>  

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

 

contents of csv file

NAME  MOBILENUMBER WorkshopNo

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

Thanks

 

Abhi_TripathiAbhi_Tripathi

Hi,

 

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

 

http://abhithetechknight.blogspot.in/2013/09/salesforcecom-custom-dataloader.html

Naveen KNNaveen KN
Check the below link
https://www.codengine.in/2019/06/blob-is-not-a-valid-utf-8-string-salesforce-apex.html